With each request for a page from the server, a new instance of the Web page class is produced. This would generally indicate that with every new request, all of the data on the page would be lost.
In ASP.NET Web Forms, have several state management to preserve the value of a variable between multiple requests to the Web server.
This post will go through some of the state management, which includes the following:
- View State
- Session State
- Application State
1) View State
This state enables you to save a small part of data for your public variable, which can then be retrieved in multiple functions on the same page.
Public int count {
get { return (int)ViewState["count"]; }
set { ViewState["count"] = value; }
}
2) Session State
This state enables you to save public variable data, which can then be accessed across multiple different pages or requests for the same user.
//set
Session["count"] = 1;
//get
int value = (int)Session["count"];
3) Application State
This state allows you to save public variable data that will be available to all users and sessions of your application.it is an ideal place to store application configuration properties or shared data.
//set
Application["count"] = 1;
//get
(int)Application ["count"];