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 then it errors out with - "Collection was modified. Enumeration opearion may not execute.".
I understand that, but dont know how to fix this. If it was just one item to be removed, that would have been easier.