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.
Page Load - lblMsg tells me that I have the right amount of rows but I don't see any of the rows.Code: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;
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.Code: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>"; }
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.Code: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>";




Reply With Quote