Click here to Skip to main content
15,065,328 members
Please Sign up or sign in to vote.
1.80/5 (3 votes)
See more:
i have created textboxes dynamically in c#.

TextBox MyTextBox=new TextBox();
MyTextBox.ID = "tb" +""+ ViewState["num"];
MyTextBox.Width = 540;
MyTextBox.Height = 60;
MyTextBox.TextMode = TextBoxMode.MultiLine;

Prasad_Kulkarni 5-Sep-12 3:05am
..and your question is??
Masroor Ansari 25-Oct-13 6:20am
two textboxes at a time dynamically
kumar2233 5-Sep-12 3:08am
how to add dynamically create textbox controls in c#.
♥…ЯҠ…♥ 5-Sep-12 3:12am
Already you have mentioned that, then what you are seeking?
Expecting to create dynamic text boxes n number of times? or anything?
kumar2233 5-Sep-12 3:15am
yes. i want n no of times to create textboxes.
Dasaradhi_r 5-Sep-12 3:16am
Create them in a loop

You have already added control in code you mentioned:
Button button1=new Button();
button1.Text="dynamic button";
button1.Left=10; button1.Top=10;  //the button's location
this.Controls.Add(button1); //this is how you can add control

Please refer;
Code: Adding Controls at Run Time (Visual C#)[^]
Adding Controls to an ASP.NET form Dynamically[^]

ASP.NET Dynamic Controls[^]
How we Dynamically Create TextBoxes in ASP.NET and get the Text of that TextBoxes :-

using System;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!IsPostBack)
int addedBooks = 2;// Set Required Number of Dynamic TextBoxes
ViewState["addedBooks"] = addedBooks;
//if postback dynamic generated controls are invisible
//so we can bind in each post back


//Method for Creating Dynamic TextBoxes
protected void CreateDynamicTextBox()
int addedBooks =Convert.ToInt16(ViewState["addedBooks"]);
for (int i = 0; i < addedBooks; i++)
Literal lt1 = new Literal();
lt1.Text = "Book Name : " + (i + 1) + " ";
pnl1.Controls.Add(lt1);// "pnl1" is a id of Panel

TextBox MyTextBox = new TextBox();
//Assigning the textbox ID name
MyTextBox.ID = "tb" + i;
MyTextBox.Width = 100;
MyTextBox.Height = 15;
MyTextBox.Text = String.Empty;

Literal lt = new Literal();
lt.Text = "

//Find Text of Dynamic TextBoxes on Button Click
protected void btnSubmit_Click(object sender, EventArgs e)
int NoOfDynamicTextBoxes = Convert.ToInt32(ViewState["addedBooks"]);
String[] books = new String[NoOfDynamicTextBoxes];
for (int i = 0; i < NoOfDynamicTextBoxes; i++)
String txtId = "tb" + i;// Produce id of Dynamically created TextBoxes
TextBox txtUserName = pnl1.FindControl(txtId) as TextBox;
if (txtUserName != null)
books[i] = txtUserName.Text;
cooldiv4u 16-Dec-13 11:32am
Hi, i am having addnewtextbox button which will add new textboxes everytime i click on it. I dont want to have postback everytime hence i have used update panel. But i am not able to add textboxes in update panel
Code snippet to create a textbox control for n number of times,
//Declaring the variable with limit
int n=5;
for (int i=0;i<n;i++)
TextBox MyTextBox=new TextBox();
//Assigning the textbox ID name 
MyTextBox.ID = "tb" +""+ ViewState["num"] + i;
MyTextBox.Width = 540;
MyTextBox.Height = 60;
MyTextBox.TextMode = TextBoxMode.MultiLine;

I hope this could help you.

With regards
kumar2233 5-Sep-12 3:32am
ur idea is correct. in , all of controls are running on server side.

here no need to pass runat="server".

I'm getting error like this.

System.Web.HttpException: Control 'tb0' of type 'TextBox' must be placed inside a form tag with runat=server.
renatocunha 26-Oct-19 23:48pm

♥…ЯҠ…♥ 26-Oct-12 5:48am
I didnt get you... can u elaborate your question.
kumar2233 5-Sep-12 4:22am
Ya The problem is sovled. Thnx prabakar...
♥…ЯҠ…♥ 5-Sep-12 5:03am
Welcome pal.....
Roy Samrat 31-Mar-13 3:55am
How you solve the problem....???
♥…ЯҠ…♥ 9-Apr-13 0:51am
Tricky question.... ;-)
Roy Samrat 1-May-13 4:59am
thanks RK.... :)
TextBox txt = new TextBox();

    txt.ID = ID;

    txt.AutoPostBack = true;

    txt.TextChanged += new EventHandler(OnTextChanged);


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