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

  • SEARCH
  • Home
  • 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

Consider this snippet of code... <form runat=server> <asp:TextBox runat=server ID=TextBoxOne /> <asp:Button runat=server ID=SubmitOne
I have the following button <asp:ImageButton ID=button1 runat=server ImageUrl=~/image.jpg CommandName = id CommandArgument =
I have the following control: <asp:TextBox ID=textbox1 runat=server Width=95px MaxLength=6 /> which i would
I have an asp:button with an onclick property that posts back to the server.
I'm trying to get a specific asp:button onclick event to fire when I press
What is the best way to determine which ASP.NET button was clicked on a
I have a button on an ASP.NET wep application form and when clicked goes
I have the following ASPX code: <asp:ScriptManager ID=ScriptManager1 runat=server /> <asp:UpdatePanel runat=server ID=UpdatePanel UpdateMode=Conditional>
I have a ASP.NET page with an asp:button that is not visible. I can't
What is the best way to keep an asp:button from displaying it's URL on

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.