Click here to Skip to main content
15,886,049 members

how to add row to table with specific scheme

shajarian_lover asked:

Open original thread
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.
Tags: C#, ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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