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
    program.


    Sorry for the long code.
    ============
    TableTest.aspx
    ============

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

    <html>
    <head>
    <title> Test </title>
    </head>
    <body>

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

    </form>
    </body>
    </html>

    ========================================
    WebControlTest.cs
    ========================================
    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 = "";
    holdtable.Controls.Add(main_table);

    if(!Page.IsPostBack){
    Response.Write("New Request<br>");
    }
    else{
    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;
    c2.Controls.Add(t);

    r.Cells.Add(c1);
    r.Cells.Add(c2);

    main_table.Rows.Add(r);
    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];
    main_table.Rows.Remove(remove);
    }
    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
    1. Advertising

  2. Luis Esteban Valencia

    anan.meera

    Joined:
    Mar 27, 2009
    Messages:
    1
    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
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lucky
    Replies:
    0
    Views:
    512
    lucky
    Jan 12, 2005
  2. Subba Rao via DotNetMonster.com

    script for moving rows up and down and traverse thru rows of HTML table

    Subba Rao via DotNetMonster.com, Mar 19, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    8,227
    Subba Rao via DotNetMonster.com
    Mar 19, 2005
  3. Rio
    Replies:
    4
    Views:
    1,188
  4. Replies:
    3
    Views:
    136
    Vincent van Beveren
    Jun 7, 2004
  5. Replies:
    2
    Views:
    126
Loading...

Share This Page