State Management
State management is defined as a process of maintaining state and page information over multiple page requests.
2 Types:-
Client side State Management
- View state
- Hidden fields
- Cookies.
- Query String
The ViewState property provides a dictionary object for retaining values between multiple requests for the same page. The ViewState indicates the status of the page when submitted to the server.The status is defined through a hidden field placed on each page.
The Hidden field stores a variable in its value property and is explicitly added to the page.
A Cookie is a small amount of data that is stored in the client machine or in the memory of a client browser session. It contains site specific information that the server sends to the client along with a page output. Cookies can be temporary or have specific expiration dates.
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie c = new HttpCookie("my cookie");
c.Value = "hello";
c.Expires = System.DateTime.Now.AddDays(7);
Response.Cookies.Add(c);
}
protected void Button1_Click(object sender, EventArgs e)
{
string s;
s=Request.Cookies["my cookie"].Value.ToString();
Response.Write(s);
}
Query string is the limited way to pass information to the web server while navigating from one page to another page. This information is passed in url of the request.
In Default.aspx
if (TextBox1.Text == "aa" && TextBox2.Text == "mm")
{
Response.Redirect("default2.aspx?name="+textbox1.text+"&pwd="+textbox2.text);
}
Give this code in Default2.aspx
protected void Page_Load(object sender, EventArgs e)
{
Response.write(Request.Querystring["name"].ToString());
Response.write(Request.Querystring["pwd"].ToString());
}
Sever side State Management
- Application
- Session
The Session state is scoped to the current browser session and the number of session states will be determined by the number of users using the application. The session can be different for each visit by the user.