我有这样的Web.sitemap文件:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="" title="" description="">
<siteMapNode url="~/Home.aspx" title="Home" description=" this is the home page" />
<siteMapNode url="~/ProjectList.aspx" title="Project List" description="Approved projects" />
<siteMapNode url="" title="Project Choices" description="">
<siteMapNode url="~/StudentChoices.aspx" title="Student Project Choices" description="" />
<siteMapNode url="~/StaffChoices.aspx" title="Supervisor Project Choices" description="" />
</siteMapNode>
<siteMapNode url="~/AllocationList.aspx" title="Project Allocation List" description="" />
<siteMapNode url="" title="Submit Proposal" description="" >
<siteMapNode url="~/submit.aspx" title="New Proposal" description="new proposal" />
<siteMapNode url="~/reSubmit.aspx" title="Re-Submit Proposal" description="re submit proposal"/>
</siteMapNode>
<siteMapNode url="~/StaffRecords.aspx" title="Staff Records" description="" >
<siteMapNode url="~/addStaff.aspx" title="Add new Staff" description="" />
</siteMapNode>
<siteMapNode url="~/StudentRecords.aspx" title="Student Records" description="" />
<siteMapNode url="~/Administration.aspx" title="Administration" description="" />
</siteMapNode>
</siteMap>
在 MasterPage 我写了以下内容:
<asp:Menu ID="Menu1" runat="server" Orientation="Horizontal"
BackColor="#33CCFF" DataSourceID="SiteMapDataSource1" Font-Overline="False"
Font-Size="Larger" ForeColor="Black" ItemWrap="True" StaticDisplayLevels="2"
StaticSubMenuIndent="60px" Width="100%">
<DynamicHoverStyle BackColor="#9999FF" ForeColor="Black" />
<DynamicMenuItemStyle BackColor="#0099FF" ForeColor="Black" />
<DynamicMenuStyle BackColor="#0099FF" />
<DynamicSelectedStyle BackColor="#0099FF" ForeColor="Black" />
</asp:Menu>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<br />
所以,当我加载网站时,我可以访问所有 MENU 项目,
但根据角色我想删除一些项目,
为了这 :
我写了以下内容:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ManageMenuItemAsperRoles();
}
}
并在函数 ManageMenuItemAsperRoles();
public void ManageMenuItemAsperRoles()
{
string role = Session["Roles"].ToString();
string AdminRole = ConfigurationManager.AppSettings["AdminRole"];
string StaffRole = ConfigurationManager.AppSettings["StaffRole"];
string StudentRole = ConfigurationManager.AppSettings["StudentRole"];
if (role == StaffRole)
{
MenuItemCollection menuItems = Menu1.Items;
MenuItem ProjectChoicesItem = new MenuItem();
MenuItem StaffRecordsItem = new MenuItem();
MenuItem StudentRecordsItem = new MenuItem();
foreach (MenuItem menuItem in menuItems)
{
if (menuItem.Text == "Project Choices")
ProjectChoicesItem = menuItem;
}
foreach (MenuItem menuItem in menuItems)
{
if (menuItem.Text == "Staff Records")
StaffRecordsItem = menuItem;
}
foreach (MenuItem menuItem in menuItems)
{
if (menuItem.Text == "Student Records")
StudentRecordsItem = menuItem;
}
menuItems.Remove(ProjectChoicesItem);
menuItems.Remove(StaffRecordsItem);
menuItems.Remove(StudentRecordsItem);
}
}
但问题是,当我放断点时,我发现,没有项目:Menu1.Items;
而且我无法删除几个菜单项,
为什么?