Creating Multiple Dropdown lists and populating them

Jun 20, 2006
Reaction score
Some one please help me with this. I will greatly appreciate any help.
In my codebehind I am populating a dropdownlist from a database like so:

//Some connection prerequisites
vendors_list.DataSource = rdr3;
vendors_list.DataTextField = "vendorname";
vendors_list.DataValueField = "vendorcode";
//Other code to close connections.

The above code works fine and I have no problem displaying the dropdown
field populated from the database
on the .aspx page like so:

<asp dropDownList ID="vendors_list" AutoPostBack="True" Runat="server">

So everything above works fine, no problems here.
However, this is what I am trying to accomplish.

The user should be able to display as many instances/copies of this
dropdownlist as they need.
They enter the desired number in a textbox and that many dropdown lists
need to be populated and shown like in the above example. Here's how I
have attempted it:

<Script Runat="Server">
protected void vendor_add(Object sender, EventArgs e)
int i=0; //increment counter
int z=Convert.ToInt32(numofvendors.Text); //Number of
dropdowns user wants to display
while(z>i) //Loop to display apropriate number of
//dropdown list populated in codebehind
vendors_list.ID = "vendor" + i.ToString(); //to
rename dropdown fields created

The problem with the above code is that it will only show one dropdown
field no matter how many were
requested. The list is populated correctly however. And interestingly
the id of the dropdownfield is
also incremented to the correct value, i.e 'vendors_list.ID = "vendor" +
I just can't understand why it won't show the correct number of
dropdowns. Is it because of postback?

In the above code if I use:

DropDownList droplist = new DropDownList();

it is able to show the correct number of drowpdown lists but not
populated. Can some one please suggest a way to show the correct number
of dropdowns and also have them populated.

P.S. AutoPostback is enabled when "vendor_add" is called. The Dropdowns
are being added to a panel.

Thank you,
Shivam Ray.


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