Fishdawg65
July 11th, 2006, 02:11 PM
I have a website for .net 2.0.
I have a table named tblVariables. It should start out with 1 row of cells: "Name", "Variable", "Action(s)". That's not a problem.
The problem is:
I have a button that when pressed should add a TableRow to my table. Obviously I want to keep all of the same rows that are already in the table and just add this row.
Here's the code to add a row to my table and this works.
TableRow tr = new TableRow();
TableCell tc = new TableCell();
TextBox tb = new TextBox();
tb.Text = "Name";
tc.Controls.Add(tb);
tr.Cells.Add(tc);
tc = new TableCell();
tb = new TextBox();
tb.Text = "Value";
tc.Controls.Add(tb);
tr.Cells.Add(tc);
tc = new TableCell();
Button btn = new Button();
btn.Text = "Delete";
btn.CommandName = "DeleteRow";
btn.CommandArgument = tblVariables.Rows.Count.ToString();
tc.Controls.Add(btn);
tr.Cells.Add(tc);
tblVariables.Rows.Add(tr);
//Notice that I set the session variable here.
Session["myTable"] = tblVariables;
Page Load - lblMsg tells me that I have the right amount of rows but I don't see any of the rows.
if (!Page.IsPostBack)
{
Session.Clear();
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Text = "Name";
tr.Cells.Add(tc);
tc = new TableCell();
tc.Text = "Value";
tr.Cells.Add(tc);
tc = new TableCell();
tc.Text = "Action(s)";
tr.Cells.Add(tc);
tblVariables.Rows.Add(tr);
Session["myTable"] = tblVariables;
lblMsg.Text = "Page Load: " + ((Table)Session["myTable"]).Rows.Count.ToString();
}
else if (Session["myTable"] != null)
{
lblMsg.Text = "Load Session: " +
((Table)Session["myTable"]).Rows.Count.ToString() + "<br>";
tblVariables = ((Table)Session["myTable"]);
lblMsg.Text += "Load tblVariables: " +
tblVariables.Rows.Count.ToString() + "<br>";
}
I've also tried to use this code in that "else if" section but this doesn't work either. lblMsg says that I have 2 rows in my Session Table and only 1 in my tblVariables.
lblMsg.Text = "Load Session: " +
((Table)Session["myTable"]).Rows.Count.ToString() + "<br>";
foreach (TableRow tr in ((Table)Session["myTable"]).Rows)
{
tblVariables.Rows.Add(tr);
}
lblMsg.Text += "Load tblVariables: " +
tblVariables.Rows.Count.ToString() + "<br>";
So how do I get this to work? I could do this within query string but I'd rather not. I was also thinking that I could do this with a DataSet and DataGrid but that seems like more work than it should be.
I have a table named tblVariables. It should start out with 1 row of cells: "Name", "Variable", "Action(s)". That's not a problem.
The problem is:
I have a button that when pressed should add a TableRow to my table. Obviously I want to keep all of the same rows that are already in the table and just add this row.
Here's the code to add a row to my table and this works.
TableRow tr = new TableRow();
TableCell tc = new TableCell();
TextBox tb = new TextBox();
tb.Text = "Name";
tc.Controls.Add(tb);
tr.Cells.Add(tc);
tc = new TableCell();
tb = new TextBox();
tb.Text = "Value";
tc.Controls.Add(tb);
tr.Cells.Add(tc);
tc = new TableCell();
Button btn = new Button();
btn.Text = "Delete";
btn.CommandName = "DeleteRow";
btn.CommandArgument = tblVariables.Rows.Count.ToString();
tc.Controls.Add(btn);
tr.Cells.Add(tc);
tblVariables.Rows.Add(tr);
//Notice that I set the session variable here.
Session["myTable"] = tblVariables;
Page Load - lblMsg tells me that I have the right amount of rows but I don't see any of the rows.
if (!Page.IsPostBack)
{
Session.Clear();
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Text = "Name";
tr.Cells.Add(tc);
tc = new TableCell();
tc.Text = "Value";
tr.Cells.Add(tc);
tc = new TableCell();
tc.Text = "Action(s)";
tr.Cells.Add(tc);
tblVariables.Rows.Add(tr);
Session["myTable"] = tblVariables;
lblMsg.Text = "Page Load: " + ((Table)Session["myTable"]).Rows.Count.ToString();
}
else if (Session["myTable"] != null)
{
lblMsg.Text = "Load Session: " +
((Table)Session["myTable"]).Rows.Count.ToString() + "<br>";
tblVariables = ((Table)Session["myTable"]);
lblMsg.Text += "Load tblVariables: " +
tblVariables.Rows.Count.ToString() + "<br>";
}
I've also tried to use this code in that "else if" section but this doesn't work either. lblMsg says that I have 2 rows in my Session Table and only 1 in my tblVariables.
lblMsg.Text = "Load Session: " +
((Table)Session["myTable"]).Rows.Count.ToString() + "<br>";
foreach (TableRow tr in ((Table)Session["myTable"]).Rows)
{
tblVariables.Rows.Add(tr);
}
lblMsg.Text += "Load tblVariables: " +
tblVariables.Rows.Count.ToString() + "<br>";
So how do I get this to work? I could do this within query string but I'd rather not. I was also thinking that I could do this with a DataSet and DataGrid but that seems like more work than it should be.