如何在asp.net菜单控件中使用for循环删除子项 [英] How to remove child item using for loop in asp.net menu control
问题描述
我正在使用asp.net菜单控件.
我无法删除超过2个子菜单项.我的索引超出范围.
但是,我在子菜单项中有6个以上的值..
为什么即使菜单计数中有该值,索引也会超出范围.
请给for循环删除每个子项...?
最近两个小时我在挣扎...?
看到我的错误代码
-----------------
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
推荐答案
亲爱的朋友,
试试这个,
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
我已经改变了
menuItemMaster.ChildItems.Remove(menuItemMaster.ChildItems.Item(3))到 menuItemMaster.ChildItems.Remove(menuItemMaster.ChildItems [j] .Item(3))
希望这会对您有所帮助
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
您好,
根据我的理解,在您的代码中,您使用的是for循环,它循环6次.
在您的第一个循环中,您将删除一些子项,并重复6次,然后引用已被删除的项.因此,您将获得索引超出范围的异常.
取而代之的是,您可以遍历并删除每个项目(如果它们与值无关).
希望这会有所帮助.
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.
这篇关于如何在asp.net菜单控件中使用for循环删除子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!