Dynamic adding rows into table problem

Discussion in 'ASP .Net' started by Luis Esteban Valencia, Jun 23, 2005.

  1. Hello

    I wrote a program with code behind in C# to add row into table
    dynamically and the program worked very well in .Net Framework 1.1. When I
    run this program in .Net Framework 2.0 beta version, the program is not
    working as in version 1.1. So what is the problem? Microsoft declared that
    the version 2.0 is fully backward support, but it is not. Is that the
    problem with 2.0 version?

    I find out the problem in version 2.0. After adding rows into table, I
    saved the table in to Session variable Session["main_table"]. When the page
    posts back, I retrieved the table from
    main_table = (Table) Session["main_table"]. In version 1.1 it worked well,
    but in version 2.0, I could save the table after dynamically adding rows,
    but when I retrieved the table on post back, I got and empty table with no
    rows in it. Please help, any resource link is appreciated. Below is the

    Sorry for the long code.

    <%@ Page Language="C#" Inherits="WebControlTest"%>

    <title> Test </title>

    <form id="test_form" runat="server">
    <asp:placeHolder runat="server" id="holdtable" />
    <asp:Button id="add_button" text="Add Row" OnClick="CreateRow"
    &nbsp;<asp:Button id="delete_button" text="Delete" OnClick="DeteleRow"
    &nbsp;<asp:Button id="submit_button" text="Submit" OnClick="Submit_Click"
    <br><asp:Label id="label" runat="server"/>


    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class WebControlTest : System.Web.UI.Page
    Table main_table;
    int num_row = 0;

    protected Label label;
    protected PlaceHolder holdtable;
    protected Button delete_button;
    protected Button submit_button;
    TextBox text = new TextBox();

    protected override void OnInit(EventArgs e) {

    main_table = (Table)Session["table"];

    if(main_table == null){
    main_table = new Table();
    main_table.BorderWidth = 1;
    main_table.GridLines = (GridLines)3;

    protected void Page_Load(object sender, System.EventArgs e)
    label.Text = "";

    Response.Write("New Request<br>");
    Response.Write("Page Post Back<br>");

    protected void CreateRow(object sender, System.EventArgs e){

    num_row = (main_table.Rows).Count;

    TableRow r = new TableRow();
    TableCell c1 = new TableCell();
    TableCell c2 = new TableCell();
    TextBox t = new TextBox();

    t.ID = "textID" + num_row;
    t.EnableViewState = true;

    r.ID="newRow" + num_row;
    c1.ID="newC1" + num_row;
    c2.ID="newC2" + num_row;

    c1.Text = "New Cell - " + num_row;


    Session["table"] = main_table;

    protected void DeteleRow(object sender, System.EventArgs e)
    main_table = (Table)Session["table"];
    num_row = (main_table.Rows).Count;

    if(num_row > 0) {
    TableRow remove = main_table.Rows[num_row - 1];
    Session["table"] = main_table;

    protected void Submit_Click(object sender, System.EventArgs e){

    num_row = (main_table.Rows).Count;

    label.Text = "";

    for(int i = 0; i < num_row; i++){
    text = (TextBox)holdtable.FindControl("textID" + i);

    if(text != null){
    label.Text += "textID" + i + " = " + text.Text + "<br>";
    Luis Esteban Valencia, Jun 23, 2005
    1. Advertisements

  2. Luis Esteban Valencia


    Mar 27, 2009
    Likes Received:
    Dynamic Add Rows in Table in asp.net

    Dear all,

    It can be done through gridview in asp. Add Text box in footer template and do you insert there. Thank You.
    anan.meera, Mar 27, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.