如何从后面的asp.net C#代码中的菜单控件中禁用特定菜单项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15066046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable specific menu item from menu control in asp.net C# code behind
提问by Ayyappan Sekaran
In Master Page:
在母版页中:
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal" Width="60%"
BackColor="#336699" Font-Bold="True"
ForeColor="White">
<StaticMenuStyle BackColor="#336699" />
<StaticSelectedStyle BackColor="#336699" />
<StaticMenuItemStyle BackColor="#336699" />
<DynamicHoverStyle BackColor="#336699" />
<DynamicMenuStyle BackColor="#336699" />
<DynamicMenuItemStyle BackColor="#336699" />
<StaticHoverStyle BackColor="#336699" />
<Items>
<asp:MenuItem Text="Dashboard" NavigateUrl="~/Timecard/Dashboard.aspx">
</asp:MenuItem>
<asp:MenuItem Text="Timecard" NavigateUrl="~/Timecard/TimeCardEntry.aspx">
</asp:MenuItem>
<asp:MenuItem Text="Reports">
<asp:MenuItem Text="Employee Time card Report" NavigateUrl="~/Reports/Employee_Timecard .aspx"></asp:MenuItem>
<asp:MenuItem Text="Employee Leave Detail Report" NavigateUrl="~/Reports/Employee Leave Details.aspx"></asp:MenuItem>
<asp:MenuItem Text="Project wise Report" NavigateUrl="~/Reports/ProjectWise.aspx"></asp:MenuItem>
</asp:MenuItem>
<asp:MenuItem Text="Master">
<asp:MenuItem Text="Company" NavigateUrl="~/Admin/CompanyList.aspx"></asp:MenuItem>
<asp:MenuItem Text="Designation" NavigateUrl="~/Admin/DesignationList.aspx"></asp:MenuItem>
<asp:MenuItem Text="Task" NavigateUrl="~/Admin/TaskList.aspx"></asp:MenuItem>
<asp:MenuItem Text="Project" NavigateUrl="~/Admin/ProjectList.aspx"></asp:MenuItem>
<asp:MenuItem Text="Employee" NavigateUrl="~/Admin/EmployeeList.aspx"></asp:MenuItem>
<asp:MenuItem Text="Employee Transfer/Promotion" NavigateUrl="~/Admin/EmployeeTransferList.aspx"></asp:MenuItem>
<asp:MenuItem Text="Holiday" NavigateUrl="~/Admin/HolidayList.aspx"></asp:MenuItem>
</asp:MenuItem>
</Items>
</asp:Menu>
I want to disable Reports and Master menus in codebehind. I have to check that it was admin or not. if user is in status admin means all menu items should display, if user is not an admin means i have to set visible=false for report and master menu. Please help me.
我想在代码隐藏中禁用报告和主菜单。我必须检查它是否是管理员。如果用户处于管理员状态意味着所有菜单项都应该显示,如果用户不是管理员意味着我必须为报告和主菜单设置可见=假。请帮我。
采纳答案by Ayyappan Sekaran
I got the solution:
我得到了解决方案:
protected void Page_Load(object sender, EventArgs e)
{
if (strAdmin == "False")
{
MenuItem mnuItem = Menu1.FindItem("Reports"); // Find particular item
Menu1.Items.Remove(mnuItem);
MenuItem mnuItem1 = Menu1.FindItem("Master"); // Find particular item
Menu1.Items.Remove(mnuItem1);
Menu1.Width = Unit.Percentage(30);
}
}
回答by Pandian
Try like below it will work...
像下面这样尝试它会起作用......
if(UserStatus != "Admin")
{
Menu1.Items.Remove(Menu1.FindItem("Reports"));
Menu1.Items.Remove(Menu1.FindItem("Master"));
}
If your menu is in Master Page Then try like below...
如果您的菜单在母版页中然后尝试如下...
var menu = Page.Master.FindControl("Menu1") as Menu;
if (menu != null)
{
menu.Items.Remove(menu.FindItem("Reports"));
menu.Items.Remove(menu.FindItem("Master"));
}