473,769 Members | 4,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem dynamically removing items from a droplist..?

hi there,

i have the following HTML code:
--------------------------
<asp:DropDownLi st 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:DropDownLis t>
--------------------------

and the following VB.NET code behind:
----------------------------------
Private Sub hddList_PreRend er(ByVal sender As Object, ByVal e As
System.EventArg s) Handles hddList.PreRend er
If hddList.Items.C ount > 0 Then
If Request.QuerySt ring("type") <> "" Then
For Each i As ListItem In hddList.Items
If i.Value <> Request.QuerySt ring("type") Then
'remove item not to be shown
hddList.Items.R emove(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.InvalidO perationExcepti on: Collection was
modified; enumeration operation may not execute.

please help,
Paul


Feb 21 '06 #1
2 4881
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.QuerySt ring("type") <> "" Then
Dim li As ListItem =
hddList.Items.F indByValue(Requ est.QueryString ("type"))
If Not IsNothing(li) Then
hddList.Items.R emove(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******@hotma il.com> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
hi there,

i have the following HTML code:
--------------------------
<asp:DropDownLi st 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:DropDownLis t>
--------------------------

and the following VB.NET code behind:
----------------------------------
Private Sub hddList_PreRend er(ByVal sender As Object, ByVal e As
System.EventArg s) Handles hddList.PreRend er
If hddList.Items.C ount > 0 Then
If Request.QuerySt ring("type") <> "" Then
For Each i As ListItem In hddList.Items
If i.Value <> Request.QuerySt ring("type") Then
'remove item not to be shown
hddList.Items.R emove(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.InvalidO perationExcepti on: 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("ty pe") - is it
possible this way, or would i need to do something else?

thanks,
Paul

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:OQ******** *****@tk2msftng p13.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.QuerySt ring("type") <> "" Then
Dim li As ListItem =
hddList.Items.F indByValue(Requ est.QueryString ("type"))
If Not IsNothing(li) Then
hddList.Items.R emove(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******@hotma il.com> wrote in message
news:%2******** *******@TK2MSFT NGP10.phx.gbl.. .
hi there,

i have the following HTML code:
--------------------------
<asp:DropDownLi st 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:DropDownLis t>
--------------------------

and the following VB.NET code behind:
----------------------------------
Private Sub hddList_PreRend er(ByVal sender As Object, ByVal e As
System.EventArg s) Handles hddList.PreRend er
If hddList.Items.C ount > 0 Then
If Request.QuerySt ring("type") <> "" Then
For Each i As ListItem In hddList.Items
If i.Value <> Request.QuerySt ring("type") Then
'remove item not to be shown
hddList.Items.R emove(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.InvalidO perationExcepti on: 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
1873
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 what the program needs to do, is open a text file, containing hundreds maybe even thousands of items, and remove duplicate items. Also if the line length exceds a certian amount of characters, I wish to remove it. I was thinking of using malloc...
0
2474
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 added row it also removes the row at the end along with the added row. Plz tell me if, I am missing any thing. Code </asp:datagrid>
4
1383
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 reset I have: Sub Page_Load(s As Object, e As EventArgs) If Page.IsPostBack then
5
2141
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 e.ItemType = ItemType.FieldItem And e.ColumnName = "IDEvent" Then Dim ctlEvent As DropDownList = CType(e.Control, DropDownList) Dim compVal AS CompareValidator = New CompareValidator()
10
4029
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 application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms, remove the controls on each of them, and then close the form, but this code should execute only...
3
2153
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 them to the collection. Whenever I uncheck the checkbox, I want to add the items back into the listview from the collection, and remove them from the collection so I can do the process multiple times. The listview tag is being used as a key, so I...
6
2005
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 flicker). http://www.codeproject.com/cs/miscctrl/listviewxp.asp Thanks, Sam
7
10067
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 main menu instead of showing all possible options and only enabling/disabling certain ones. I have a table that stores the menu item name, parent item (if applicable), display order, etc. so that I can dynamically load my
6
4277
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 boxes are generating a postback.here is a code . protected void Page_Load(object sender, EventArgs e) { int selected_question = (int)Session; if (!Page.IsPostBack) { display_blueprint(); string...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10219
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.