Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, i have an asp page that contains this div, this table by default contains two rows:
ASP.NET
<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:

C#
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.
Posted
Updated 14-Nov-12 1:21am
v4
Comments
njammy 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?
Herman<T>.Instance 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!!!
njammy 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?

1 solution

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

To
C#
tblInfo.Rows.Add(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.
 
Share this answer
 
v2
Comments
shajarian_lover 14-Nov-12 21:59pm    
i'm used tblinfo.Rows.Add(row);
but it doesn't work!!
Herman<T>.Instance 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?
Herman<T>.Instance 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
http://www.mediafire.com/view/?vbrzgrtchd1za27
please give me your Email Address.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900