asp.net中menuitem中的C#条件语句

时间:2015-02-02 20:54:52

标签: c# asp.net conditional menuitem

我的项目已从VB更改为C#。我确实希望在menuitem中完成条件。我意识到可能有其他方法可以做到这一点,但是,我希望它可以在menuitem中完成。

我需要在menuitem中做一个条件,以便根据用户角色(管理员,协调员,讲师,学习者)显示菜单链接。

以下是菜单代码。括号中的文字显示条件。

<asp:Menu ID="MyMenu" runat="server" DynamichorizonalOffset="2"
          ForeColor="#000E8F" StaticSubMenuIndent="10px" Width="50%" CssClass="verticalmenu" 
          Font-Size="11pt" OnMenuItemClick="MyMenu_MenuItemClick">
    <DynamicMenuStyle CssClass="IE8Fix" verticalPadding="2px" />
    <DynamicMenuItemStyle CssClass="horizonalmenu" VerticalPadding="5px" />
    <StaticMenuItemStyle verticalPadding="2px" ItemSpacing="2px" 
                         CssClass="horizonalmenu" />
    <StaticHoverStyle ForeColor="#7D00B6" />
        <Items>
            <asp:MenuItem Text="Home" Value="Home" NavigateUrl="~/Accounts/Menu.aspx"></asp:MenuItem>
            <asp:MenuItem Text="Search/Register for Classes" Value="Search/Register for Classes" NavigateUrl="~/Registration/SearchCourses.aspx" ></asp:MenuItem>
            <asp:MenuItem Text="Transcript [For Learner Only]" Value="Transcript"  NavigateUrl="~/InserviceHistory/InserviceTranscript.aspx"></asp:MenuItem>
            <asp:MenuItem Text="Request for In-Service Credit [For Learner Only]" Value="My Menu" NavigateUrl="~/InserviceCredit/IndividualRequest/InstructionalIndividualCreditRequest.aspx"></asp:MenuItem>
            <asp:MenuItem Text="Users [Admin, Coord, Instr]" Value="My Menu">
            <asp:MenuItem Text="Administrator [Admin only]" Value="My Menu">
            <asp:MenuItem Text="Add" Value="My Menu"></asp:MenuItem>
            <asp:MenuItem Text="Edit" Value="My Menu"></asp:MenuItem>
            <asp:MenuItem Text="Delete" Value="My Menu"></asp:MenuItem></asp:MenuItem>
            <asp:MenuItem Text="Coordinators [Admin and Coord only]" Value="My Menu">
            <asp:MenuItem Text="Add" Value="My Menu"></asp:MenuItem>
            <asp:MenuItem Text="Edit" Value="My Menu"></asp:MenuItem>
            <asp:MenuItem Text="Delete" Value="My Menu"></asp:MenuItem></asp:MenuItem>
        </Items>
</asp:Menu>

我找到了两种做条件的方法但又不知道如何合并它们,这些是我找到的例子(不是我的菜单示例编码):

    ----Example 1 -----
<%  Dim count As Integer = 0
    Dim message As String 

    If count = 0 Then
        message = "There are no items." 
    ElseIf count = 1 Then
        message = "There is 1 item." 
    Else
        message = "There are " & count & " items." 
    End If
%>

    ----Example 2 ------

<%
if(1==1)
{%>

html code

<% } 
else 
{
%>
NOt 1

<% } %>

3 个答案:

答案 0 :(得分:0)

在页面的Load事件中,检查当前用户的类型。

然后使用此代码查找必要的菜单项并将其删除

MenuItem menuItem = MyMenu.FindItem("Users"); 
MyMenu.Items.Remove(menuItem);

答案 1 :(得分:0)

您可以在后面的代码中执行此操作。如果您有一个母版页,那么这将是适当的地方。如果用户不是管理员用户,则可以使用以下内容删除管理菜单项:

NavigationMenu.Items.RemoveAt(indexOfAdminMenuItem)

您也可以尝试一下:

<asp:Menu ID="MyMenu" runat="server" DynamichorizonalOffset="2" ForeColor="#000E8F" StaticSubMenuIndent="10px" Width="50%" CssClass="verticalmenu" Font-Size="11pt" OnMenuItemClick="MyMenu_MenuItemClick">
    <DynamicMenuStyle CssClass="IE8Fix" verticalPadding="2px" />
    <DynamicMenuItemStyle CssClass="horizonalmenu" VerticalPadding="5px" />
    <StaticMenuItemStyle verticalPadding="2px" ItemSpacing="2px" CssClass="horizonalmenu" />
    <StaticHoverStyle ForeColor="#7D00B6" />
    <Items>
        <asp:MenuItem Text="Home" Value="Home" NavigateUrl="~/Accounts/Menu.aspx"></asp:MenuItem>
        <asp:MenuItem Text="Search/Register for Classes" Value="Search/Register for Classes" NavigateUrl="~/Registration/SearchCourses.aspx" ></asp:MenuItem>
        <asp:MenuItem Text="Transcript [For Learner Only]" Value="Transcript"  NavigateUrl="~/InserviceHistory/InserviceTranscript.aspx"></asp:MenuItem>
        <asp:MenuItem Text="Request for In-Service Credit [For Learner Only]" Value="My Menu" NavigateUrl="~/InserviceCredit/IndividualRequest/InstructionalIndividualCreditRequest.aspx"></asp:MenuItem>
        <asp:MenuItem Text="Users [Admin, Coord, Instr]" Value="My Menu">
        <% 
        if (userIsAdministrator)
        {
        %>
        <asp:MenuItem Text="Administrator [Admin only]" Value="My Menu">
        <%
        }
        %>
        <asp:MenuItem Text="Add" Value="My Menu"></asp:MenuItem>
        <asp:MenuItem Text="Edit" Value="My Menu"></asp:MenuItem>
        <asp:MenuItem Text="Delete" Value="My Menu"></asp:MenuItem></asp:MenuItem>
        <asp:MenuItem Text="Coordinators [Admin and Coord only]" Value="My Menu">
        <asp:MenuItem Text="Add" Value="My Menu"></asp:MenuItem>
        <asp:MenuItem Text="Edit" Value="My Menu"></asp:MenuItem>
        <asp:MenuItem Text="Delete" Value="My Menu"></asp:MenuItem></asp:MenuItem>
    </Items>
</asp:Menu>

答案 2 :(得分:0)

我个人不喜欢在标记中混合使用C#代码。它真的很脆弱。

相反,您可以在场景中从代码隐藏创建整个菜单。

ASPX

<asp:Menu ID="MyMenu" runat="server" DynamichorizonalOffset="2"
    ForeColor="#000E8F" StaticSubMenuIndent="10px" Width="50%" CssClass="verticalmenu"
    Font-Size="11pt" OnMenuItemClick="MyMenu_MenuItemClick">
    <DynamicMenuStyle CssClass="IE8Fix" VerticalPadding="2px" />
    <DynamicMenuItemStyle CssClass="horizonalmenu" VerticalPadding="5px" />
    <StaticMenuItemStyle VerticalPadding="2px" ItemSpacing="2px"
        CssClass="horizonalmenu" />
    <StaticHoverStyle ForeColor="#7D00B6" />
</asp:Menu>

代码背后

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        PopulateMenu();
    }
}

private void PopulateMenu()
{
    MyMenu.Items.Add(new MenuItem
    {
        Text = "Home",
        Value = "Home",
        NavigateUrl = "~/Accounts/Menu.aspx"
    });


    MyMenu.Items.Add(new MenuItem
    {
        Text = "Search/Register for Classes",
        Value = "Search/Register for Classes",
        NavigateUrl = "~/Registration/SearchCourses.aspx"
    });


    if (User.IsInRole("Learner"))
    {
        MyMenu.Items.Add(new MenuItem
        {
            Text = "Transcript",
            Value = "Transcript",
            NavigateUrl = "~/InserviceHistory/InserviceTranscript.aspx"
        });
        MyMenu.Items.Add(new MenuItem
        {
            Text = "Request for In-Service Credit",
            Value = "Request for In-Service Credit",
            NavigateUrl = "~//InserviceCredit/IndividualRequest/InstructionalIndividualCreditRequest.aspx"
        });
    }

    if (User.IsInRole("Admin") || User.IsInRole("Coord") || User.IsInRole("Instr"))
    {
        var usersMenuItem = new MenuItem
        {
            Text = "Users",
            Value = "Users",
            NavigateUrl = "~/Accounts/Users.aspx"
        };

        usersMenuItem.ChildItems.Add(new MenuItem
        {
            Text = "Add",
            Value = "Add",
            NavigateUrl = ""
        });
        usersMenuItem.ChildItems.Add(new MenuItem
        {
            Text = "Edit",
            Value = "Edit",
            NavigateUrl = ""
        });
        usersMenuItem.ChildItems.Add(new MenuItem
        {
            Text = "Delete",
            Value = "Delete",
            NavigateUrl = ""
        });

        MyMenu.Items.Add(usersMenuItem);
    }

    if (User.IsInRole("Admin") || User.IsInRole("Coord"))
    {
        var coordinatorsMenuItem = new MenuItem
        {
            Text = "Coordinators",
            Value = "Coordinators",
            NavigateUrl = "~/Accounts/Users.aspx"
        };

        coordinatorsMenuItem.ChildItems.Add(new MenuItem
        {
            Text = "Add",
            Value = "Add",
            NavigateUrl = ""
        });
        coordinatorsMenuItem.ChildItems.Add(new MenuItem
        {
            Text = "Edit",
            Value = "Edit",
            NavigateUrl = ""
        });
        coordinatorsMenuItem.ChildItems.Add(new MenuItem
        {
            Text = "Delete",
            Value = "Delete",
            NavigateUrl = ""
        });

        MyMenu.Items.Add(coordinatorsMenuItem);
    }
}