State Management

In ASP.NET, the webserver do not have an idea about the request coming from new user or old user. The server is unable to identify that the user is returned user or a new user. To solve these problems we use state management.

State management is of the following two types:

  1. Client side state management
  2. Server side state management

Client side state management

Client side state management handled by:

Server side state management

Server side state management handled by:

View state

Example for view state

Creating view state

View state.add(“vikram”);
//reuse view state
String name=view state(“vikram”).tostring();


Hidden fields

Cookies

We have two types of cookies

  1. Short time cookies.
  2. Long time cookies.

// Storing value in cookie
HttpCookie cookie = new HttpCookie("NickName");
cookie.Value = “vikram";
Request.Cookies.Add(cookie);
// Retrieving value in cookie
lblNickName.Text = "Welcome" + Request.Cookies["NickName"].ToString();

Query string

Example for query string

http://localhost:49262/welcome.aspx?Name=vikram&LastName=12345

Page 1

  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. Response.Redirect("welcome.aspx?Name=" + this.TextBox1.Text + "&LastName=" + this.TextBox2.Text);
  4. }
Page 2
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. string s = Request.QueryString["name"];
  4. Label1.Text = “welcome“ + s;
  5. }
Application state

Application state details will clear when the process is restarted.

//storing information into application state
Application["Name"] = “vikram";
//retrieving details from application state
Label.text=Application[“name”].tostring( );


Session state

//Storing informaton in session state
Session[“name"] = “vikram";
//Retrieving information from session state
string str = Session["NickName"];
Label1.text=str;