Click here to Skip to main content
15,935,429 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi Everyone,

I have three buttons. i have written code to generate Textboxes dynamically by clicking three buttons.But the issue is when i am clicking one button the textbox is appearing but when i am clicking next button the previous button's Textboxes are disappearing. how will i resolve this issue. Kindly Help.
My code is
C#
protected void Button1_Click(object sender, EventArgs e)
    {
       if(!Page.IsCrossPagePostBack)
       {
        String Serials = TextBox4.Text;
        String[] SerialArray = Serials.Split(',');
        for (int i = 0; i < SerialArray.Length-1; i++)
        {
            Label la = new Label();
            la.Text = SerialArray[i];
            la.ID = "abc" + i.ToString();

            PlaceHolder1.Controls.Add(la);
     

            TextBox txtbox = new TextBox();

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

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (!Page.IsCrossPagePostBack)
        {
            String Serials = TextBox4.Text;
            String[] SerialArray = Serials.Split(',');
            for (int i = 0; i < SerialArray.Length - 1; i++)
            {
                Label la = new Label();
                la.Text = SerialArray[i];
                la.ID = "abc" + i.ToString();

                PlaceHolder2.Controls.Add(la);
           

                TextBox txtbox = new TextBox();

                PlaceHolder2.Controls.Add(new LiteralControl("<input id='txt' name='Textbox" + i + "'type='text'  />"));
                PlaceHolder2.Controls.Add(new LiteralControl("<br/>"));
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        if (!Page.IsCrossPagePostBack)
        {
            String Serials = TextBox4.Text;
            String[] SerialArray = Serials.Split(',');
            for (int i = 0; i < SerialArray.Length - 1; i++)
            {
                Label la = new Label();
                la.Text = SerialArray[i];
                la.ID = "abc" + i.ToString();

                PlaceHolder3.Controls.Add(la);
         

                TextBox txtbox = new TextBox();

                PlaceHolder3.Controls.Add(new LiteralControl("<input id='txt' name='Textbox" + i + "'type='text'  />"));
                PlaceHolder3.Controls.Add(new LiteralControl("<br/>"));
            }
        }
    }
Posted
Updated 3-Apr-14 22:23pm
v2
Comments
Siva Hyderabad 4-Apr-14 3:42am    
every control should be contains different ID's
change
la.ID = "abc1" , la.ID = "abc2" , la.ID = "abc3"
Siva Hyderabad 4-Apr-14 3:43am    
This line also
id='txt'

1.Each time you click a button, the page is sent to the web server, where you code is executed in the ASP.NET events order: Page_Init, Page_Load and then your ButtonX_Click event. Note that only one of this button event handler will be executed depend on what you clicked. Then the entire page will be recreated and send it back to the web browser in HTTP format and you browser will re-render it on your screen.

2.Your textboxes created before (on other button click) will not be created, because the code that create them will not be executed when you click on a second button.

3.You should change you logic and you have to cache the buttons that was clicked (for example by saving some flags or a int that using byte field with all 3 possible values into the Session) then to manage the creation of your old textboxes into the Page_Init event.
 
Share this answer
 
v2
Comments
Member 10578683 4-Apr-14 4:49am    
Can u give some link of example to understand this
Raul Iloc 4-Apr-14 6:38am    
Example:
http://www.aspsnippets.com/Articles/Creating-Dynamic-TextBox-Controls-in-ASP.Net.aspx

ASP.NET Page Life Cycle Events
http://www.codeproject.com/Tips/444310/ASP-NET-Page-Life-Cycle-Events
http://stackoverflow.com/questions/20677782/how-can-i-retrieve-data-from-textbox-when-its-dynamically-added-by-button


http://www.codeproject.com/Questions/453289/How-to-dynamically-create-textboxes-in-asp-net-and

try with these links
 
Share this answer
 
v2

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