简体   繁体   English

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

[英]Setting Unordered List Visibility Asp.net error

I am trying to set the visibility of a "ul" element in c# code behind but getting a null exception 我试图在后面的C#代码中设置“ ul”元素的可见性,但得到空异常

Function behind Page (Master page in this instance) Page(在本例中为母版页)背后的功能

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

Html behind Page (Master page in this instance) 网页后面的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>

You must make the ul runat=server 您必须使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>

And the you change you method, accessing the element directly 然后您更改方法,直接访问元素

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM