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">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2">
<asp:PlaceHolder ID="phTextBoxes" runat="Server"></asp:PlaceHolder>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnAddTitle" runat="Server" Text="Add" />
</td>
<td>
<asp:Button ID="btnRead" runat="Server" Text="Read" OnClick="btnRead_Click" />
</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;
phTextBoxes.Controls.Add(tb);
phTextBoxes.Controls.Add(new LiteralControl("<br/>"));
}
}
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++;
}
}
private void ReadTextBoxes()
{
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)
{
ReadTextBoxes();
}
}









8 comments:
very good code
very good code
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
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
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
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
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.
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.
Post a Comment