我工作在Visual Studio 2013網站項目..如何恰克導航欄項目從母版頁的代碼對於用戶角色的背後,在c#asp.net
我已經在我的導航欄一個有些項目他們是「管理」,我希望它採取用戶,如果他是某個頁面的管理員,並且他是另一個頁面的委員會等等。我已經使用了下面的代碼,但我得到了這個異常:
(An exception of type '
System.NullReferenceException
' occurred in App_Web_2g0memkh.dll but was not handled in user code Additional information: Object reference not set to an instance of an object.)
有沒有人有解決這個問題的想法?
的Site.Master:
<LoggedInTemplate>
<ul class="nav navbar-nav navbar-right" runat="server" id="list">
<li id="A1" runat="server"><a runat="server" title="Go To Control Page" href="~/Admin/control.aspx">Hello, <%: Context.User.Identity.GetUserName() %></a></li>
<li id="A2" runat="server"><a runat="server" title="Go To Control Page" href="~/Committee/control.aspx">Hello, <%: Context.User.Identity.GetUserName() %></a></li>
<li id="A3" runat="server" ><a runat="server" title="Manage your account" href="~/Account/Manage">Hello, <%: Context.User.Identity.GetUserName() %></a></li>
<li><asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" OnLoggingOut="Unnamed_LoggingOut" /></li>
</ul>
</LoggedInTemplate>
Site.master.cs:(我只是測試爲admin)
protected void Page_Load(object sender, EventArgs e)
{
if (Page.User.IsInRole("Admin"))
{
HtmlGenericControl li1 = (HtmlGenericControl)this.Page.Master.FindControl("A1");
li1.Visible = true;
HtmlGenericControl li2 = (HtmlGenericControl)this.Page.Master.FindControl("A2");
li2.Visible = false;
HtmlGenericControl li3 = (HtmlGenericControl)this.Page.Master.FindControl("A3");
li3.Visible = false;
}
}
我認爲這個問題是與標籤!
是的,我嘗試過這一點,但沒有奏效,它工作正常與網頁表單(意味着普通的.aspx頁面),但它不適用於母版頁...它給了我這個錯誤: 錯誤名稱'A1'在當前上下文中不存在 –