设置无序列表可见性Asp.net错误

时间:2016-01-02 22:34:13

标签: c# asp.net

我正在尝试设置" ul" c#代码后面的元素,但得到一个空异常

页面后面的功能(本例中的主页面)

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

页面后面的Html(本例中的主页面)

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

1 个答案:

答案 0 :(得分:0)

您必须制作ul runat=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;
}