I want to add row to table by user click, not when do that in code behind of asp
and then when click on “addRowButton”, only one row can be add to table. So, I search this problem in Google, and see some articles about “ViewState” in asp that causing this problem, but couldn’t understand that how can I use or change the “ViewState” to solve this problem.
Here is template of my Table in ASP Page:
<table runat="server" ID="myTable">
<tr><td>
<asp:textbox runat="server" ID="txt1"></asp:textbox>
</td></tr>
</table>
and i’m using this code to add some row with specific cells to Table:
protected void AddNewRow(object sender, EventArgs e)
{
HtmlTable tbl = (System.Web.UI.HtmlControls.HtmlTable)this.FindControl("myTable");
HtmlTableCell cell = new HtmlTableCell();
HtmlTableRow row = new HtmlTableRow();
TextBox txt = new TextBox();
txt.ID = "txtTest";
cell.Controls.Add(txt);
row.Controls.Add(cell);
int rowNumber = tbl.Rows.Count;
tbl.Controls.AddAt(rowNumber, row);
}
Please help
You need to store the current number of rows into the ViewState field (use the
ViewStateproperty of the current Page context). Or store the same information in a hidden field, then callAddNewRowas many times as ViewState says so during theOnInitevent of your page.Here’s some psuedocode: