how can i hide html list item <li> using c# from code behind

爷,独闯天下 提交于 2020-01-13 11:39:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!