Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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.
Posted 8-Nov-12 12:48pm
Edited 14-Nov-12 2:21am
v4
Comments
onenomi at 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 at 14-Nov-12 8:27am
   
He uses HtmlTable in stead of DataTable
shajarian_lover at 14-Nov-12 21:57pm
   
dear onenomi,
i'm use HtmlTable!!!
onenomi at 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 at 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 at 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
good
Please Sign up or sign in to vote.

Solution 1

Change
int abc = tblInfo.Rows.Count;
            tblInfo.Controls.AddAt(abc, row);
To
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.
  Permalink  
v2
Comments
shajarian_lover at 14-Nov-12 21:59pm
   
i'm used tblinfo.Rows.Add(row);
but it doesn't work!!
digimanus at 15-Nov-12 6:16am
   
what error?
shajarian_lover at 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 at 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 at 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.
digimanus at 15-Nov-12 7:52am
   
This forum is used as communication, not email.
 
What are the ID's in the other TR's ?
digimanus at 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
0 Sergey Alexandrovich Kryukov 9,578
1 OriginalGriff 8,420
2 Peter Leow 4,874
3 Kornfeld Eliyahu Peter 3,240
4 Maciej Los 2,356


Advertise | Privacy | Mobile
Web01 | 2.8.150327.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2015
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