Creating Multiple Dropdown lists and populating them

Discussion in '.NET' started by enstn85, Jun 20, 2006.

  1. enstn85

    enstn85

    Joined:
    Jun 20, 2006
    Messages:
    1
    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.
     
    enstn85, Jun 20, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Kidd
    Replies:
    3
    Views:
    482
    Andrew Kidd
    Apr 22, 2004
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    410
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. Daniel Nogradi
    Replies:
    3
    Views:
    350
    Dennis Lee Bieber
    Nov 10, 2006
  4. Daz
    Replies:
    2
    Views:
    271
  5. Andrew Kidd

    multiple dropdown lists = multiple data readers???

    Andrew Kidd, Apr 20, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    189
    Andrew Kidd
    Apr 21, 2004
Loading...

Share This Page