Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4569084
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:10:05+00:00 2026-05-21T19:10:05+00:00

<asp:Button onclick=Some_event Text=Add TextBox ID=id1 runat=server /> //once clicked: <asp:TextBox ID=txt1 ……></asp:TextBox> //when clicked

  • 0
<asp:Button onclick="Some_event" Text="Add TextBox" ID="id1" runat="server" />
//once clicked:
<asp:TextBox ID="txt1" ......></asp:TextBox>
//when clicked again:
<asp:TextBox ID="txt1" ......></asp:TextBox>
<asp:TextBox ID="txt2" ......></asp:TextBox>
//and so on...

Is there a way to create dynamic controls which will persist even after the postback? In other words, when the user clicks on the button, a new textbox will be generated and when clicks again the first one will remain while a second one will be generated. How can I do this using asp.net ? I know that if I can create the controls in the page_init event then they will persist but I dont know if it possible to handle a button click before the page_init occurs, therefore there must be another way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T19:10:06+00:00Added an answer on May 21, 2026 at 7:10 pm

    Yes, this is possible. One way to do this using purely ASP.NET (which seems like what you’re asking for) would be to keep a count of the TextBox controls that you have added (storing that value in the ViewState) and recreate the TextBox controls in the Page_Load event. Of course, nowadays most people would probably use Javascript or jQuery to handle this task client side, but I put together a quick example to demonstrate how it works with postbacks:

    Front page:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DynamicControls.aspx.cs" Inherits="MyAspnetApp.DynamicControls" EnableViewState="true" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server"></head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="btnAddTextBox" runat="server" Text="Add" OnClick="btnAddTextBox_Click" />
            <asp:Button ID="btnWriteValues" runat="server" Text="Write" OnClick="btnWriteValues_Click" />
            <asp:PlaceHolder ID="phControls" runat="server" />
        </div>
        </form>
    </body>
    </html>
    

    Code behind:

    using System;
    using System.Web.UI.WebControls;
    
    namespace MyAspnetApp
    {
        public partial class DynamicControls : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //Recreate textbox controls
                if(Page.IsPostBack)
                {
                    for (var i = 0; i < TextBoxCount; i++)
                        AddTextBox(i);
                }
            }
    
            private int TextBoxCount
            {
                get 
                {
                    var count = ViewState["txtBoxCount"];
                    return (count == null) ? 0 : (int) count;
                }
                set { ViewState["txtBoxCount"] = value; }
            }
    
            private void AddTextBox(int index)
            {
                var txt = new TextBox {ID = string.Concat("txtDynamic", index)};
                txt.Style.Add("display", "block");
                phControls.Controls.Add(txt);
            }
    
            protected void btnAddTextBox_Click(object sender, EventArgs e)
            {
                AddTextBox(TextBoxCount);
                TextBoxCount++;
            }
    
            protected void btnWriteValues_Click(object sender, EventArgs e)
            {
                foreach(var control in phControls.Controls)
                {
                    var textBox = control as TextBox;
                    if (textBox == null) continue;
                    Response.Write(string.Concat(textBox.Text, "<br />"));
                }
            }
        }
    }
    

    Since you are recreating the controls on each postback, the values entered into the textboxes will be persisted across each postback. I added btnWriteValues_Click to quickly demonstrate how to read the values out of the textboxes.

    EDIT
    I updated the example to add a Panel containing a TextBox and a Remove Button. The trick here is that the Remove button does not delete the container Panel, it merely makes it not Visible. This is done so that all of the control IDs remain the same, so the data entered stays with each TextBox. If we were to remove the TextBox entirely, the data after the TextBox that was removed would shift down one TextBox on the next postback (just to explain this a little more clearly, if we have txt1, txt2 and txt3, and we remove txt2, on the next postback we’ll create two textboxes, txt1 and txt2, and the value that was in txt3 would be lost).

    public partial class DynamicControls : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
            {
                for (var i = 0; i < TextBoxCount; i++)
                    AddTextBox(i);
            }
        }
    
        protected void btnAddTextBox_Click(object sender, EventArgs e)
        {
            AddTextBox(TextBoxCount);
            TextBoxCount++;
        }
    
        protected void btnWriteValues_Click(object sender, EventArgs e)
        {
            foreach(var control in phControls.Controls)
            {
                var panel = control as Panel;
                if (panel == null || !panel.Visible) continue;
                foreach (var control2 in panel.Controls)
                {
                    var textBox = control2 as TextBox;
                    if (textBox == null) continue;
                    Response.Write(string.Concat(textBox.Text, "<br />"));
                }
            }
        }
    
        private int TextBoxCount
        {
            get 
            {
                var count = ViewState["txtBoxCount"];
                return (count == null) ? 0 : (int) count;
            }
            set { ViewState["txtBoxCount"] = value; }
        }
    
        private void AddTextBox(int index)
        {
            var panel = new Panel();
            panel.Controls.Add(new TextBox {ID = string.Concat("txtDynamic", index)});
            var btn = new Button { Text="Remove" };
            btn.Click += btnRemove_Click;
            panel.Controls.Add(btn);
            phControls.Controls.Add(panel);
        }
    
        private void btnRemove_Click(object sender, EventArgs e)
        {
            var btnRemove = sender as Button;
            if (btnRemove == null) return;
            btnRemove.Parent.Visible = false;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I would be able to write : <asp:TextBox id=Input runat=server/> <asp:Button onclick=<% Input.Text='my
I have a button with an onclick event <asp:Button ID=btn_Save runat=server Text=Save OnClick=btn_Save_Click/> I
I want to trigger on click of a button this: <asp:Button runat=server ID=submit Text=Submit
I have a asp:UpdatePanel with a asp:Button and a asp:TextBox : <asp:UpdatePanel ID=UpdatePanel1 runat=server>
How do I escape apostrophes / single quotes in ASP.net XML? <asp:Button ID=myButton runat=server
I have an asp button with no onclick event but only onclientclick. This call
What is the difference between using the OnClick attribute of an ASP.Net Button: <asp:Button
I have an ImageButton in a GridView. <asp:GridView ID=ItemGridView runat=server DataKeyNames=Id OnRowDataBound=ItemGridView_RowDataBound AllowPaging=True AllowSorting=True
I have export button, onclick event creates markup of some current asp.net page's controls
I have a simple ASP.Net login page. When the login button is clicked, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.