What Is View State And How It Works In ASP.NET

View State is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique. View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back

ViewState holds the values of controls. It does not restore the value to control after page post back. ViewState can hold the value on single web page, if we go to other page using response.redirect then ViewState will be null.

ViewState syntax same as Session, Session is a server side object while ViewState is a client side object. Session can stores values across on multiple pages while ViewState stores values on single page.

 Now see example:  What Is View State And How It Works In ASP.NET

ViewState[“name“]=TextBox1.Text;
ViewState[“password“]=TextBox2.Text;

Step 2: Now retive viewstate value if viewstate in not null

if (ViewState[“name“] != null & ViewState[“password“] != null)
{
TextBox1.Text = ViewState[“name“].ToString();
TextBox2.Text = ViewState[“password“].ToString();
}

By using view State it can be solve the page postback problem and maintain the form or page data until the page are not close.

SEE MORE

 

Leave a Reply

Your email address will not be published. Required fields are marked *