简体   繁体   English

使用ASP.NET C#代码更改Div样式

[英]Div style changing using asp.net C# code

<div runat="server" class="labels" style="display:none; height: 100%; font-family: 'Segoe UI';">
     <asp:Label ID="Label4" runat="server" Text="Description:" Font-Bold="True"></asp:Label>&nbsp;
     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
     <br />
     <asp:Label ID="Label5" runat="server" Text="Impact:" Font-Bold="True"></asp:Label>&nbsp;
     <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
     <br />
     <asp:Label ID="Label6" runat="server" Text="Recommendation:" Font-Bold="True"></asp:Label>&nbsp;
     <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
     <br /><br />
</div>

I want to change style to display:true as it is know on display: none and i want to done it using c# code. 我想将样式更改为display:true,因为它在显示上是已知的:none,我想使用c#代码完成它。 how to call class name and change its style/attributes... 如何调用类名并更改其样式/属性...

First give an id to your div like this 首先像这样给你的div一个ID

<div runat="server" id="div1"></div> 

And then in your C# code write this to add style to div. 然后在您的C#代码中编写此代码以向div添加样式。

string style = div1.Style[HtmlTextWriterStyle.Display];

if(style.ToLower()=="none")
   div1.Style.Add(HtmlTextWriterStyle.Display, "block");

And here is how you can remove style. 这是删除样式的方法。

div1.Style.Remove(HtmlTextWriterStyle.Display);
<% var display = "block"; 
   if(isHidden){
       display = "none";
   }
%>

<div runat="server" class="labels" style="display:<%=display%>; height: 100%; font-family: 'Segoe UI';">
     <asp:Label ID="Label4" runat="server" Text="Description:" Font-Bold="True"></asp:Label>&nbsp;
     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
     <br />
     <asp:Label ID="Label5" runat="server" Text="Impact:" Font-Bold="True"></asp:Label>&nbsp;
     <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
     <br />
     <asp:Label ID="Label6" runat="server" Text="Recommendation:" Font-Bold="True"></asp:Label>&nbsp;
     <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
     <br /><br />
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM