dropdownlist bug?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a web form in which I'm selecting values of 10 different dropdownlists using a for each. I use DirectCast along with FindControl to get each dropdown and set the value. It seems that setting the SelectedIndex of the variable referencing my DropDownList changes the selectedindex of the 9 other dropdowns to match so the same SelectedIndex is set for every dropdown. I've stepped through, and I know the code is not setting the same selectedindex every time. The sp object referenced in my code below is a custom class that allows me to match up values to the list. IN the example below, sp.Attribute might = "VAL_1" which would result in finding a listitem with a value of "VAL_1

Here is the code
Dim ddlAttribute as DropDownLis
i=
For each sp in param
i+=
ddlAttribute = DirectCast(FindControl("ddlSearchAtt_" & i),DropDownList
ddlAttribute.SelectedIndex = ddlAttribute.Items.IndexOf(ddlAttribute.Items.FindByValue(sp.Attribute)
Nex

thanks in advanc
sk
 
It's not a bug.

FindControl returns a reference to a control. This is one memory address
where a control resides. You take this one reference and you add it to 9
places. Then, you say to this one reference, which is found in 9 places, put
a selectedindex attribute on yourself. So it does. But, remember there are 9
other places where this reference is found all pointing back to this one
area in memory which represents a control so the 9 places have no choice but
to reflect what ever the control does. makes sense?
Here is what it looks like diagramatically

- - - - - - - - -
|
______|_______
|control selection|
-----------------

What you are probably after is to make a new control for every pass of the
loop.
--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
skyking1 said:
I have a web form in which I'm selecting values of 10 different
dropdownlists using a for each. I use DirectCast along with FindControl to
get each dropdown and set the value. It seems that setting the SelectedIndex
of the variable referencing my DropDownList changes the selectedindex of the
9 other dropdowns to match so the same SelectedIndex is set for every
dropdown. I've stepped through, and I know the code is not setting the same
selectedindex every time. The sp object referenced in my code below is a
custom class that allows me to match up values to the list. IN the example
below, sp.Attribute might = "VAL_1" which would result in finding a listitem
with a value of "VAL_1"
 
I'm trying to get my hands around what you are saying, but having trouble

Since the form is designed with 10 controls,ddlSearchAtt_1,_2,_3 etc, how is it that findcontrol("ddlSearchAtt_1") and findControl("ddlSearchAtt_2") are returning a reference to the same object

Are you just saying that I need to "redeclare" my ddlAttribute object as new for each iteration of the loop and that should fix it

Thanks alot for your help, this has been killing me.
 
yes

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
skyking1 said:
I'm trying to get my hands around what you are saying, but having trouble.

Since the form is designed with 10 controls,ddlSearchAtt_1,_2,_3 etc, how
is it that findcontrol("ddlSearchAtt_1") and findControl("ddlSearchAtt_2")
are returning a reference to the same object?
Are you just saying that I need to "redeclare" my ddlAttribute object as
new for each iteration of the loop and that should fix it.
 
Interesting. Still doesn't work. I even changed the code to do this

Me.ddlSearchAtt_1.SelectedIndex =
Me.ddlSearchAtt_2.SelectedIndex =

When this code runs, all SelectedIndexes are changed to 4. Any other ideas?
 
Back
Top