dropdowns:setting index

S

SJ

Hi,

i'm trying to use codebehind to setup my dropdowns.

myWebFile.aspx has the following code :

<asp:DropDownList id=test runat=server />
<asp:DropDownList id=test2 runat=server />

And i'm defining the ListItems like this in codebehind:

for(int i=0;i<3;i++)
{
test.Items.Add(new ListItem(i.ToString(), i.ToString()));
test2.Items.Add(new ListItem(i.ToString(), i.ToString()));
}

And finally setting the selected index like this :

test.SelectedIndex = 1;
test2.SelectedIndex = 2;

Except the code above doesnt work as expected and sets BOTH test and test2
with a SelectedIndex of 2.
Any ideas why this would happen?

thanks
-SJ
 
G

Guest

IIRC it's because when you set the SelectedIndex it goes back to the source
of the list, which is the ListItem collection and since they both use the
same one it sets the source's index....
That's my guess.....you may want to declare the ListItem object twice, with
2 names, and add seperatly?
 
S

SJ

That fixed it.
thanks!

-SJ.

Curt_C said:
IIRC it's because when you set the SelectedIndex it goes back to the source
of the list, which is the ListItem collection and since they both use the
same one it sets the source's index....
That's my guess.....you may want to declare the ListItem object twice, with
2 names, and add seperatly?
 

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,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top