Hi, i have an asp page that contains this div, this table by default contains two rows:
<div>
<table border="1" runat="server" id="tblInfo">
<tr>
<td>Row 1</td>
</tr>
<tr>
<td>Row 2</td>
</tr>
</table>
<asp:Button runat="server" ID="btnAdd" OnClick="AddNewBtnClick" Text="Add Row"/>
</div>
and this is the code behind of this page:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace OTED
{
public partial class WebForm1 : System.Web.UI.Page
{
public static int counter = 0;
protected void Page_Load(object sender, EventArgs e)
{
rebuildMyTable();
}
protected void rebuildMyTable()
{
if (this.ViewState["TableRowCount"] == null)
this.ViewState["TableRowCount"] = 0;
for (int i = 0; i < Int32.Parse(this.ViewState["TableRowCount"].ToString()); i++)
AddNewRow();
}
protected void AddNewBtnClick(object sender, EventArgs e)
{
AddNewRow();
if (this.ViewState["TableRowCount"] == null)
this.ViewState["TableRowCount"] = 1;
else
{
this.ViewState["TableRowCount"] = Int32.Parse((this.ViewState["TableRowCount"].ToString())) + 1;
}
}
protected void AddNewRow()
{
HtmlTableCell cell = new HtmlTableCell();
HtmlTableRow row = new HtmlTableRow();
TextBox txt = new TextBox();
txt.ID = counter.ToString();
cell.Controls.Add(txt);
row.Controls.Add(cell);
int abc = tblInfo.Rows.Count;
tblInfo.Controls.AddAt(abc, row);
counter = counter + 1;
}
}
}
finaly i'm want to do this, when user clicked on "btnAdd", add the one row per click to the table, but this code doesn't work properly, can every one help me?
i'm a ASP Beginner, TNX.