Click here to Skip to main content
15,917,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two dropdowns dr1,dr2 and a button. dr1 contains items. i want that when i select an item from dr1 and click on button so that selected item must be coppied into dr2. when i select item2 from dr1 and press the button again it copies that item into dr2. i have used a code but i doesnt work and gives and error. can somebody tell me what i need to do.

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        DropDownList2.Items.Add(DropDownList1.SelectedItem);

    }
Posted
Comments
walterhevedeich 5-Aug-11 13:03pm    
What is the error?
saifullahiit 5-Aug-11 13:06pm    
Cannot have multiple items selected in a DropDownList.

You can look at that article on CP;

Multi select Dropdown list in ASP.NET[^]

This will help you select multiple items from a drop down. If you do not want to use this, instead you can use a listbox.
Good luck.
 
Share this answer
 
v3
Comments
#realJSOP 5-Aug-11 13:16pm    
He didn't say that he wanted multiple items selected. I think the problem is that the act of adding the SelectedItem causes the 2nd dropdown to think there's more than one selected.
Orcun Iyigun 5-Aug-11 14:53pm    
Well I think you are right. I am kinda misleaded because of his comment saying "Cannot have multiple items selected in a DropDownList."
Try this:

C#
DropDownList2.Items.Add(DropDownList1.Items[DropDownList1.SelectedIndex]);


You *might* need to do this first:

C#
DropDownList2.SelectedIndex = -1;
 
Share this answer
 
v3
Comments
Mr.Sourav.Maitra 5-Aug-11 14:56pm    
SelectedIndex will store the Index value. This is not the exact probem what he mentioned

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900