By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

Dynamic adding rows into table problem

P: n/a
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>";
}
}
}
}
Nov 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.