Problem with dynamically adding controls to panel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mepraseetha
    New Member
    • Jul 2008
    • 1

    Problem with dynamically adding controls to panel

    hai........
    i have an asp.net page.with four text box controls.I also have a link button .
    i want to add 4 text boxes each time i click on the link button.I write code for this but it didnt works well.Only the first time it creates 4 text boxes but when i click the link button again the text box controls are not displayed.

    here is my code
    Code:
    static count=0;
    protected void LinkButton1_Click(object sender, EventArgs e)
        {
                int q;
            
                   q = count + 1;
                    TextBox t1 = new TextBox();
                    t1.ID = "a" + q.ToString();
                    TextBox t2 = new TextBox();
                    t2.ID = "b" + q;
                    TextBox t3 = new TextBox();
                    t3.ID = "c" + q;
                    TextBox t4 = new TextBox();
                    t4.ID = "d" + q;
                    Panel1.Controls.Add(t1);
                    Panel2.Controls.Add(t2);
                    Panel3.Controls.Add(t3);
                    Panel4.Controls.Add(t4);
                   Panel1.Visible = true;
                   Panel2.Visible = true;
                   Panel3.Visible = true;
                   Panel4.Visible = true;
                  count = count + 1;
                  Response.Write(Panel1.Controls.Count);
                          
            }
    Plese give me a solution
    Last edited by Curtis Rutland; Jul 25 '08, 02:04 PM. Reason: Added Code Tags - Please use the # button
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    I'm not sure if that static value persists between postbacks. Try putting your count value in a hidden field.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Maybe, maybe not....

      I had a similar problem with creating controls on a form. I swore they weren't being created. Then I realized they really were being created, but without specifying their location they were all being created on top of each other, thus looking like one.

      If you are only going by what it *looks* like is happening remember: Looks can be deceiving. Perhaps while debugging have the creation loop exit into a message box telling you how many total controls were made.

      Then again, might be something entirely different.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by mepraseetha
        hai........

        Plese give me a solution
        We are here to help you find the solution to your problem, but will not outright give you the answer to your problem.

        Please check out the article on How to use dynamic controls in ASP.NET...it maybe of some use to you.

        -Moderator Frinny

        Comment

        Working...