creating dynamic drop down list(ASP .net)

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

    #1

    creating dynamic drop down list(ASP .net)

    I am tring to create n number of dropdown list.each have the same content. In this way i am able to create the dropdownlist but when the post back happens on the page the dropdownlist vanishes.
    so what should be done in this case. i don't now VB so if you could please explain in c# it will be very kind of u.
    [code=cpp]
    DropDownList[] t = new DropDownList[12];
    or (int i = 1; i <= n; i++)
    {

    t[i].ID = "C" + Convert.ToStrin g (i);

    t[i].DataTextField = "cropName";
    t[i].DataValueField = "cropId";
    t[1].Visible = true;

    t[i].DataSource = ds;
    t[i].DataBind();
    t[i].Items.Insert(0 , "--Select--");
    Literal l1 = new Literal();
    l1.Text = "<br/>";
    PlaceHolder1.Co ntrols.Add(t[i]);
    PlaceHolder1.Co ntrols.Add(l1);


    }[/code]
    Last edited by Frinavale; Sep 19 '08, 02:07 PM. Reason: added [code] tags
  • shweta123
    Recognized Expert Contributor
    • Nov 2006
    • 692

    #2
    Hi,

    Hi ,
    You have to check the isPostBack property of the page . You can create dropdownlistbox es and fill them if isPostBack property is false so that the dropdownlistbox es will not be created each time your page is posted back.
    e.g.

    Code:
    if(this.isPostBack == false)
      {
        //Now, your code code will be executed only once
    DropDownList[] t = new DropDownList[12];
    or (int i = 1; i <= n; i++)
    {
    
    t[i].ID = "C" + Convert.ToString (i);
    
    t[i].DataTextField = "cropName";
    t[i].DataValueField = "cropId";
    t[1].Visible = true;
    
    t[i].DataSource = ds;
    t[i].DataBind();
    t[i].Items.Insert(0, "--Select--");
    Literal l1 = new Literal();
    l1.Text = "<br/>";
    PlaceHolder1.Controls.Add(t[i]);
    PlaceHolder1.Controls.Add(l1);
    
    } 
    }

    Comment

    • smithak82
      New Member
      • Sep 2008
      • 12

      #3
      Originally posted by shweta123
      Hi,

      Hi ,
      You have to check the isPostBack property of the page . You can create dropdownlistbox es and fill them if isPostBack property is false so that the dropdownlistbox es will not be created each time your page is posted back.
      e.g.

      Code:
      if(this.isPostBack == false)
        {
          //Now, your code code will be executed only once
      DropDownList[] t = new DropDownList[12];
      or (int i = 1; i <= n; i++)
      {
      
      t[i].ID = "C" + Convert.ToString (i);
      
      t[i].DataTextField = "cropName";
      t[i].DataValueField = "cropId";
      t[1].Visible = true;
      
      t[i].DataSource = ds;
      t[i].DataBind();
      t[i].Items.Insert(0, "--Select--");
      Literal l1 = new Literal();
      l1.Text = "<br/>";
      PlaceHolder1.Controls.Add(t[i]);
      PlaceHolder1.Controls.Add(l1);
      
      } 
      }
      ---------------
      hi
      actually my code isattached to button it is not in page load.
      so if i make the post back false the dynamic dropdownlist will not be created.
      actually i have a text box and a button. in text box i have to enter the number of drop downlist required. once i enter the number in the text box and press the button. the dynamic dropdownlist get created. in the same page there are some other dependent dropdownlist. to the page get postback. when the page is postbacked the drop down list created dynamically vanishes.
      please help
      Last edited by Frinavale; Sep 19 '08, 02:09 PM. Reason: fixed [code] tags and [quote] tags

      Comment

      • PRR
        Recognized Expert Contributor
        • Dec 2007
        • 750

        #4
        When you post back your page the code in page load gets executed... As you have written code on button click that wont be visible .... You could do one thing.. use some static variable(bool) and call function depending on whether the button was clicked or not ... and if clicked call your function that puts dropdowns on your page...You will ahve to use 2 static variables.(int n bool)
        [QUOTE=smithak82]
        Originally posted by shweta123
        Hi,

        Hi ,
        You have to check the isPostBack property of the page . You can create dropdownlistbox es and fill them if isPostBack property is false so that the dropdownlistbox es will not be created each time your page is posted back.
        e.g.

        Code:
        if(this.isPostBack == false)
          {
            //Now, your code code will be executed only once
        DropDownList[] t = new DropDownList[12];
        or (int i = 1; i <= n; i++)
        {
        
        t[i].ID = "C" + Convert.ToString (i);
        
        t[i].DataTextField = "cropName";
        t[i].DataValueField = "cropId";
        t[1].Visible = true;
        
        t[i].DataSource = ds;
        t[i].DataBind();
        t[i].Items.Insert(0, "--Select--");
        Literal l1 = new Literal();
        l1.Text = "<br/>";
        PlaceHolder1.Controls.Add(t[i]);
        PlaceHolder1.Controls.Add(l1);
        
        } 
        }
        ---------------
        hi
        actually my code isattached to button it is not in page load.
        so if i make the post back false the dynamic dropdownlist will not be created.
        actually i have a text box and a button. in text box i have to enter the number of drop downlist required. once i enter the number in the text box and press the button. the dynamic dropdownlist get created. in the same page there are some other dependent dropdownlist. to the page get postback. when the page is postbacked the drop down list created dynamically vanishes.
        please help
        Last edited by Frinavale; Oct 1 '08, 02:12 PM. Reason: fixed [code] tag in quote

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          You have to keep in mind the Scope of your objects.

          If you are dynamically creating DropDownLists in a button click event, the scope of those DropDownLists is limited to that function.

          You need to declare your list of DropDownLists outside of that function so that their scope can exist for the whole page.

          If you do not do this, your DropDownLists will not existed upon postback (so they cannot be rendered again) and also any events raised by the DropDownLists (eg: autopostback) will not be handled.

          You should keep track of how many DropDownLists your page requires (in a HiddenField, Session, Cookie, ViewState...any way you wish) and you will need to initialize that number when the page first posts back.

          (Recall the ASP.NET Page Life Cycle -> once the page is sent to the browser every control on the server is destroyed)

          You may run into a problem because it is recommended that you initialize your dynamic controls in the Page Init event. The Page Init event occurs before the control ViewStates are loaded. The reason why it is recommended that you do this is because right after the Page Init event your control ViewStates are loaded....if you have not instantiated your controls before this, the ViewState cannot be loaded for the control and you will lose all of your Events and other control ViewState data.

          Please check out the article on how to use dynamic controls in asp.net. I realize that this article has examples written in VB but the concepts that you have to understand when using dynamic controls are important to grasp.

          -Frinny

          Comment

          • smithak82
            New Member
            • Sep 2008
            • 12

            #6
            thank you,
            It worked.
            I tried implementing it step by step and it worked.
            thanks a lot.
            it really solved my problem.

            Comment

            Working...