Dynamic adding rows into table problem

  • Thread starter Luis Esteban Valencia
  • Start date
L

Luis Esteban Valencia

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>";
}
}
}
}
 
Joined
Mar 27, 2009
Messages
1
Reaction score
0
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top