vb.net selected value bug?

E

eguworks

Hay,
I think there's a slight bug when it comes to setting the selectedvalue
of serveral *dynamicly filled* listboxes.
Its like this:
Have a page with 5 dropdownlist boxes
fill them with 5 items as displayed below

Dim i As Integer = 0
For i = 1 To 5
Dim li As New ListItem
li.Text = i
li.Value = i
DropDownList1.Items.Add(li)
DropDownList2.Items.Add(li)
DropDownList3.Items.Add(li)
DropDownList4.Items.Add(li)
DropDownList5.Items.Add(li)
Next

After that, I want to have each of them select a different item, code
DropDownList1.SelectedValue = "1"
DropDownList2.SelectedValue = "2"
DropDownList3.SelectedValue = "3"
DropDownList4.SelectedValue = "4"
DropDownList5.SelectedValue = "5"

What really happens in this case, when it sets
DropDownList1.SelectedValue to "1", it sets ALL dropdownlists to "1",
same for "2", "3" etc.
It constantly sets all dropdownlists instead of just one
This doesnt happen when the dropdownlist is hard filled.

I can probably think of some javascript workaround, but this is still a
bit odd behaviour.
 
H

Hans Kesting

Hay,
I think there's a slight bug when it comes to setting the selectedvalue
of serveral *dynamicly filled* listboxes.
Its like this:
Have a page with 5 dropdownlist boxes
fill them with 5 items as displayed below

Dim i As Integer = 0
For i = 1 To 5
Dim li As New ListItem
li.Text = i
li.Value = i
DropDownList1.Items.Add(li)
DropDownList2.Items.Add(li)
DropDownList3.Items.Add(li)
DropDownList4.Items.Add(li)
DropDownList5.Items.Add(li)
Next

After that, I want to have each of them select a different item, code
DropDownList1.SelectedValue = "1"
DropDownList2.SelectedValue = "2"
DropDownList3.SelectedValue = "3"
DropDownList4.SelectedValue = "4"
DropDownList5.SelectedValue = "5"

What really happens in this case, when it sets
DropDownList1.SelectedValue to "1", it sets ALL dropdownlists to "1",
same for "2", "3" etc.
It constantly sets all dropdownlists instead of just one
This doesnt happen when the dropdownlist is hard filled.

I can probably think of some javascript workaround, but this is still a
bit odd behaviour.

What I think happens is this:
When you set DropDownList1.SelectedValue = "1", that dropdownlist
searches through it's ListItems for a matching Value. When it has found
one, it sets the "Selected" property of that listitem to True.
BUT: as you have added that single listitem to *all* dropdowns, it gets
set everywhere.

Solution: fill each dropdownlist separately.
By the way: ListItem has a constructor where you can set Text and Value
immediately.


Hans Kesting
 
K

Karl Seguin [MVP]

Hans is right. you only have 1 instance of your listitems per value.

Setting SelectedValue uses a list.Items.FindByValue(value), which enumerates
through the ListItem collection. This is a simple manner of how references
work in general purposes language.

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,814
Latest member
SpicetreeDigital

Latest Threads

Top