如何根据 c# 中母版页代码中的用户角色更改导航栏项目 asp.net
how to chage navbar items with respect to user roles from master page code behind in c# asp.net
我在 visual studio 2013 年从事网站项目..
我的导航栏中有一些项目,其中一个是 "manage",如果用户是某个页面的管理员,如果他是另一个页面的委员会等等,我希望它可以带走用户.. 我使用了以下代码,但出现了这个异常:
(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 :(我只为管理员测试)
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;
}
}
我认为问题出在标签上!
我不知道你为什么要这样做:
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;
}
对我有用
好的,谢谢,我解决了我的问题 =) 它在 LoggedInTemplate 中,我以编程方式完成了它的功能......如果有人想从中受益,这就是我的解决方案......
我取消了 asp:LoginView,并登录了模板。然后我给ul标签一个id,runnat="server"如下
<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;
}
}
我在 visual studio 2013 年从事网站项目..
我的导航栏中有一些项目,其中一个是 "manage",如果用户是某个页面的管理员,如果他是另一个页面的委员会等等,我希望它可以带走用户.. 我使用了以下代码,但出现了这个异常:
(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 :(我只为管理员测试)
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;
}
}
我认为问题出在标签上!
我不知道你为什么要这样做:
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;
}
对我有用
好的,谢谢,我解决了我的问题 =) 它在 LoggedInTemplate 中,我以编程方式完成了它的功能......如果有人想从中受益,这就是我的解决方案......
我取消了 asp:LoginView,并登录了模板。然后我给ul标签一个id,runnat="server"如下
<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;
}
}