如何在母版页导航栏中隐藏元素 [英] How to hide an element in master page navbar
本文介绍了如何在母版页导航栏中隐藏元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试根据用户角色在母版页中的 navbar
中隐藏菜单选项但是当我尝试调用该元素时,它给了我一个错误.
I'm trying to hide menu options from navbar
in master page based on user roles
but when I tried to call the element it gives me an error.
这就是我所做的:
MasterPage
MasterPage
<div class="navbar-collapse collapse">
<ul id="MasterMenu" class="nav navbar-nav">
<li id="liDashboard">
<a runat="server" href="~/_Dashboard">Dashboard</a>
</li>
<li id="liTicket">
<a runat="server" href="~/Forms/Tickets/_Ticket">Ticket+</a>
</li>
<li id="liReports">
<a runat="server" href="-">Reports</a>
</li>
</ul>
</div>
然后在另一页
if (User.IsInRole("User"))
{
System.Web.UI.HtmlControls.HtmlGenericControl liDashboard = (System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("liDashboard");
liDashboard.Visible = false;
}
我还尝试添加 runat ="server"
,但仍然无法正常工作.
Also I tried to add runat="server"
but it still not working.
推荐答案
Use Null-Conditional operator (?.
) and check for null before assignment:
System.Web.UI.HtmlControls.HtmlGenericControl liDashboard =
(System.Web.UI.HtmlControls.HtmlGenericControl)Master?.FindControl("liDashboard");
if (liDashboard != null) liDashboard.Visible = false;
您还错过了以下行中的 runat ="server"
:
Also you have missed runat="server"
in the following line:
<li id="liDashboard" runat="server">
这篇关于如何在母版页导航栏中隐藏元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文