Click here to Skip to main content
15,893,381 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;

this.Controls.Add(MyTextBox);
Posted
Comments
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.

You have already added control in code you mentioned:
C#
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[^]
 
Share this answer
 
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;
CreateDynamicTextBox();
}
else
{
//if postback dynamic generated controls are invisible
//so we can bind in each post back
CreateDynamicTextBox();
}

}

//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;
pnl1.Controls.Add(MyTextBox);

Literal lt = new Literal();
lt.Text = "
";
pnl1.Controls.Add(lt);
}
}

//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;
}
}
}
}
 
Share this answer
 
Comments
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;
this.Controls.Add(MyTextBox);
}


I hope this could help you.

With regards
R.K.
 
Share this answer
 
v3
Comments
kumar2233 5-Sep-12 3:32am    
ur idea is correct. in asp.net , 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    
Use

this.Form.Controls.Add(MyTextBox);
♥…ЯҠ…♥ 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.....
C#
TextBox txt = new TextBox();

    txt.ID = ID;

    txt.AutoPostBack = true;

    txt.TextChanged += new EventHandler(OnTextChanged);

    this.Form1.Controls.Add(txt);
 
Share this answer
 

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