【问题标题】:Remove css from div on server side C#从服务器端 C# 的 div 中删除 css
【发布时间】:2015-06-05 16:18:09
【问题描述】:

我有一个 div 标签,它具有隐藏可见性的内联 css

 <div id="SecondGrid" runat="server" style="width:80%; margin-left:10%; visibility:hidden;">
//content
</div>

我在gridview中有一个按钮,点击它我想删除上述div的可见性css

  protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ShowCarModels")
        {
            CarBrandID = Convert.ToInt32(e.CommandArgument.ToString());
            BindGridView2(CarBrandID);
            SecondGrid.Style.Remove("visibility");

        }
    }

div 似乎仍然是隐藏的。你能帮帮我吗?

【问题讨论】:

标签: c# css asp.net gridview


【解决方案1】:

您应该使用Visible 属性。当您在服务器端使用 html 控件时,您可以使用 Visible 属性来显示或隐藏控件。

SecondGrid.Visible = true;

对于上述解决方案,您需要将 html 标记更改为以下内容

<div id="SecondGrid" runat="server" visible="false" style="width:80%; margin-left:10%;">
//content
</div>

但如果您想继续使用您的标记,您也可以使用 HtmlTextWriterStyle 删除样式

SecondGrid.Style.Remove(HtmlTextWriterStyle.Visibility);

【讨论】:

  • 不,它仍然无法正常工作。我认为不能在“GridView1_RowCommand”函数中更改 css。 :(
  • 您是否调试过代码,当单击按钮转到该行以将 Visible 设置为 true 时?
  • 是的,做了调试。它执行该行但 css 'Visibility' 不会改变
  • 为网格中的按钮设置 AutoPostBack="true"。还是不行
  • 正如@Manish 提到的,应该从 Html 代码中删除“Visibility”并仅添加 Visible='false',然后在您的代码中仅设置 Visible=true。不需要改变样式
【解决方案2】:

dint 找到了在“GridView1_RowCommand”下没有任何 css 样式更改不起作用的原因,但找到了解决我的解决方案的方法。只是想分享。

已将 css 放在一个类中

  .SecondG 
        {
            visibility:hidden;
        }

将类分配给 div

  <div id="SecondGrid" runat="server" style="width:80%; margin-left:10%; padding:20px; border:1px solid #E6E6E6;" class="SecondG" >

创建了一个 javascript 来从 div 中删除类

  function ShowModel()
        {
               var d = document.getElementById("<%=SecondGrid.ClientID%>");
               d.className = "";

        }

在网格中单击按钮时调用上述 javascript 函数。

 <asp:TemplateField ItemStyle-Width="5%" HeaderText="Models" HeaderStyle-CssClass="header-center">
                                        <ItemTemplate>
                                            <asp:Button ID="img_Save" runat="server" OnClientClick="ShowModel()" CommandArgument='<%# Eval("CarBrandID") %>' CommandName="ShowCarModels" Width="100%"  Text="Display Models"  />
                                        </ItemTemplate>
                                    </asp:TemplateField>

及其工作。非常感谢大家的帮助..

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 2011-07-09
  • 2018-11-06
  • 1970-01-01
  • 1970-01-01
  • 2018-10-30
  • 2015-07-19
相关资源
最近更新 更多