By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,507 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

problem dynamically removing items from a droplist..?

P: n/a
hi there,

i have the following HTML code:
--------------------------
<asp:DropDownList id="hddList" runat="server">
<asp:ListItem Value="1">Item 1</asp:ListItem>
<asp:ListItem Value="2">Item 2</asp:ListItem>
<asp:ListItem Value="3">Item 3</asp:ListItem>
<asp:ListItem Value="4">Item 4</asp:ListItem>
<asp:ListItem Value="5">Item 5</asp:ListItem>
<asp:ListItem Value="6">Item </asp:ListItem>
</asp:DropDownList>
--------------------------

and the following VB.NET code behind:
----------------------------------
Private Sub hddList_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles hddList.PreRender
If hddList.Items.Count > 0 Then
If Request.QueryString("type") <> "" Then
For Each i As ListItem In hddList.Items
If i.Value <> Request.QueryString("type") Then
'remove item not to be shown
hddList.Items.Remove(i.Text)
End If
Next
End If
End If
End Sub
----------------------------------

What i am doing is if querystring value = , say 3, i want to programatically
remove all items and only leave Value=3 in there but i am getting an error:

Exception Details: System.InvalidOperationException: Collection was
modified; enumeration operation may not execute.

please help,
Paul


Feb 21 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You're using an enumerator, which iterates from item number 1 to item number
last, but you're removing each item as you move forward through the
Collection. This makes the enumerator unable to correctly iterate through
the list, as the size of it has changed. For future reference, if you ever
need to loop through a Collection to find something, start at the end. But
in this case, you don't need any such thing. The DropDownList Items property
is a List ItemCollection, which has a FindByText and FindByValue method,
which you can use without any looping. Example:

If Request.QueryString("type") <> "" Then
Dim li As ListItem =
hddList.Items.FindByValue(Request.QueryString("typ e"))
If Not IsNothing(li) Then
hddList.Items.Remove(li)
End If
End If

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
To a tea you esteem
a hurting back as a wallet.
"Milsnips" <mi******@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
hi there,

i have the following HTML code:
--------------------------
<asp:DropDownList id="hddList" runat="server">
<asp:ListItem Value="1">Item 1</asp:ListItem>
<asp:ListItem Value="2">Item 2</asp:ListItem>
<asp:ListItem Value="3">Item 3</asp:ListItem>
<asp:ListItem Value="4">Item 4</asp:ListItem>
<asp:ListItem Value="5">Item 5</asp:ListItem>
<asp:ListItem Value="6">Item </asp:ListItem>
</asp:DropDownList>
--------------------------

and the following VB.NET code behind:
----------------------------------
Private Sub hddList_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles hddList.PreRender
If hddList.Items.Count > 0 Then
If Request.QueryString("type") <> "" Then
For Each i As ListItem In hddList.Items
If i.Value <> Request.QueryString("type") Then
'remove item not to be shown
hddList.Items.Remove(i.Text)
End If
Next
End If
End If
End Sub
----------------------------------

What i am doing is if querystring value = , say 3, i want to
programatically remove all items and only leave Value=3 in there but i am
getting an error:

Exception Details: System.InvalidOperationException: Collection was
modified; enumeration operation may not execute.

please help,
Paul

Feb 21 '06 #2

P: n/a
thanks Kevin,

i'll try that.. although i want kinda the opposite of what u showed me.. i
want to remove all items that dont equal the querystring("type") - is it
possible this way, or would i need to do something else?

thanks,
Paul

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:OQ*************@tk2msftngp13.phx.gbl...
You're using an enumerator, which iterates from item number 1 to item
number last, but you're removing each item as you move forward through the
Collection. This makes the enumerator unable to correctly iterate through
the list, as the size of it has changed. For future reference, if you ever
need to loop through a Collection to find something, start at the end. But
in this case, you don't need any such thing. The DropDownList Items
property is a List ItemCollection, which has a FindByText and FindByValue
method, which you can use without any looping. Example:

If Request.QueryString("type") <> "" Then
Dim li As ListItem =
hddList.Items.FindByValue(Request.QueryString("typ e"))
If Not IsNothing(li) Then
hddList.Items.Remove(li)
End If
End If

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
To a tea you esteem
a hurting back as a wallet.
"Milsnips" <mi******@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
hi there,

i have the following HTML code:
--------------------------
<asp:DropDownList id="hddList" runat="server">
<asp:ListItem Value="1">Item 1</asp:ListItem>
<asp:ListItem Value="2">Item 2</asp:ListItem>
<asp:ListItem Value="3">Item 3</asp:ListItem>
<asp:ListItem Value="4">Item 4</asp:ListItem>
<asp:ListItem Value="5">Item 5</asp:ListItem>
<asp:ListItem Value="6">Item </asp:ListItem>
</asp:DropDownList>
--------------------------

and the following VB.NET code behind:
----------------------------------
Private Sub hddList_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles hddList.PreRender
If hddList.Items.Count > 0 Then
If Request.QueryString("type") <> "" Then
For Each i As ListItem In hddList.Items
If i.Value <> Request.QueryString("type") Then
'remove item not to be shown
hddList.Items.Remove(i.Text)
End If
Next
End If
End If
End Sub
----------------------------------

What i am doing is if querystring value = , say 3, i want to
programatically remove all items and only leave Value=3 in there but i am
getting an error:

Exception Details: System.InvalidOperationException: Collection was
modified; enumeration operation may not execute.

please help,
Paul


Feb 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.