Click here to Skip to main content
15,905,875 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void Button1_Click(object sender, EventArgs e)
    {
      if (txtQuantity.Text != "")
        {
            if (Page.IsPostBack)
            {
                for (int i = 0; i <= Convert.ToInt16(txtQuantity.Text) / 3; i++)
                {


                    Label la = new Label();
                    la.Text = ddlProductName.SelectedItem.Value + (i + 1).ToString();
                    la.ID = "abc1" + i;

                    PlaceHolder1.Controls.Add(la);

                    //la = FindControl(String.Format(i)) as Label;




                    TextBox txtbox = new TextBox();
                    // txtbox.Text = "Textbox - " + i.ToString();


                    PlaceHolder1.Controls.Add(new LiteralControl("<input id='txt' name='Textbox" + i + "'type='text'  />"));

                    PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
                }
                for (int j = (Convert.ToInt16(txtQuantity.Text) / 3) + 1; j <= Convert.ToInt16(txtQuantity.Text) * 2 / 3; j++)
                {


                    Label la1 = new Label();
                    la1.Text = ddlProductName.SelectedItem.Value + (j + 1).ToString();
                    la1.ID = "abc2" + j;

                    PlaceHolder2.Controls.Add(la1);

                    //la = FindControl(String.Format(i)) as Label;




                    TextBox txtbox1 = new TextBox();
                    // txtbox.Text = "Textbox - " + i.ToString();
                    PlaceHolder2.Controls.Add(new LiteralControl("<input id='txt' name='Textbox" + j + "'type='text'  />"));
                    PlaceHolder2.Controls.Add(new LiteralControl("<br />"));
                }
                for (int k = (Convert.ToInt16(txtQuantity.Text) * 2 / 3) + 1; k < Convert.ToInt16(txtQuantity.Text); k++)
                {


                    Label la2 = new Label();
                    la2.Text = ddlProductName.SelectedItem.Value + (k + 1).ToString();
                    la2.ID = "abc3" + k;

                    PlaceHolder3.Controls.Add(la2);

                    //la = FindControl(String.Format(i)) as Label;




                    TextBox txtbox1 = new TextBox();
                    // txtbox.Text = "Textbox - " + i.ToString();


                    PlaceHolder3.Controls.Add(new LiteralControl("<input id='txt' name='Textbox" + k + "'type='text'  />"));

                    PlaceHolder3.Controls.Add(new LiteralControl("<br />"));
                }
            }

        }
    }
Posted
Updated 20-Jun-14 0:22am
v2
Comments
DamithSL 20-Jun-14 6:35am    
try by removing the if (Page.IsPostBack) condition, add dynamic controls even in post back.
Member 10578683 20-Jun-14 7:07am    
ya i tried but not working
DamithSL 20-Jun-14 7:09am    
what is the issue? explain what you mean by not working.
Member 10578683 20-Jun-14 7:20am    
I mean the textboes are generating but when i am clicking other buttons the textboxes disappearing.

1 solution

In Page Load method do the dynamic control loading, don't do it in the button click event and also don't you IsPostBack condition. in both PostBack and initial page load you can check the count given in textbox and load the controls.
 
Share this answer
 
Comments
Member 10578683 21-Jun-14 2:59am    
but in my code it is showing error and i need to generate the textboxes on button click

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900