We can add/delete rows to asp.net table component dynamically with preservation of input data.
AddNewRow.aspx Page:
function LinkButtonClick(lbID) {
var ind = lbID.split("Row_")[1];
ind = ind.split("Col_")[0];
document.getElementById("").value = ind;
document.getElementById("").click();
}
.lblmouse
{
font-size:70%;
color:Red;
cursor:pointer;
text-transform:uppercase;
}
AddNewRow.aspx.cs Page:
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class AddNewRow : System.Web.UI.Page
{
private int numOfRows = 1;
DataTable dtStates;
const int colsCount = 10;//You can changed the value of 3 based on you requirements
// Now iterate through the table and add your controls
protected void Page_Load(object sender, EventArgs e)
{
//Generate the Rows on Initial Load
if (!Page.IsPostBack)
{
GenerateTable(numOfRows);
}
}
protected void btnAddNew_Click(object sender, EventArgs e)
{
if (ViewState["RowsCount"] != null)
{
numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());
GenerateTable(numOfRows);
}
}
private void SetPreviousData(int rowsCount, int colsCount)
{
Table table = (Table)this.Page.FindControl("PlaceHolder1").FindControl("Table1"); // **** if (table != null) {
for (int i = 0; i < rowsCount; i++)
{
for (int j = 0; j < colsCount; j++)
{
if (j < colsCount - 2)
{
//Extracting the Dynamic Controls from the Table
TextBox tb = (TextBox)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);
//Use Request object for getting the previous data of the dynamic textbox
tb.Text = Request.Form["TextBoxRow_" + i + "Col_" + j];//***** }
}
else if (j == colsCount - 2)
{
//Extracting the Dynamic Controls from the Table
DropDownList ddl = (DropDownList)table.Rows[i].Cells[j].FindControl("DropDownListRow_" + i + "Col_" + j);
//Use Request object for getting the previous data of the dynamic textbox
ddl.Text = Request.Form["DropDownListRow_" + i + "Col_" + j];//***** }
}
else
{
//Extracting the Dynamic Controls from the Table
Label lb = (Label)table.Rows[i].Cells[j].FindControl("LinkButtonRow_" + i + "Col_" + j);
//Use Request object for getting the previous data of the dynamic textbox
//lb.Text = Request.Form["LinkButtonRow_" + i + "Col_" + j];//***** }
//lb.Click += new EventHandler(lb_Click);
}
}
}
}
private void GenerateTable(int rowsCount)
{
//Creat the Table and Add it to the Page
Table table = new Table();
table.ID = "Table1";
PlaceHolder1.Controls.Add(table);//****** //The number of Columns to be generated
for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
row.ID = "Row_" + i;
for (int j = 0; j < colsCount; j++)
{
if (j < colsCount - 2)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
// Set a unique ID for each TextBox added
tb.ID = "TextBoxRow_" + i + "Col_" + j;
// Add the control to the TableCell
cell.Controls.Add(tb);
// Add the TableCell to the TableRow
row.Cells.Add(cell);
}
else if (j == colsCount - 2)
{
TableCell cell = new TableCell();
DropDownList ddl = new DropDownList();
// Set a unique ID for each DropDownList added
dtStates = fetchStates();
ddl.DataSource = dtStates;
ddl.DataValueField = "StateID";
ddl.DataTextField = "StateName";
ddl.DataBind();
ddl.ID = "DropDownListRow_" + i + "Col_" + j;
// Add the control to the TableCell
cell.Controls.Add(ddl);
// Add the TableCell to the TableRow
row.Cells.Add(cell);
}
else
{
TableCell cell = new TableCell();
Label lb = new Label();
// Set a unique ID for each LinkButton added
lb.ID = "LinkButtonRow_" + i + "Col_" + j;
lb.Text = "delete";
lb.Attributes.Add("OnClick", "LinkButtonClick('" + lb.ClientID + "')");
lb.CssClass = "lblmouse";
// Add the control to the TableCell
cell.Controls.Add(lb);
// Add the TableCell to the TableRow
row.Cells.Add(cell);
}
}
// And finally, add the TableRow to the Table
table.Rows.Add(row);
}
//Set Previous Data on PostBacks
SetPreviousData(rowsCount, colsCount);
//Sore the current Rows Count in ViewState
rowsCount++;
ViewState["RowsCount"] = rowsCount;
}
protected void lb_Click(object sender, EventArgs e)
{
if (ViewState["RowsCount"] != null)
{
int deleteID = int.Parse(HiddenField1.Value);
numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());
GenerateTableOnDelete(numOfRows, deleteID);
}
}
protected void GenerateTableOnDelete(int rowsCount, int deleteID)
{
rowsCount--;
//Creat the Table and Add it to the Page
Table table = new Table();
table.ID = "Table1";
PlaceHolder1.Controls.Add(table);//****** //The number of Columns to be generated
const int colsCount = 10;//You can changed the value of 3 based on you requirements
// Now iterate through the table and add your controls
for (int i = 0, k = 0; i < rowsCount; i++)
{
if (i < deleteID)
k = i;
else
k = i - 1;
if (deleteID != i)
{
TableRow row = new TableRow();
row.ID = "Row_" + k;
for (int j = 0; j < colsCount; j++)
{
if (j < colsCount - 2)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
// Set a unique ID for each TextBox added
tb.ID = "TextBoxRow_" + k + "Col_" + j;
// Add the control to the TableCell
cell.Controls.Add(tb);
// Add the TableCell to the TableRow
row.Cells.Add(cell);
}
else if (j == colsCount - 2)
{
TableCell cell = new TableCell();
DropDownList ddl = new DropDownList();
// Set a unique ID for each DropDownList added
dtStates = fetchStates();
ddl.DataSource = dtStates;
ddl.DataValueField = "StateID";
ddl.DataTextField = "StateName";
ddl.DataBind();
ddl.ID = "DropDownListRow_" + k + "Col_" + j;
// Add the control to the TableCell
cell.Controls.Add(ddl);
// Add the TableCell to the TableRow
row.Cells.Add(cell);
}
else
{
TableCell cell = new TableCell();
Label lb = new Label();
// Set a unique ID for each LinkButton added
lb.ID = "LinkButtonRow_" + k + "Col_" + j;
lb.Text = "delete";
lb.Attributes.Add("OnClick", "LinkButtonClick('" + lb.ClientID + "')");
lb.CssClass = "lblmouse";
// Add the control to the TableCell
cell.Controls.Add(lb);
// Add the TableCell to the TableRow
row.Cells.Add(cell);
}
}
// And finally, add the TableRow to the Table
table.Rows.Add(row);
}
}
//Set Previous Data on PostBacks
SetPreviousDataOnDelete(rowsCount, colsCount, deleteID);
//Sore the current Rows Count in ViewState
ViewState["RowsCount"] = rowsCount;
}
private void SetPreviousDataOnDelete(int rowsCount, int colsCount, int deleteID)
{
Table table = (Table)this.Page.FindControl("PlaceHolder1").FindControl("Table1"); // **** if (table != null) {
for (int i = 0, k = 0; i < rowsCount; i++)
{
if (i < deleteID)
k = i;
else
k = i - 1;
if (deleteID != i)
{
for (int j = 0; j < colsCount; j++)
{
if (j < colsCount - 2)
{
//Extracting the Dynamic Controls from the Table
TextBox tb = (TextBox)table.Rows[k].Cells[j].FindControl("TextBoxRow_" + k + "Col_" + j);
//Use Request object for getting the previous data of the dynamic textbox
tb.Text = Request.Form["TextBoxRow_" + i + "Col_" + j];//***** }
}
else if (j == colsCount - 2)
{
//Extracting the Dynamic Controls from the Table
DropDownList ddl = (DropDownList)table.Rows[k].Cells[j].FindControl("DropDownListRow_" + k + "Col_" + j);
//Use Request object for getting the previous data of the dynamic textbox
ddl.Text = Request.Form["DropDownListRow_" + i + "Col_" + j];//***** }
}
else
{
//Extracting the Dynamic Controls from the Table
Label lb = (Label)table.Rows[k].Cells[j].FindControl("LinkButtonRow_" + k + "Col_" + j);
//Use Request object for getting the previous data of the dynamic textbox
//lb.Text = Request.Form["LinkButtonRow_" + i + "Col_" + j];//***** }
//lb.Click += new EventHandler(lb_Click);
}
}
}
}
}
protected void btnPost_Click(object sender, EventArgs e)
{
if (ViewState["RowsCount"] != null)
{
numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());
}
int rowsCount = numOfRows - 1;
GenerateTable(rowsCount);
Table table = (Table)this.Page.FindControl("PlaceHolder1").FindControl("Table1"); // **** if (table != null) {
for (int i = 0; i < rowsCount; i++)
{
for (int j = 0; j < colsCount; j++)
{
if (j < colsCount - 2)
{
//Extracting the Dynamic Controls from the Table
TextBox tb = (TextBox)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);
}
else if (j == colsCount - 2)
{
//Extracting the Dynamic Controls from the Table
DropDownList ddl = (DropDownList)table.Rows[i].Cells[j].FindControl("DropDownListRow_" + i + "Col_" + j);
}
else
{
//Extracting the Dynamic Controls from the Table
Label lb = (Label)table.Rows[i].Cells[j].FindControl("LinkButtonRow_" + i + "Col_" + j);
}
}
}
}
protected DataTable fetchStates()
{
DataTable dtSt = new DataTable();
dtSt.Columns.Add("StateID");
dtSt.Columns.Add("StateName");
DataRow drSt = dtSt.NewRow();
drSt[0] = 0;
drSt[1] = "Select";
dtSt.Rows.Add(drSt);
drSt = dtSt.NewRow();
drSt[0] = 1;
drSt[1] = "Delhi";
dtSt.Rows.Add(drSt);
drSt = dtSt.NewRow();
drSt[0] = 2;
drSt[1] = "Bhubaneswar";
dtSt.Rows.Add(drSt);
drSt = dtSt.NewRow();
drSt[0] = 3;
drSt[1] = "Cuttuck";
dtSt.Rows.Add(drSt);
drSt = dtSt.NewRow();
drSt[0] = 4;
drSt[1] = "Puri";
dtSt.Rows.Add(drSt);
return dtSt;
}
}