asp.net 中菜单项中的 C# 条件语句
C# conditional statements in menuitem in asp.net
我的项目已从 VB 更改为 C#。我确实希望在菜单项中完成条件。我知道可能还有其他方法可以做到这一点,但是,我希望它在菜单项中完成。
我需要在菜单项中设置条件,以便根据用户角色(管理员、协调员、讲师、学习者)显示菜单链接。
下面是菜单代码。括号中的文字表示条件。
<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
<% } %>
在页面的 Load 事件中,检查当前用户的类型。
然后使用此代码找到必要的菜单项并将其删除
MenuItem menuItem = MyMenu.FindItem("Users");
MyMenu.Items.Remove(menuItem);
您可以在后面的代码中完成。如果您有母版页,那将是合适的地方。如果用户不是管理员用户,您可以删除管理员菜单项,如:
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>
我个人不喜欢在标记中混合使用 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);
}
}
我的项目已从 VB 更改为 C#。我确实希望在菜单项中完成条件。我知道可能还有其他方法可以做到这一点,但是,我希望它在菜单项中完成。
我需要在菜单项中设置条件,以便根据用户角色(管理员、协调员、讲师、学习者)显示菜单链接。
下面是菜单代码。括号中的文字表示条件。
<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
<% } %>
在页面的 Load 事件中,检查当前用户的类型。
然后使用此代码找到必要的菜单项并将其删除
MenuItem menuItem = MyMenu.FindItem("Users");
MyMenu.Items.Remove(menuItem);
您可以在后面的代码中完成。如果您有母版页,那将是合适的地方。如果用户不是管理员用户,您可以删除管理员菜单项,如:
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>
我个人不喜欢在标记中混合使用 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);
}
}