Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have created a placeholder, button. on clicking the button dynamic textbox is generating. i want to show the first dynamic textbox should appear always. then when i click the button the next textbox should appear.

here is my code
<pre>  protected void LinkButton1_Click(object sender, EventArgs e)
    {
        CreateDynamicControls(true);
    }
  private void CreateDynamicControls(bool incrementRowCount = false)
    {
        int rowCount = 0;

        //initialize a session.
        rowCount = Convert.ToInt32(ViewState["clicks"]);

        if (incrementRowCount)
            rowCount++;

        ViewState["clicks"] = rowCount;

        //Create the textboxes and labels each time the button is clicked.
        for (int i = 0; i < rowCount; i++)
        {
            TextBox TxtBoxU = new TextBox();
            //TextBox TxtBoxE = new TextBox();

            Label lblU = new Label();
            //Label lblE = new Label();

            TxtBoxU.ID = "TextBoxU" + i.ToString();
            //TxtBoxE.ID = "TextBoxE" + i.ToString();
            TxtBoxU.Width=900;
            

            lblU.ID = "LabelU" + i.ToString();
            //lblE.ID = "LabelE" + i.ToString();

            lblU.Text = (i + 1).ToString() + " : ";
            //lblE.Text = "E-Mail : ";

            //Add the labels and textboxes to the Panel.
            PlaceHolder1.Controls.Add(lblU);
            PlaceHolder1.Controls.Add(TxtBoxU);
            PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));
            PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));
        }
    }
Kindly help


What I have tried:

</<pre><pre>  protected void LinkButton1_Click(object sender, EventArgs e)
    {
        CreateDynamicControls(true);
    }
  private void CreateDynamicControls(bool incrementRowCount = false)
    {
        int rowCount = 0;

        //initialize a session.
        rowCount = Convert.ToInt32(ViewState["clicks"]);

        if (incrementRowCount)
            rowCount++;

        ViewState["clicks"] = rowCount;

        //Create the textboxes and labels each time the button is clicked.
        for (int i = 0; i < rowCount; i++)
        {
            TextBox TxtBoxU = new TextBox();
            //TextBox TxtBoxE = new TextBox();

            Label lblU = new Label();
            //Label lblE = new Label();

            TxtBoxU.ID = "TextBoxU" + i.ToString();
            //TxtBoxE.ID = "TextBoxE" + i.ToString();
            TxtBoxU.Width=900;
            

            lblU.ID = "LabelU" + i.ToString();
            //lblE.ID = "LabelE" + i.ToString();

            lblU.Text = (i + 1).ToString() + " : ";
            //lblE.Text = "E-Mail : ";

            //Add the labels and textboxes to the Panel.
            PlaceHolder1.Controls.Add(lblU);
            PlaceHolder1.Controls.Add(TxtBoxU);
            PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));
            PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));
        }}
>
Posted
Updated 8-Feb-17 6:45am
Comments
Maciej Los 8-Feb-17 1:57am    
And what is wrong with your code?

1 solution

You need to put the code to create the first TextBox in the PageLoad event.

Also I think your CreateDynamicControls should only be creating one set of textbox + label at a time - you're going to try to recreate pre-existing controls as the code currently stands
 
Share this answer
 

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