2016-01-02 2 views
1

Я пытаюсь установить видимость элемента «уль» в C# код позади, но получение исключения нулевогоНастройка Unordered Список Visibility Asp.net ошибка

Функции за страницей (Master страница в данном случае)

protected void profileDropDown(Object sender, ImageClickEventArgs e) 
{ 
    HtmlGenericControl dropDownList = (HtmlGenericControl)Master.FindControl("profileList"); 
    if (dropDownOpen == false) 
    { 
     dropDownList.Visible = true; 
    } 
    else 
    { 
     dropDownList.Visible = false; 
    } 
} 

Html за страницу (Master страницы в данном случае)

<body> 
<form id="form" runat="server"> 
<div runat="server" id="navBarContainer"> 
    <img runat="server" id="imgLogo" src="Images/logo_netflix.png"/> 
    <ul id="navBarLeft"> 
     <li id="liLobbies" class="navItem"><a href="/Lobbies.aspx">Lobbies</a></li> 
    </ul> 
    <ul id="navBarRight"> 
     <li id="liProfile" class="navItem"> 
      <div id="profileHeader"> 
       <img id="imgProfilePic" src="Images/img_user.png"/> 
       <span><asp:Label Text="Profile" runat="server" CssClass="lblProfile"></asp:Label></span> 
       <asp:ImageButton runat="server" id="imgDropDown" src="Images/icon_down_arrow.png" OnClick="profileDropDown"/> 
      </div> 
      <ul id="profileList"> 
       <li id="liAccount" class="navItemProfile"><a class="navItemProfile" href="/Account/Login.aspx">Account</a></li> 
       <li id="liMessages" class="navItemProfile"><a class="navItemProfile" href="/Account/Login.aspx">Messages</a></li> 
      </ul> 
     </li> 
     <li id="liLogin" class="navItem"><a href="/Account/Login.aspx">Login</a></li> 
     <li id="liRegister" class="navItem"><a href="/Account/Reigster.aspx">Register</a></li> 
    </ul> 
</div> 
<asp:ContentPlaceHolder id="body" runat="server"> 
</asp:ContentPlaceHolder> 
</form> 

ответ

0

Вы должны сделать ulrunat=server

<ul id="profileList" runat="server"> 
    <li id="liAccount" class="navItemProfile"><a class="navItemProfile" href="/Account/Login.aspx">Account</a></li> 
    <li id="liMessages" class="navItemProfile"><a class="navItemProfile" href="/Account/Login.aspx">Messages</a></li> 
</ul> 

И вы меняете вам метод, получить доступ к элементу непосредственно

protected void profileDropDown(Object sender, ImageClickEventArgs e) 
{ 
    profileList.Visible = !dropDownOpen; 
}