问题
I want to hide html list item that is "li" tag using C#. But i can't do this. In earlier i just hide DIV tag using c#. But i can't hide "li" tag. Please help me to do this .If you can please send your detail Explanation...
This is my partial code :
this.hide.style.Add("display", "none"); // Error in hide
This is my html code :
<li ID="hide" style="display: Block;"><a href="../list.aspx" >list Approval</a></li>
Please help me to solve this issue ....
回答1:
Add an id
and runat="server"
to your list item:
<li id="fooItem" runat="server">
<%-- ... --%>
</li>
Set the visibility property from code behind (C# example):
if (someBool)
{
fooItem.Visible = false;
}
You can also use this approach for applying/removing a style:
if (someBool)
{
fooItem.Attributes.Add("class", "foobar");
// or removing a style
foobarItem.Attributes.Remove("class");
}
回答2:
You can access a Html item as a GenericHtmlControl by adding the runat='Server' attribute to the markup, you can then access the properties programatically as if it were a "normal" ASP.Net UI control.
<li ID="hide" style="display: Block;" runat="Server"><a href="../list.aspx" >list Approval</a></li>
HtmlGenericControl listItem = this.hide as HtmlGenericControl;
if (listItem != null)
this.hide.style.Add("display", "none");
回答3:
<asp:Panel ID="Panel1" runat="server">
<div >
//place here your list
</div>
</asp:Panel>
and with c# you can hide a panel
来源:https://stackoverflow.com/questions/9632546/how-can-i-hide-html-list-item-li-using-c-sharp-from-code-behind