Question

ASPX:

<asp:Menu id ="menuBar" runat="server" Orientation="Horizontal" 
       IncludeStyleBlock="false">
        <item>
         <asp:MenuItem Text="Home"></asp:MenuItem>
         <asp:MenuItem Text="Folio">
           <asp:MenuItem Text="Nature" NavigateUrl="#" value="1"></asp:MenuItem>
           <asp:MenuItem Text="People" NavigateUrl="#" value="2"></asp:MenuItem>
         </asp:MenuItem>
         <asp:MenuItem Text="Contact">
         <asp:MenuItem Text="admin" NavigateUrl="#" value="3"></asp:MenuItem>
         </asp:MenuItem>
        </items>
</asp:Menu>

C#:

protected void page_load(object sender, eventargs e)
{
  List<tblDetal> lstDetail = objDal.GetData();
  foreach(MenuItem parent in menuBar.Items)
  {
    if(parent.ChildItems.Count > 0)
     {
       foreach(MenuItem child in parent.ChildItems)
        {
          bool showItem = false;
          foreach(var item in lstDetail)
           {
             if(convert.toint32(child.value)==item.FormId)
              {
                showItem = true;
                break;
              }
           }
          if(showItem==false)
           parent.ChildItems.Remove(child);
        }
     }
  }
}

That was my simple code to remove items from menu based on some criterion. After the compiler reads, the 'remove' line, it goes back to the foreach loop and it then throws an exception - "Collection was modified. Enumeration operation may not execute".

I understand that, but don't know how to fix this. If it was just one item to be removed, that would have been easier.

Was it helpful?

Solution

You are removing an item from a list while attempting to modify it. You cannot modify a collection while you are using it. What you will need to do is put it in a temporary list and after iteration remove the children that violate the showitem boolean.

protected void Page_Load(object sender, EventArgs e)
{
   List<tblDetal> lstDetail = objDal.GetData();

   //New Code
   List<MenuItem> listOfChildrenToDelete;

   foreach(MenuItem parent in menuBar.Items)
   {
       if(parent.ChildItems.Count > 0)
       {
           //Instantiate new temporary List
           listOfChildrenToDelete = new List<MenuItem>();

           foreach(MenuItem child in parent.ChildItems)
           {
                bool showItem = false;
                foreach(var item in lstDetail)
                {
                    if(Convert.ToInt32(child.value) == item.FormId)
                    {
                       showItem = true;
                       break;
                    }
                }
                if(showItem==false)
                {
                    //Add to new temporary list
                    listOfChildrenToDelete.Add(child);
                }
            }
            //Added Code
           foreach(MenuItem childToDelete in listOfChildrenToDelete)
           {
                //Delete after you are no longer looking at that collection.
                parent.ChildItems.Remove(childToDelete);
           }
        }
    }
}

Does this make sense?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top