Archive for January 2017
ASP.NET State Management Techniques
State management Techniques
Every click on website will load some data in web page.Every time that data will come from server
To save state for every request we use state management techniques.
google login - inbox - sent items(it will remember u)
There are two types of state management techniques. They are client side state management techniques and server side state management techniques.
Client Side State Management Techniques
View State (Same page)
Example: page with lot of fields are filled and refreshed but data is not lost (Page)
Code Example:create asp web form
add html textbox, asp text box and button
you can see on button click html textbox data will be lost but not for asp textbox because it has inbuilt viewstate.
viewstate data is stored behind the page which can be seen by seeing view page source.
Querystring (Next page)
Passing data from one page to other through url.http://google.com?a=12
you selected an item on first page, that value is sent to next page through url.
Example:
protected void Button2_Click(object sender, EventArgs e)
{
//sending value in a text box to next page through url
Response.Redirect("Default2.aspx?a=" + TextBox2.Text);
}
http://localhost:50158/Default2.aspx?a=abc
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Request["a"]);
}
Cookies (Multiple pages)
Data is stored in system or browser.Example: remember pwd
Hidden field (same page)
a control that is not visible but can store some data.
Server Side State Management Techniques
Session (Multiple page)
It will have the lifetime of the user.From login to logout.
data is saved on the server.
Application State (Multiple page)
Stored a value which has lifetime of an application.data is stored on the server.
Example: pageview in a blog
To count page views of a page:
protected void Page_Load(object sender, EventArgs e)
{
if (Application["PageViews"] == null)
{ Application["PageViews"] = 0; }
else
{
Application["PageViews"] = ((int)Application["PageViews"]) + 1;
Response.Write(Application["PageViews"].ToString());
}
}