Friday, March 16, 2012

persistence of variables in an aspx page...

If I have a code-behind page that loads a bunch of data into variables in the Page_Load method, will the variables keep their data for that particular instance of the page? For instance, if I have:

private string userid;
// ...
public void Page_Load(..)
{
userid = Request.QueryString["ID"];
}

and the user does some input on the web form and then submits the page, will the "userid" variable still contain the data I put in it during the Page_Load method? If so, does that work for all objects (like DataSets, etc.)? If not, will I have to put the "userid" data into a session variable instead?Request.QueryString["ID"] will persist across all postbacks, but the userID variable will not. You'll need to reload that variable each time, or put it in viewstate the first time.
I was just using the querystring as an example; I realize that particular value will persist, but that fact is negligable in the overall scope of my question. Regardless, you answered it. If I put such variables in ViewState, how long will they remain there? Will they be destroyed when the page is closed? Can I store larger objects in viewstate, such as DataSets? Thanks!
The viewstate remains for the life of the page, so, unstill it's navigated away from.

You could certainly put a dataset in there, but I wouldn't recommend it. Viewstate is stored as a hidden form field on the page, so, you'd be serializing kilobytes of additional data and sending it to the client as payload.
Ah, I completely forgot about that. You're absolutely right. Guess I'll have to find another way around it. Thanks!

0 comments:

Post a Comment