.net(dynamic dropdownlist)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smithak82
    New Member
    • Sep 2008
    • 12

    .net(dynamic dropdownlist)

    sir,
    i have a dynamic drop downlist. I have to get the data from the database. but i am not able to do it .the code which i have used for dynamic dropdownlist is
    Code:
    for(int i = 0; i <= count; i++)
           {
                DropDownList  t = new DropDownList();
                t.ID = "C" + i;
                           
                Literal l1 = new Literal();
                l1.Text = "<br/>";
                         PlaceHolder1.Controls.Add(t);
                         
                         PlaceHolder1.Controls.Add(l1);
               
                
           }
    Last edited by Curtis Rutland; Sep 17 '08, 01:21 PM. Reason: Added Code Tags - Please use the # button
  • cloud255
    Recognized Expert Contributor
    • Jun 2008
    • 427

    #2
    Are you trying to create a new dropdown box control for each record from your database? That is what your code is doing, by saying:

    Code:
    for(int i = 0; i <= count; i++)
    {
    DropDownList t = new DropDownList();
     .....
    you try to create as many dropDownLists as there are count (which i am assuming is your number of records), but this is not happening as you are just using the same variable, t, and thus re-initilizing it many times...

    if you want one control with all the records in the list, you need to move the decleration of the dropDownList outside of the for loop:

    Code:
    DropDownList t = new DropDownList();
    
    for(int i = 0; i <= count; i++)
    {
    and then add objects to the Items collection of the DropDownList.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Aside from the facts that Cloud has pointed out...the scope of your DropDownLists are incorrect.

      They will get added to the page but you will not be able to retrieve anything from these DropDownList when the page is posted back.

      See this article on how to use dynamic controls in asp.net for more information on what I'm talking about.

      -Frinny

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Please at least attempt to understand the concepts outlined by the dynamic controls article. I realize that it is written in VB.NET but these same concepts are applied in all ASP.NET applications... ...only the syntax differs.

        What confused you in the article? Just the VB code?

        -Frinny

        Comment

        Working...