title: How to hide menu items as per roles in asp.net
body: <p>I have <strong>Web.sitemap</strong> file like this  : </p>
  
  <pre><code> &lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" &gt;
      &lt;siteMapNode url="" title=""  description=""&gt;
          &lt;siteMapNode url="~/Home.aspx" title="Home"  description=" this is the home page" /&gt;
          &lt;siteMapNode url="~/ProjectList.aspx" title="Project List"  description="Approved projects" /&gt;
          &lt;siteMapNode url="" title="Project Choices" description=""&gt;
              &lt;siteMapNode url="~/StudentChoices.aspx" title="Student Project Choices"  description="" /&gt;
              &lt;siteMapNode url="~/StaffChoices.aspx" title="Supervisor Project Choices"  description="" /&gt;
          &lt;/siteMapNode&gt;
          &lt;siteMapNode url="~/AllocationList.aspx" title="Project Allocation List"  description="" /&gt;
          &lt;siteMapNode url="" title="Submit Proposal" description="" &gt;
              &lt;siteMapNode url="~/submit.aspx" title="New Proposal"  description="new proposal" /&gt;
              &lt;siteMapNode url="~/reSubmit.aspx" title="Re-Submit Proposal" description="re submit proposal"/&gt;
          &lt;/siteMapNode&gt;
          &lt;siteMapNode url="~/StaffRecords.aspx" title="Staff Records"  description="" &gt;
              &lt;siteMapNode url="~/addStaff.aspx" title="Add new Staff" description="" /&gt;
          &lt;/siteMapNode&gt;
          &lt;siteMapNode url="~/StudentRecords.aspx" title="Student Records"  description="" /&gt;
          &lt;siteMapNode url="~/Administration.aspx" title="Administration"  description="" /&gt;
      &lt;/siteMapNode&gt;
  &lt;/siteMap&gt;
  </code></pre>
  
  <p>in MasterPage i wrote following things : </p>
  
  <pre><code>&lt;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%"&gt;
                  &lt;DynamicHoverStyle BackColor="#9999FF" ForeColor="Black" /&gt;
                  &lt;DynamicMenuItemStyle BackColor="#0099FF" ForeColor="Black" /&gt;
                  &lt;DynamicMenuStyle BackColor="#0099FF" /&gt;
                  &lt;DynamicSelectedStyle BackColor="#0099FF" ForeColor="Black" /&gt;
              &lt;/asp:Menu&gt;
              &lt;asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" /&gt;
              &lt;br /&gt;
  </code></pre>
  
  <p>so, when i load site i am able to all MENU items , </p>
  
  <p>but as per roles i want to remove few items,</p>
  
  <p>for this : </p>
  
  <p>i wrote following things :</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
              if (!IsPostBack)
              {
                  ManageMenuItemAsperRoles();
              }
      } 
  </code></pre>
  
  <p>and in function ManageMenuItemAsperRoles();</p>
  
  <pre><code>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);
          }
      }
  </code></pre>
  
  <p>but problem is , when i put break points i found that, there is no item in : <strong>Menu1.Items;</strong></p>
  
  <p>and i am not able to remove few menu items,</p>
  
  <p>why?</p>