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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 886
  • Last Modified:

asp:table loses data

I have a asp:table that I put on my aspx page.  I then on page_load create some rows for it.  Everything works fine and the table displays, but I do other processing on the page where I need to postback, so when I hit a button to postback, the table is gone, even though I have enableviewstate set to true.  Is there a way to display the table so I don't have to recreate it on every postback?
0
macros14
Asked:
macros14
  • 2
1 Solution
 
strickddCommented:
Create a function that creates the table and then call it regardless of page postback

private void Page_Load(object sender, System.EventArgs e)
{
      FillTable(); //must be outside of !Page.IsPostback so it fires each time

      if( !Page.IsPostback)
      {
          //do whatever
      }
}


private void FillTable()
{
     //code to create table
}
0
 
aki4uCommented:
In your Page_Load Event write(first line in this sub):

C#:
if (Page.IsPostBack)
      return;

VB.NET:
If (Page.IsPostBack) Then Exit Sub
0
 
macros14Author Commented:
I don't want to create the table everytime on postback, it takes awhile to process, is there a reason why enableviewstate isn't working with the table?
0
 
strickddCommented:
If you dynamically create content in .Net you have to keep populating that information on postback or else it will default to whatever the control is set up to be. Another option you have it to set the table into a session object:

private void Page_Load(object sender, System.EventArgs e)
{
      if( !Page.IsPostback)
      {
          FillTable();
      }

     if(Page.IsPostback)
     {
         ShowTable();
     }
}


private void FillTable()
{
     Session["TableData"] = tableObj;
}

private void ShowTable()
{
     tableObj = (Table)Session["TableData"]; //This will cast the session as object type Table
}
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now