What is viewstate in ASP.NET?

January 16, 2010 | Filed Under Asp.Net | Leave a Comment

Viewstate object is used to persist data of variables across postbacks. It even existed in classic ASP. In ASP.NET, a variable’s value is assigned to a viewstate object and then this is passed as a hidden variable and then may be retrieved by a page after a postback. See the example below…
//Save the value in ViewState object before the PostBack
ViewState(”SomeVar”) = txtFirstName.text;

//Retrieve the value from ViewState object after the PostBack
String strFirstName = ViewState(”SomeVar”).ToString();
Note that Viewstate object’s value is accessible only at page level. This means that if a viewstate is created at page1.aspx, then it may be used only within page1.aspx after the postback, and cannot be used by any other page

Comments

Leave a Reply





© PHPInterviewQuestion.com 2009 - 2010

eXTReMe Tracker