How To Create TextBox Control Dynamically at Runtime

http://aspdotnetcodebook.blogspot.com/2008/03/how-to-create-textbox-control.html in this post i will show how to create Textbox Control dynamically and read there value.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TextBoxDynamic.aspx.cs" Inherits="TextBoxDynamic" %>

<!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">
<title>Untitled Page</title>
<body>
<form id="form1" runat="server">
<div>
<tr>
<td colspan="2">
<asp:PlaceHolder ID="phTextBoxes" runat="Server"></asp:PlaceHolder>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class TextBoxDynamic : System.Web.UI.Page
{

string strValue = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{

}

private void CreateTextBoxes()
{

for (int counter = 0; counter <= NumberOfControls; counter++)
{
TextBox tb = new TextBox();
tb.Width = 150;
tb.Height = 18;
tb.TextMode = TextBoxMode.SingleLine;
tb.ID = "TextBoxID" + (counter + 1).ToString();
// add some dummy data to textboxes
tb.Text = "Enter Title " + counter;

}

}
protected override void CreateChildControls()
{
// Here we are recreating controls to persist the ViewState on every post back
if (Page.IsPostBack)
{
NumberOfControls += 1;
CreateTextBoxes();
}
else
{
CreateTextBoxes();
// Increase the control value to 1
NumberOfControls = 0;
}

}
protected void btnAddTitle_Click(object sender, EventArgs e)
{

NumberOfControls += 1;

}
public int NumberOfControls
{
get
{
if (ViewState["Count"] == null)
{
return 0;
}
return (int)ViewState["Count"];
}
set
{
ViewState["Count"] = value++;
}
}

{
strValue = string.Empty;
int n = NumberOfControls;

for (int i = 0; i <= NumberOfControls; i++)
{

string boxName = "TextBoxID" + (i + 1).ToString();
TextBox tb = phTextBoxes.FindControl(boxName) as TextBox;
strValue += tb.Text + "\n";

}
Response.Write(strValue);

}

protected void btnRead_Click(object sender, EventArgs e)
{

}
}

Reactions

1. very good code

2. very good code

3. I am a .net begiiner. Your code is working but one more text box is also created when i click on Read button. Any idea to fix it. Thanks

4. I am a .net begiiner. Your code is working but one more text box is also created when i click on Read button. Any idea to fix it. Thanks

5. Please disregard my previous comment regarding a fix to Read button. I figure out how to fix it. Actually check to tell the source of button click. Don't increase NumberOfControls if button click is from Read. thanks

6. I am a .net begiiner. Your code is working but one more text box is also created when i click on Read button. Any idea to fix it.Please tell me

7. Hello Sir, I'm a new user of .Net. I need ur help to create dynamic textboxes by getting the count of the selected item from the dropdownlist using query n display only those many text boxes. but the text box must display only after selecting the item from dropdownlist. Plz help me in this... i'll be waiting for ur reply... thank you.

8. Happy to have found the two articles on dynamic textboxes; I've been struggling with it for days.

Having a small problem because mye table is also being created dynamically, and can't find the right syntax to add the placeholder to the dynamic table.

If anyone has a suggestion, I'd appreciate it.

9. This code is working well but one more textbox is added when i am clicking read button. I think your solution is for clicking any button in a form. any idea to clear the problem rectify the problem that is extra textbox is add

10. hiiiii
i am jitendra but i want to say that the last value is appear bydefault when i click on read button

11. Hi ................
Till TextBox tb = phTextBoxes.FindControl(boxName) as TextBox;
i am getting texbox id but textbox value is showing null please anybody help me

12. I FRIENDS THIS IS KAREM,

I HAVE ONE DOUBT WHAT IS USE OF viewState IN ABOVE EXAMPLE CODE ,COULD ANY ONE OF YOU HELP ME TO CLARIFY THAT ONE
WE CREATING TEXT BOXES AND ACCESS THOSE VALUE IN SIDE THE TEXTBOXES

13. Nice Code... Can you please tell me why one more text box is created when i am clicking Add button

14. Am struggling with this for two daya.. Thanks it helped me a lot..

Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)