473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

problem dynamically removing items from a droplist..?

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
2 4859
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Materialised | last post by:
I have been given the task, of developing a program to sit next to a cgi based c program (I know this is offtopic but my question does only refer to the standard c part of the code). Basically...
0
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically...
4
by: TJS | last post by:
After a post back the selected index should be rest in a droplist to match user's choice, but page always show selected item as first one in the droplist. How can iforce the selected item to be...
5
by: TJS | last post by:
I need to add validation to drodownlist control but it sends back an error message that says: "System.Web.UI.WebControls.DropDownList' does not allow child controls" the code is : If...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
3
by: Matt Michael | last post by:
I have a listview control and a collection object right now that I'm trying to pass information to and from. Whenever I click on the checkbox, I want it to remove certain listview items and add...
6
by: Samuel R. Neff | last post by:
How can you detect the version of windows common controls installed at run-time? I want to implement the ListViewXP (flicker-free) but still need to support non-XP computers (which will have...
7
by: Steve_Black | last post by:
Hello, I'm toying with the idea of loading a MenuStrip (VB.Net 2005) dynamically based on who is logged into my system. Every user has different security settings and I want to customize the...
6
by: shashi shekhar singh | last post by:
Respected Sir, I have to create multiple dynamic dropdownlist boxes and add items dynamically in <asp:table> server control but problem occurs , i.e. except of fist dropdown list no dropdownlist...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.