Click here to Skip to main content
13,353,462 members (43,356 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi, i have an asp page that contains this div, this table by default contains two rows:
        <table border="1"  runat="server" id="tblInfo">
                <td>Row 1</td>
                <td>Row 2</td>
        <asp:Button runat="server" ID="btnAdd" OnClick="AddNewBtnClick" Text="Add Row"/>

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)

        protected void rebuildMyTable()
            if (this.ViewState["TableRowCount"] == null)
                this.ViewState["TableRowCount"] = 0;
            for (int i = 0; i < Int32.Parse(this.ViewState["TableRowCount"].ToString()); i++)
        protected void AddNewBtnClick(object sender, EventArgs e)
            if (this.ViewState["TableRowCount"] == null)
                this.ViewState["TableRowCount"] = 1;
                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();
            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.
Posted 8-Nov-12 12:48pm
Updated 14-Nov-12 2:21am
onenomi 12-Nov-12 5:05am
Please first answer the following so I can provide a solution:

Do you have any DataSet files set up?

How do you insert the data anywhere else in the app? or are you starting from scratch?

What does your table look like in which you want to insert the data?
digimanus 14-Nov-12 8:27am
He uses HtmlTable in stead of DataTable
shajarian_lover 14-Nov-12 21:57pm
dear onenomi,
i'm use HtmlTable!!!
onenomi 15-Nov-12 4:22am
I have copied your code to a test project and your code works as it is. Have you tried putting into a new solution/project?
shajarian_lover 15-Nov-12 5:34am
yes, it's work. but if you check the "id" of textboxes that added to table, you can see that the new row are replaced with old rows.
can i have your Yahoo ID?
onenomi 15-Nov-12 8:14am
So just simply set the TextBox id using the ViewState[TableRowCount] value instead of the counter. Also you counter is static which should be a property to get set the ViewState["TableRowCount"] variable.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

int abc = tblInfo.Rows.Count;
            tblInfo.Controls.AddAt(abc, row);


You need to add the row, because the controls are already added to the row so no need to add controls to the table.
shajarian_lover 14-Nov-12 21:59pm
i'm used tblinfo.Rows.Add(row);
but it doesn't work!!
digimanus 15-Nov-12 6:16am
what error?
shajarian_lover 15-Nov-12 6:23am
it's work, but when the user clicked on the Button, the new rows replace with the old rows, if you check the id of new rows, you can see this.
can i have your Yahoo ID?
digimanus 15-Nov-12 6:27am
First of all I don't have a Yahoo ID!
Can't you make a printscreen and that you link that printscreen in this message ?
shajarian_lover 15-Nov-12 6:30am
this link image that i'm uploaded it into mediafire
please give me your Email Address.
digimanus 15-Nov-12 7:52am
This forum is used as communication, not email.

What are the ID's in the other TR's ?
digimanus 15-Nov-12 8:03am
you use a static counter, maybe that is the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180111.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100