Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using asp.net menu control.
i am not able to remove more than 2 child menu item. i am getting index out of range.
But, i have more than 6 values in the child menu items..
why i am getting index out of range even if the value is there in the menu count.
Pls. give the for loop to remove the every child item...?
i am getting struggle last two hours...?


see my error code
-----------------
For j As Integer = 0 To menuItemMaster.ChildItems.Count - 1  ==> 6 count will be there
               If menuItemMaster.ChildItems(j).Value = "0" Then
                   menuItemMaster.ChildItems.Remove(menuItemMaster.ChildItems.Item(0))
               End If
               If menuItemMaster.ChildItems(j).Value = "2" Then
                   menuItemMaster.ChildItems.Remove(menuItemMaster.ChildItems.Item(2))
               End If
               If menuItemMaster.ChildItems(j).Value = "3" Then
                   menuItemMaster.ChildItems.Remove(menuItemMaster.ChildItems.Item(3))
               End If
If menuItemMaster.ChildItems(j).Value = "4" Then
                   menuItemMaster.ChildItems.Remove(menuItemMaster.ChildItems.Item(4))  ==> Error : out of index range
               End If
           Next
Posted
Updated 6-Feb-12 23:42pm
v2
Comments
manognya kota 7-Feb-12 5:49am    
Added <pre> tags to your code.

Dear Friend,

Try this,

For j As Integer = 0 To menuItemMaster.ChildItems.Count - 1  ==> 6 count will be there
               If menuItemMaster.ChildItems(j).Value = "0" Then
                   menuItemMaster.ChildItems.Remove(menuItemMaster.ChildItems[j].Item(0))
               End If
               If menuItemMaster.ChildItems(j).Value = "2" Then
                   menuItemMaster.ChildItems.Remove(menuItemMaster.ChildItems[j].Item(2))
               End If
               If menuItemMaster.ChildItems(j).Value = "3" Then
                   menuItemMaster.ChildItems.Remove(menuItemMaster.ChildItems[j].Item(3))
               End If
If menuItemMaster.ChildItems(j).Value = "4" Then
                   menuItemMaster.ChildItems.Remove(menuItemMaster.ChildItems[j].Item(4))  ==> Error : out of index range
               End If
           Next


I have changed
menuItemMaster.ChildItems.Remove(menuItemMaster.ChildItems.Item(3)) to menuItemMaster.ChildItems.Remove(menuItemMaster.ChildItems[j].Item(3))

Hope this will help you out
 
Share this answer
 
Comments
gani7787 7-Feb-12 6:10am    
i am getting error if i add "j" in childitems..?
Varun Sareen 7-Feb-12 7:03am    
what error??
Varun Sareen 7-Feb-12 7:07am    
try out the link provided by manognya kota i.e.,,

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.menuitemcollection.remove.aspx
Hi,

As per my understanding, in your code you are using a for loop, which loops for 6 times say.

In your first loop you are removing few child items and this repeats for 6 times and you refer the items that are already removed.So you get a index out of range exception.

Instead you can loop through and remove each item at once if its irrespective of value.
Hope this helps.
 
Share this answer
 
v2
Comments
gani7787 7-Feb-12 6:04am    
can you pls. give me the example. i tried ur solution. but, still not working...
manognya kota 7-Feb-12 6:27am    
Check this links.

http://forums.asp.net/t/976738.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.menuitemcollection.remove.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900