Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: menu ASP.NET4.0
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 6-Feb-12 23:41pm
gani77871.3K
Edited 6-Feb-12 23:42pm
v2
Comments
manognya kota at 7-Feb-12 5:49am
   
Added <pre> tags to your code.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
gani7787 at 7-Feb-12 6:10am
   
i am getting error if i add "j" in childitems..?
Varun Sareen at 7-Feb-12 7:03am
   
what error??
Varun Sareen at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
gani7787 at 7-Feb-12 6:04am
   
can you pls. give me the example. i tried ur solution. but, still not working...
manognya kota at 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)

  Print Answers RSS
0 OriginalGriff 554
1 Gihan Liyanage 338
2 RyanDev 248
3 Sergey Alexandrovich Kryukov 243
4 ChauhanAjay 220
0 Sergey Alexandrovich Kryukov 9,011
1 OriginalGriff 7,941
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 7 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100