Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm adding a DataSet object to the viewstate in the Page_Load().

protected void Page_Load(object sender, EventArgs e)
{
    string path = "...";
    DataSet ds = XmlOperations.GetXmlFile(path);
    ViewState["DataSet"] = ds;
    ...
}


Then at some point I need to change the contents of the DataSet, thus I have to change the DataSet and re-add the DataSet to the ViewState.

protected void btnSave_Click(object sender, EventArgs e)
{   ...
    ViewState["DataSet"] = newDataSet;
    ...
}


When I try to get the new DataSet (after the page reloads), the DataSet object that is returning is the original DataSet and not the 'newDataSet'.

Can you change a ViewState object on postback? How?
I dont want to use Session.
Posted
Updated 24-Jun-10 4:34am
v2

1 solution

rudolphgatt wrote:
DataSet object to the viewstate

Not a good practise as DataSet's can be huge and storing them in ViewState might affect performance.

rudolphgatt wrote:
Can you change a ViewState object on postback

Yes

rudolphgatt wrote:
How?

What you have done in btnSave looks fine. ViewState will get updated as that line gets executed.

rudolphgatt wrote:
the DataSet object that is returning is the original DataSet and not the 'newDataSet'.

Looks like you missed using IsPostback property of the page.

Do something like:
C#
protected void Page_Load(object sender, EventArgs e)
{    
   string path = "...";    
   if(!IsPostback)
   {
      DataSet ds = XmlOperations.GetXmlFile(path);    
      ViewState["DataSet"] = ds;    
   }
   //...

}

Currently, the Viewstate is updated at every page load. You might just want that to happen for the first time when page loads.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900