473,883 Members | 1,583 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 4888
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
1879
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
2481
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
1385
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
2144
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
4036
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
2158
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
2009
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
10076
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
4287
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
9933
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9779
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
11114
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...
0
10730
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10407
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...
1
7960
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4605
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
4205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.