在主页面中从站点地图中删除子节点

问题描述:

我使用asp:Menu来显示菜单栏。菜单控件使用站点地图作为数据源。现在我想根据权限从站点地图节点中删除一些子节点。我试图操作Mennuitems,但无法删除该子节点。在主页面中从站点地图中删除子节点

这是我的菜单控制

<td class="TRMenu" valign="middle" align="left"> 
    <asp:Menu ID="menu" runat="server" CssClass="menu" EnableViewState="False" Orientation="Horizontal" 
       DataSourceID="newSiteMap"> 
    </asp:Menu> 
    <asp:SiteMapDataSource ID="newSiteMap" runat="server" ShowStartingNode="False" StartingNodeUrl="~/_PL/SPONSOR/Default.aspx" /> 
</td> 

代码这是代码到我的网站地图

<siteMapNode url="~/_PL/SPONSOR/Default.aspx" title="SPONSOR" description="SPONSOR"> 
    <siteMapNode url="~/_PL/SPONSOR/Home.aspx" title="HOME" description="HOME" /> 
    <siteMapNode url="~/_PL/SPONSOR/Default.aspx?0" title="Dash Board" description="Dash Board" /> 
    <siteMapNode url="~/_PL/SPONSOR/SiteViewAll.aspx" title="Site Info." description="Site Information" id="Site"> 
     <siteMapNode url="~/_PL/SPONSOR/Site.aspx" title="Add/Update Site Info." description="Add/Update Site Information" id="Add"/> 
     <siteMapNode url="~/_PL/SPONSOR/SiteViewAll.aspx?0" title="View Site Info." description="View Site Information" /> 
    </siteMapNode> 


    <siteMapNode url="~/_PL/SPONSOR/UserViewAll.aspx" title="User Info." description="User Info" > 
     <siteMapNode url="~/_PL/SPONSOR/User.aspx?New" title="Add/Update User Info." description="Add/Update User Information" /> 
     <siteMapNode url="~/_PL/SPONSOR/UserViewHirerchical.aspx" title="View Hirerchical User" description="View Hirerchical User Information" /> 
     <siteMapNode url="~/_PL/SPONSOR/UserViewAll.aspx?0" title="View User Info." description="View User Information" /> 
    </siteMapNode> 
    <!--<siteMapNode url="~/_PL/SPONSOR/CRFProtocol.aspx" title="CRF Protocol" description="CRF Protocol" />--> 
    <siteMapNode url="~/_PL/SPONSOR/eCRFDownload.aspx" title="CRF Download Forms" description="CRF Download Forms" /> 
    <siteMapNode url="~/_PL/SPONSOR/LogSheet.aspx" title="Log Sheet" description="Log Sheet" /> 
    <siteMapNode url="~/Default.aspx?logout=SPONSOR" title="Logout" description="Logout" /> 
</siteMapNode> 

我想根据许可

<siteMapNode url="~/_PL/SPONSOR/Site.aspx" title="Add/Update Site Info." description="Add/Update Site Information" id="Add"/> 

我删除以下节点尝试过以下方法。

protected void Page_Unload(object sender, EventArgs e) 
{ 

    foreach (MenuItem Item in menu.Items) 
    { 
     if (Item.Text.Contains("Site")) 
     { 
      string str = Item.ChildItems[0].Text; 
      Item.ChildItems.RemoveAt(0); 
     } 

    } 
} 

我在页面加载和预渲染方法中也尝试过这种方法,但当页面加载时仍然存在节点。

我该如何删除它。

我认为正确的方式来实现它在菜单控制的数据绑定,事件:

<asp:Menu ID="menu" runat="server" EnableViewState="False" Orientation="Horizontal" OnDataBound="Menu_DataBound" DataSourceID="newSiteMap"> 


protected void Menu_DataBound(object sender, EventArgs e) 
{ 
    foreach (MenuItem item in menu.Items) 
    { 
     if (Item.Text.Contains("Site")) 
     { 
     string str = Item.ChildItems[0].Text; 
     Item.ChildItems.RemoveAt(0); 
     } 

    } 
} 

Page.Unload-事件认定项目,但似乎并没有被能够影响控制的状态(可能是因为它意味着其他的东西)。