dropdownlists -- what am i doing wrong?

A

Azmat

Hi everyone...whats wrong here?

I created two dropdownlists in an aspx file. I populated both of them
with 3 simple listitems -- basically containing the numbers 1,2,3.

So I have

dropdownlist1 and dropdownlist2
They both contains 3 list items. The first one's value/text is 1, the
second one's is 2 the third list item's is 3.

Now, I want one dropdownlist to display the number 2 and the other to
display the number 3.

This is what I did.

DropDownList1.SelectedIndex = 1;
DropDownList2.SelectedIndex = 2;

It compiles and runs. However BOTH dropdownlists contain 3 (index =
2) as the selected item. and not just dropdownlist2.

cant figure it out.

Thanks in advance!

azmat
 
V

Victor Garcia Aprea [MVP]

Hi Azmat,

Thats a very simple example and should be working ok. If you have some code
to post it may help the guessing game,

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://obies.com/vga/blog.aspx

To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
 
A

Azmat

Sure Victor...yeah the code is 'really' extremely simple...but it
still wont work ;-). Here it is...


private void Page_Load(object sender, System.EventArgs e)
{
ListItem li1 = new ListItem();
li1.Text = "1";
li1.Value = "1";
ListItem li2 = new ListItem();
li2.Text = "2";
li2.Value = "2";
ListItem li3 = new ListItem();
li3.Value = "3";
li3.Text = "3";


DropDownList1.Items.Insert(0, li1);
DropDownList1.Items.Insert(1, li2);
DropDownList1.Items.Insert(2, li3);

// DropDownList1.SelectedItem.Text = "3";
DropDownList1.SelectedIndex = 2;


DropDownList2.Items.Insert(0, li1);
DropDownList2.Items.Insert(1, li2);
DropDownList2.Items.Insert(2, li3);

// DropDownList2.SelectedItem.Text = "2";
DropDownList2.SelectedIndex = 1;

}

When I run the code, i get two dropdown lists, and the value 2 is
selected for BOTH of them. Both dropdowns have the numbers 1,2,3 in
the collection.

bamboozled!
azmat
 
A

Alvin Bruney

wait. i apologize, i spoke too quickly. this is not a bug.

Insert (index, control) takes an index and a control from the signature.
What is happening is you are adding the same control to two different
dropdowns. It's slide of hand really. The two dropdowns fool you into
believing that the items in the dropdowns belong to different objects, but
they dont. They really belong to one place in memory, which is where the
control resides. Now if you set a selection on the dropdown list, it puts a
selection on the control. Since this one control is shared in two dropdowns,
it has no choice but to appear selected in both dropdowns. In fact it is not
possible, given this scenario, to have two different selections in these two
different dropdowns partly because multiselections in dropdowns are not
allowed. My physics teacher once said if A is touching B, B is touching A.
Same thing applies here.

DROP1 DROP2
| |
|____________|
|
listitem1
listitem2
listitem3

To get the behavior you want you need to create two separate listitem
objects. The following code will work correctly
DROP1 DROP2
| |
| |
listitem1 listitem11
listitem2 listitem22
listitem3 listitem33
//create a new set of list items

ListItem li11 = new ListItem();

li11.Text = "1";

li11.Value = "1";

ListItem li22 = new ListItem();

li22.Text = "2";

li22.Value = "2";

ListItem li33 = new ListItem();

li33.Value = "3";

li33.Text = "3";



DropDownList1.Items.Insert(0, li11);

DropDownList1.Items.Insert(1, li22);

DropDownList1.Items.Insert(2, li33);
 

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,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top