Click here to Skip to main content
13,667,632 members
Rate this:
 
Please Sign up or sign in to vote.
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 4-Sep-12 21:03pm
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.
Dasaradhi_r 5-Sep-12 3:16am
   
Create them in a loop
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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;
}
}
}
}
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
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.
♥…ЯҠ…♥ 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.... :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

TextBox txt = new TextBox();

    txt.ID = ID;

    txt.AutoPostBack = true;

    txt.TextChanged += new EventHandler(OnTextChanged);

    this.Form1.Controls.Add(txt);
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180820.1 | Last Updated 10 Feb 2014
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100