Design Page:
<asp:HiddenField ID="hdnCount" Value="1" runat="server" />
<asp:Table ID="tblMain" runat="server">
<asp:TableHeaderRow>
<asp:TableHeaderCell>
<asp:Button ID="btnAdd" runat="server"
Text="Add"
OnClick="btnAdd_Click"/>
<asp:Button ID="btnSave" runat="server"
Text="Save"
OnClick="btnSave_Click" />
</asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell>
<asp:TextBox ID="txtName1" runat="server"
Width="200">
</asp:TextBox>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
Code Page:-
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Linq;
using System.Drawing;
public partial class Default10 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateTextBoxes();
}
private void CreateTextBoxes()
{
int count = int.Parse(hdnCount.Value);
// Recreate the textboxes on each postback
for (int i = 2; i <= count; i++)
{
TextBox tb = new TextBox();
tb.ID = "txtName" + i.ToString();
tb.Width = 200;
tb.Text = Request.Form["txtName" + i.ToString()];
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Controls.Add(tb);
tr.Cells.Add(tc);
tblMain.Rows.Add(tr);
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
int count = int.Parse(hdnCount.Value);
count++;
hdnCount.Value = count.ToString();
TextBox tb = new TextBox();
tb.ID = "txtName" + count.ToString();
tb.Width = 200;
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Controls.Add(tb);
tr.Cells.Add(tc);
tblMain.Rows.Add(tr);
}
protected void btnSave_Click(object sender, EventArgs e)
{
int rowCount = int.Parse(hdnCount.Value);
for (int rowNumber=1; rowNumber<=rowCount; rowNumber++ )
{
TextBox tb = (TextBox)tblMain.Rows[rowNumber].Cells[0].FindControl
("txtName" + rowNumber.ToString());
if (tb != null)
Response.Write(tb.Text + "<br/>");
}
}
}
<asp:HiddenField ID="hdnCount" Value="1" runat="server" />
<asp:Table ID="tblMain" runat="server">
<asp:TableHeaderRow>
<asp:TableHeaderCell>
<asp:Button ID="btnAdd" runat="server"
Text="Add"
OnClick="btnAdd_Click"/>
<asp:Button ID="btnSave" runat="server"
Text="Save"
OnClick="btnSave_Click" />
</asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell>
<asp:TextBox ID="txtName1" runat="server"
Width="200">
</asp:TextBox>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
Code Page:-
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Linq;
using System.Drawing;
public partial class Default10 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateTextBoxes();
}
private void CreateTextBoxes()
{
int count = int.Parse(hdnCount.Value);
// Recreate the textboxes on each postback
for (int i = 2; i <= count; i++)
{
TextBox tb = new TextBox();
tb.ID = "txtName" + i.ToString();
tb.Width = 200;
tb.Text = Request.Form["txtName" + i.ToString()];
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Controls.Add(tb);
tr.Cells.Add(tc);
tblMain.Rows.Add(tr);
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
int count = int.Parse(hdnCount.Value);
count++;
hdnCount.Value = count.ToString();
TextBox tb = new TextBox();
tb.ID = "txtName" + count.ToString();
tb.Width = 200;
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Controls.Add(tb);
tr.Cells.Add(tc);
tblMain.Rows.Add(tr);
}
protected void btnSave_Click(object sender, EventArgs e)
{
int rowCount = int.Parse(hdnCount.Value);
for (int rowNumber=1; rowNumber<=rowCount; rowNumber++ )
{
TextBox tb = (TextBox)tblMain.Rows[rowNumber].Cells[0].FindControl
("txtName" + rowNumber.ToString());
if (tb != null)
Response.Write(tb.Text + "<br/>");
}
}
}