We help IT Professionals succeed at work.

asp:table loses data

macros14
macros14 asked
on
Medium Priority
910 Views
Last Modified: 2008-02-01
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?
Comment
Watch Question

Commented:
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
}

Commented:
In your Page_Load Event write(first line in this sub):

C#:
if (Page.IsPostBack)
      return;

VB.NET:
If (Page.IsPostBack) Then Exit Sub

Author

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?
Commented:
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
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.