Creating Multiple Dropdown lists and populating them


Joined
Jun 20, 2006
Messages
1
Reaction score
0
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";
vendors_list.DataBind();
//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
dropdowns
{
vendorpanel.Controls.Add(vendors_list);
//dropdown list populated in codebehind
vendors_list.ID = "vendor" + i.ToString(); //to
rename dropdown fields created
i++;
}
}
</Script>

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.ToString();'.
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();
vendorpanel.Controls.Add(droplist);

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.
 
Ad

Advertisements


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

Top