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

3

5

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 ....

Eogene answered 9/3, 2012 at 10:29 Comment(0)
S
18

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");
}
Stuyvesant answered 5/6, 2012 at 20:51 Comment(0)
S
4

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");  
Slr answered 9/3, 2012 at 10:49 Comment(1)
Remember to add "using System.Web.UI.HtmlControls;" to the usingHuddleston
G
0
<asp:Panel ID="Panel1" runat="server">
     <div >
        //place here your list
    </div>
 </asp:Panel>

and with c# you can hide a panel

Galley answered 9/3, 2012 at 10:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.