How do I add a textbox dynamically in ASP.NET?


I've a following requirement for my asp.net page:


User can add a textbox dynamically on a page A by clicking on link "Add a new category" hyperlink

</li> <li>

He clicks submit button on page A and gets redirected to page B.

</li> <li>

When he clicks on page A link from this page, the textboxes that he added should be persisted.

</li> </ol>

Can someone help me with the code on this?

Appreciate your help!


In the ButtonClick Method write.

TextBox tb = new TextBox(); Parent.Controls.Add( tb );

The Parent is the control you want to add the textbox to, for instance a panel.

You can also look at <a href="http://www.4guysfromrolla.com/articles/081402-1.aspx#postadlink" rel="nofollow">this resource</a>.

Hope it helps.


Adding a user control dynamically is simple. But in this case, I don't think you need to do that, instead you should look at creating a repeater with a textbox inside it, and when the user clicks Add Category, add one item to the repeater datasource.

This way you can handle both control creation and state persistence at the same time.


Dynamically creating Textboxes:

suppose you have page like this<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/8adEP.png" data-original="https://i.stack.imgur.com/8adEP.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

when you enter '1' in textbox and click on ADD button,output will be like display one textbox below.. i have designed like this, <img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/nVSIQ.png" data-original="https://i.stack.imgur.com/nVSIQ.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

i have one textbox and placeholder for displaying dynamic textboxes.. double click on Add button...in btnadd_click,you have to write the following code

protected void btnadd_Click(object sender, EventArgs e) {

int i; for (i = 0; i < Convert.ToInt32(txtno.Text); i++) { TextBox txtbox = new TextBox(); phtxt.Controls.Add(txtbox); phtxt.Controls.Add(new LiteralControl("<br>")); } }

debug it... and the output is, <img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/0k6gk.png" data-original="https://i.stack.imgur.com/0k6gk.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />


As others have stated adding the text box dynamically is fairly straight forward, just create the Textbox and add it to the controls collections wherever you need it to show up. You then need to store the information that this user gets this additional text box. Assuming that this is meant for long term, you will need to store this information in your backend store. Whenever you are constructing the page you will need to read the store information first to see what textboxes to create.

I would suggest doing it as follows. In the Onload event, if you have not done so before, load the dynamic information from your DB. Add any necessary controls to the page and store this information in viewstate. On any subsequent postbacks, read the information from viewstate to add the additional controls. This will save you from having to read constantly from the database on each postback.


dealing with dynamic user controls can be a pain in the ass.

as a rule of thumb i follow, whenever you create a dynamic user control, then you must set it's ID so ASP.net can reallocate it on post back, and to keep the controls values after post back you should reload your user controls on Page_Init event.

hope this helps.


  • How to update data in Redis and MySQL at the same time?
  • How to fix: The return type of an async method must be void, Task or Task [AppName]
  • How do I get NHibernate to save an entity if I assign it an ID, but generate one otherwise?
  • Applying custom annotation advice to spring data jpa repository
  • Load balanced Fiware Orion
  • How to add an object in my collection by only using add method? [closed]
  • Scrolling News Ticker Jquery - Issues
  • Using extern @class in order to add a category?
  • “A GKScore must specify a leaderboard.”
  • Create registry key in 32-bit hive on x64 PC using Installshield 2012 LE - Avoid redirection
  • Is is safe to use HSQLDB for production? (JBoss AS5.1)
  • Is there a way to disable a specific event in kendo ui scheduler?
  • How Get arguments value using inline assembly in C without Glibc?
  • How to make R's read_csv2() recognise the text characters properly
  • How do I include a SWC in an AS2 Flash project?
  • How to add a focus style to an editable ComboBox in WPF
  • Implementation of State Monad
  • How do I superscript characters in a UIButton?
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Word Open XML Mail Merge
  • How do I alternate colors in Flat List (React Native)
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • Django rest serializer Breaks when data exists
  • How to rebase a series of branches?
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Azure Cloud Service Web Role web pages do not load
  • Can Jackson SerializationFeature be overridden per field or class?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • what is the difference between the asp.net mvc application and asp.net web application
  • KeystoneJS: Relationships in Admin UI not updating
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Authorize attributes not working in MVC 4
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Busy indicator not showing up in wpf window [duplicate]
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?