1

is there any way to make the li with the id liTest and liLogout invisible?

I tried this:

liTest.Visible = false 

but I get this error:

Error 3, The name 'litest' does not exist in the current context

 <asp:LoginView runat="server" ViewStateMode="Disabled">
     <AnonymousTemplate>
         <ul class="nav navbar-nav navbar-right">
             <li><a runat="server" href="~/Account/Register">Register</a></li>
             <li><a runat="server" href="~/Account/Login">Log in</a></li>
             <li><a  ID="liTest" runat="server" >test</a></li>
             <li><a ID="liLogout" runat="server"  >Logout</a></li>
         </ul>
     </AnonymousTemplate>
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
nick bijmoer
  • 99
  • 1
  • 2
  • 14

3 Answers3

0

Reference: How to: Access Server Controls by ID

When a control is not inside a naming container, you can get a reference to it by using the control's ID. When a control is inside a naming container, you must call a method that searches the naming container for the control's ID. A control might also be inside a naming container that you do not have direct access to.

So, you need to write a function to search for the design time ID at runtime, like so:

private Control FindControlRecursive(Control rootControl, string controlID)
{
    if (rootControl.ID == controlID) return rootControl;

    foreach (Control controlToSearch in rootControl.Controls)
    {
        Control controlToReturn = 
            FindControlRecursive(controlToSearch, controlID);
        if (controlToReturn != null) return controlToReturn;
    }
    return null;
}

Then call this function to get a reference to the control you need:

var ctrl = FindControlRecursive(this, "liTest");
if (ctrl !=null) {
   ctrl.Visible = false;
}
muratgu
  • 7,241
  • 3
  • 24
  • 26
0

Replace this:

<li><a  ID="liTest" runat="server" >test</a></li>
<li><a ID="liLogout" runat="server"  >Logout</a></li>

With this:

<li ID="liTest" runat="server"><a>test</a></li>
<li ID="liLogout" runat="server"><a>Logout</a></li>

This makes the LI controls accessible by server software.

If you also want to have the a controls accessible by server software, than you could use something like this:

<li ID="liTest" runat="server"><a ID="liTestA" runat="server">test</a></li>
<li ID="liLogout" runat="server"><a ID="liLogoutA" runat="server">Logout</a></li>
Clíodhna
  • 818
  • 1
  • 15
  • 29
Evert
  • 11
  • 2
  • Thanks to Cliodhna I have a bronze badge! This is my first and only reward yet. See https://stackoverflow.com/questions/10504034/how-do-you-convert-a-string-into-hexadecimal-in-vb-net for my latest answer (comment actually). – Evert Mar 12 '18 at 13:40
-3

Can you use CSS?

#liTest, #liLogout {display:none;}

EDIT: Your id is not for li element, it's for a.

jare25
  • 506
  • 1
  • 7
  • 17
  • I can use CSS, but i would like to make the li invisible for example if( i == 1), I tried to do it like this :
  • Logout
  • but that aint working either – nick bijmoer Jun 13 '15 at 22:23