2015-04-17 12 views
0

我工作在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; 
    } 
} 

我認爲這個問題是與標籤!

回答

0

好的謝謝我解決我的問題=)它在LoggedInTemplate中,我通過編程實現了它的功能...如果有人想要獲得這個好處,這是我的解決方案...

我取消了asp:LoginView,並登錄了模板。然後,我給了一個ID,UL標籤和runnat =「服務器」如下

<ul class="nav navbar-nav navbar-right" runat="server" id="LoggedIn" visible="False"> 

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

和我裏面的Page_Load代碼隱藏:

if (Request.IsAuthenticated) 
    { 
     LoggedIn.Visible = true; 
     if (Page.User.IsInRole("Admin")) 
     { 
      A1.Visible = true; 
      A2.Visible = false; 
      A3.Visible = false; 
     } 
    } 
0

我不知道你爲什麼這樣做:

HtmlGenericControl li1 = (HtmlGenericControl)this.Page.Master.FindControl("A1"); 
li1.Visible = true; 

只是叫他們直接

if (Page.User.IsInRole("Admin")) 
{ 
    A1.Visible = true; 
    A2.Visible = false; 
    A3.Visible = false; 
} 

它爲我工作

+0

是的,我嘗試過這一點,但沒有奏效,它工作正常與網頁表單(意味着普通的.aspx頁面),但它不適用於母版頁...它給了我這個錯誤: 錯誤名稱'A1'在當前上下文中不存在 –

相關問題