|
-
July 11th, 2006, 02:11 PM
#1
Table and Session Variable
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.
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;
Page Load - lblMsg tells me that I have the right amount of rows but I don't see any of the rows.
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>";
}
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:
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.
-
July 11th, 2006, 02:30 PM
#2
Re: Table and Session Variable
you didn't update the table when postback.
-
July 11th, 2006, 02:37 PM
#3
Re: Table and Session Variable
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)
{
tblVariables = ((Table)Session["myTable"]);
}
 Originally Posted by jasonli
you didn't update the table when postback.
Can you be more specific? Doesn't the bolded code update my table? It doesn't appear to do it on PostBack but the elseif is a PostBack, I'm just making sure that it's not null. Do I need to loop through the rows and add them?
-
July 11th, 2006, 04:26 PM
#4
Re: Table and Session Variable
You are right. You have to repeat the code of loop through the rows and add them.
So, this is asp.net!
-
July 11th, 2006, 05:09 PM
#5
Re: Table and Session Variable
Something must not be right. When I click my button, it show 2 TableRows: default and the variable. When I click it again, it still only shows 2 TableRows: default and the variable. I feel like my my setting of the session variable is not working in my button click.
Here is my Page Load
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
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() + "<br>";
}
else
{
if (((Table)Session["myTable"]).Rows.Count > 0)
{
foreach (TableRow trow in ((Table)Session["myTable"]).Rows)
{
tblVariables.Rows.Add(trow);
}
lblMsg.Text = "Session Load: " +
((Table)Session["myTable"]).Rows.Count.ToString() + "<br>";
}
else
{
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 = "No Rows: " +
((Table)Session["myTable"]).Rows.Count.ToString() + "<br>";
}
}
}
Here is my button Click. lblMsg shows that after the button is clicked, I now have 2 rows in my session table.
Code:
protected void btnAdd_Click(object sender, EventArgs e)
{
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);
Session["myTable"] = tblVariables;
lblMsg.Text += "Added: " +
((Table)Session["myTable"]).Rows.Count.ToString();
}
-
July 12th, 2006, 11:03 AM
#6
Re: Table and Session Variable
any other comments on what I could be doing wrong?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|