li从asp.net

时间:2017-11-14 12:40:36

标签: c# asp.net

我无法访问母版页中的li控件, 当用户不是“admin”时,我需要将其设置为visible = false,使用runat =“server”我无法从代码访问,为什么?我怎么解决这个问题? ASPX

<li runat="server" id="tabadmin"><a runat="server" href="~/DatiInterni.aspx" >Amministrazione sito</a></li>

aspx.cs

if (Context.User.Identity.Name != "admin")
{
    HtmlGenericControl li = (HtmlGenericControl) FindControl("tabadmin");
    li.Attributes.Add("Visible", "false");

}

登录视图

<asp:LoginView runat="server" ViewStateMode="Disabled">   
    <AnonymousTemplate>
        <ul class="nav navbar-nav navbar-right">
        <li><a runat="server" href="~/Account/Login" title="Login">Accedi</a></li>
          <li><a runat="server" href="~/Account/Register" title="Register">Registrati</a></li>  
        </ul>
    </AnonymousTemplate>
     <%--Lato destro della navbar se l'utente si è autenticato--%>
    <LoggedInTemplate>
        <ul class="nav navbar-nav navbar-right"  runat="server">
         <li><a runat="server" href="~/Account/Manage" title="Manage your account">Ciao, <%: Context.User.Identity.GetUserName()  %> </a></li>
        <li class="dropdown">
      <a class="dropdown-toggle" data-toggle="dropdown" href="#">Menu
      <span class="caret"></span></a>
      <ul class="dropdown-menu" runat="server" id="menu">
        <li><a href="/Apparati.aspx">Dati clienti</a></li>
        <li><a href="/DatiInterni.aspx">Dati interni</a></li>
     <li runat="server" id="tabadmin"><a runat="server" href="~/DatiInterni.aspx" >Amministrazione sito</a></li>
     <li>
        <asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Disconnetti" LogoutPageUrl="/Account/Login.aspx" OnLoggingOut="Unnamed_LoggingOut" />
    </li>
      </ul>
    </li>
        </ul>
    </LoggedInTemplate>
</asp:LoginView>

1 个答案:

答案 0 :(得分:0)

解决了这段代码

            <asp:LoginView runat="server" ViewStateMode="Disabled" ID="LogSection">


                <AnonymousTemplate>
                    <ul class="nav navbar-nav navbar-right">
                    <li><a runat="server" href="~/Account/Login" title="Login">Accedi</a></li>
                      <li><a runat="server" href="~/Account/Register" title="Register">Registrati</a></li>  
                    </ul>
                </AnonymousTemplate>
                 <%--Lato destro della navbar se l'utente si è autenticato--%>
                <LoggedInTemplate>
                    <ul class="nav navbar-nav navbar-right"  runat="server">
                     <li><a runat="server" href="~/Account/Manage" title="Manage your account">Ciao, <%: Context.User.Identity.GetUserName()  %> </a></li>
                    <li class="dropdown" id="menu" runat="server">
                  <a class="dropdown-toggle" data-toggle="dropdown" href="#">Menu
                  <span class="caret"></span></a>
                  <ul class="dropdown-menu" runat="server" id="list" >
                    <li><a href="/Apparati.aspx">Dati clienti</a></li>
                    <li><a href="/DatiInterni.aspx">Dati interni</a></li>
                 <li runat="server" id="tabadmin" visible="true"><a runat="server" href="~/Admin/Administration.aspx" >Amministrazione sito</a></li>
                                                    <li>
                            <asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Disconnetti" LogoutPageUrl="/Account/Login.aspx" OnLoggingOut="Unnamed_LoggingOut" />
                        </li>
                  </ul>
                </li>
                    </ul>
                </LoggedInTemplate>
            </asp:LoginView>

背后的代码

    if (Context.User.Identity.Name != "admin" && Context.User.Identity.Name != "")
    {
        HtmlGenericControl menu = (HtmlGenericControl)LogSection.FindControl("menu");
        HtmlGenericControl list = (HtmlGenericControl)menu.FindControl("list");
        HtmlGenericControl li = (HtmlGenericControl)list.FindControl("tabadmin");
        li.Visible = false;
    }