[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to save a hole table to session and get back its values?

Posted on 2007-10-21
8
Medium Priority
?
2,105 Views
Last Modified: 2009-07-29
Hi,

I have an APS.NET table created dynamicaly.
After clicking an my refresh button the hole table gets lost. So i tried to save the table to session.
Here is my Code:

-- ASPX file --

     <asp:Table ID="Table1" runat="server"></asp:Table><br />
     <asp:Button runat="server" ID="button" Text="Restore" OnClick="CheckCB"/>
     <asp:Label runat="server" ID="info"></asp:Label>
 

-- CS file --

public partial class _Default : System.Web.UI.Page
{
 public TableRow row;
 public TableCell cell;
 public CheckBox checkbox;
 public string strLine = null;

 protected void Page_Load(object sender, EventArgs e)
 {
  if (!IsPostBack)
  {
   fillTable();
  }
  else
  {
   SaveTableToSession();
  }
 }


 private void fillTable()
 {
  for (int i = 0; i < 10; i++)
  {
   row = new TableRow();
   checkbox = new CheckBox();

   checkbox.ID = i.ToString();
   checkbox.Text = "CB" + i.ToString();
   cell = new TableCell();
   cell.Controls.Add(checkbox);
   row.Cells.Add(cell);
   cell.Dispose();
 
   cell = new TableCell();
   cell.Text = "Zeile " + i.ToString();
   cell.Dispose();

   row.Cells.Add(cell);
   Table1.Rows.Add(row);
  }
  Table1.GridLines = GridLines.Both;
  Table1.DataBind();
  SaveTableToSession();
 }

 private void SaveTableToSession()
 {
  Session["Table"] = Table1;
 }
 
private void GetTableFromSession()
 {
  Table1=(Session["Table"] as Table);
 }

 public void CheckCB(object sender, EventArgs e)
 {
  GetTableFromSession();
  strLine = null;
  for (int i=0;i<Table1.Rows.Count;i++)
  {
   foreach(Control co in Table1.Rows[i].Cells[0].Controls)
   {
    if (co.GetType() == typeof(CheckBox))
    {
     strLine += ((CheckBox)co).Checked.ToString()+"_";
    }
   }
  }
  info.Text = strLine;
 }
}


The questions in foreground are now:
What happens if I press a button generally in which order?
I mean for example which time the Table1 gets lost?
0
Comment
Question by:develc
  • 4
  • 2
  • 2
8 Comments
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 20117284
on the table makesure that ViewState is enabled so that you persist its information

Andrew
0
 
LVL 1

Author Comment

by:develc
ID: 20117439
I added the Line Table1.EnableViewState=true before Table1.DataBind() but nothing happend.
The Table1 saved in the session is empty.

The first thing after reloading a page is thr Page_Load Event.
At that time are the Variable values (here the Table1) already lost or are they lost if the method the button causes becomes loaded?
0
 
LVL 20

Accepted Solution

by:
REA_ANDREW earned 150 total points
ID: 20117489
What I would do is create a DataTable and assign this as a datasource for a gridView.  A DataTable is geared with Data Persitence in mind and is serializable.  A Table is for mere layout.  Also the ASP:Table is not marked as serializable, although this means you can store it in session state you cannot store in ViewState which is really the bext place for persisted page data.

Andrew
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Assisted Solution

by:iHadi
iHadi earned 300 total points
ID: 20117779
Hi develc

Add the datatable to a dataset and try to save the dataset
0
 
LVL 1

Author Comment

by:develc
ID: 20120963
OK,

now I created a DataSeet and put a Table in it.
Here is the Code:

-- ASPX file --
 <asp:DataGrid ID="DataGrid" runat="server"></asp:DataGrid>
 <asp:Button runat="server" ID="button1" Text="Refresh" OnClick="Refresh"/>

-- CS file --
TableRow row;
 DataTable dt;
 DataRow row2;

 CheckBox checkbox;
 DataSet ds;

 protected void Page_Load(object sender, EventArgs e)
 {
  if (!IsPostBack)
  {
   FillDataSet();
  }
  else
  {
   dt = new DataTable("DT1");
   ds = (Session["ds"] as DataSet);
   row2 = ds.Tables[0].NewRow();
   row2["Col2"] = "NewLine" + ds.Tables[0].Rows.Count.ToString();
   ds.Tables[0].Rows.Add(row2);
   DataGrid.DataSource = ds;
   DataGrid.DataBind();
   Session["ds"] = ds;
  }
 }

 public void Refresh(object sender, EventArgs e)
 {
 }

 private void FillDataSet()
 {
  ds = new DataSet("DS1");
  dt = new DataTable("DT1");

  checkbox = new CheckBox();
  checkbox.Text = "testbox";

  dt.Columns.Add("Col1", typeof(CheckBox));
  dt.Columns.Add("Col2", typeof(System.String));
  row2 = dt.NewRow();
  row2["Col1"] = checkbox;
  row2["Col2"] = "zeile";
  dt.Rows.Add(row2);
  ds.Tables.Add(dt);
  DataGrid.DataSource = ds;
  DataGrid.DataBind();
  Session["ds"] = ds;
 }


Following problem:
----------------------

The "Col1" is not shown. In that column I want to save CheckBoxes (as anyone can see).
How can I show that col?
0
 
LVL 1

Author Comment

by:develc
ID: 20124260
I really need help. Maybe the probleme of showing that Colum is easier than I think, but don't get it. I tried col.DataType=Syste,.Type.GetType(System.Boolean);
but the checkboxes aren't there anyway. Just a TRUE or FALSE is written in that column (I found that solution on another website).

Need help!
0
 
LVL 13

Assisted Solution

by:iHadi
iHadi earned 300 total points
ID: 20124895
To do so you need to edit the datagrid's template and change the column's type from text to checkbox.

You can also see the following urls:  
http://www.codeproject.com/aspnet/datagridcheckbox.asp
http://www.codersource.net/asp_net_gridviewcontrol.aspx <- Good tutorial for your needs
0
 
LVL 1

Author Comment

by:develc
ID: 20128795
Probleme solved! Thanks a lot!

Here the latest code:

-- APSX file --

 <asp:DataGrid ID="DataGrid" runat="server">
  <Columns>
   <asp:TemplateColumn>
    <ItemTemplate>
     <asp:CheckBox ID="checkbox" runat="server"/>
    </ItemTemplate>
   </asp:TemplateColumn>
  </Columns>
 </asp:DataGrid>
 <asp:Label ID="info" runat="server"></asp:Label>
 <asp:Button runat="server" ID="button1" Text="Refresh" OnClick="Refresh"/>

-- CS file --

 DataTable dt = new DataTable("DataTable");
 DataRow row;

 protected void Page_Load(object sender, EventArgs e)
 {
  if (!IsPostBack)
  {
   FillDataSet();
  }
 }


 public void Refresh(object sender, EventArgs e)
 {
  for (int i = 0; i < DataGrid.Items.Count; i++)
  {
   CheckBox cb = (CheckBox)DataGrid.Items[i].FindControl("checkbox");
   if (cb.Checked)
   {
    info.Text += "C:" + i.ToString();
   }
  }
 
 }

 private void FillDataSet()
 {
  DataColumn text = new DataColumn("Info");
  dt.Columns.Add(text);
  row = dt.NewRow();
  row[0] = "Infotext";
  dt.Rows.Add(row);
  row = dt.NewRow();
  row[0] = "Infotext2";
  dt.Rows.Add(row);
  DataGrid.DataSource = dt;
  DataGrid.DataBind();
 }
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
Suggested Courses
Course of the Month19 days, 20 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question