State management is a process by which you maintain state and page information over multiple requests for the same or different pages.
In asp.net For every page request a new instance of page is created and destroyed as we are using http stateless protocol to send request to the sever.
But we have to recognise wheather the new user is requesting the page or not.
For example:
A user cannot send his credentials every time to the server when he request his authorized pages, so if once he is logged in we have to maintain his login state untill he logged out.
For that asp.net technology offers State management techniques to maintain the state.
There are two types of State management techniques:
1. Client side
2. Server side.
In client side we have:
a. View state
b. Control state
c. Query string
d. Hidden field
e. Cookies
In server side we have:a. Session stateb. Application state a. View state:
View state stores the information of a page.
view state information is stored in hidden field and automatically returned to server after every postback.
It can store any type of data and it is of type object.
Data in view state is in encrypted format.
This view state data can be viewed by simply following the below steps.
right click on the browser
click on view source.
you can find an element of type hidden which stores view state.
the value in that is of encrypted format which can also be decrypted.
Example to check the view state:
Add a html textbox, asp textbox and a button to a .aspx page.
View the page in the browser.
enter some text in both the textboxes and click on the button.
after the page post back.
the data in the asp textbox remains same but the data is the html textbox is lost because asp controls state is maintained.
This view state is automatically managed by asp.net.
you need not do anything for this.
Example to store and display value using view state:View state is used to save data in client side. You can add values to the view state using Add method as shown below.Textbox1 text is added to the view state with a key "a" which is used to retrieve the value back. CSharp.NET Code:
protected void Button1_Click(object sender, EventArgs e)
{
ViewState.Add("a", TextBox1.Text);
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox2.Text = ViewState["a"].ToString();
}
Control stateControl state is a client side state management technique used to save the state of individual controls.Query string:
Query string is used to send data from one page to another page through url.
But the value is visible to the user in url.
Sending values to multiple pages is possible if you send it explicitly.
In the below example i am sending two values from the on page to other using query string state mangement technique.
CSharp.NET code in first page to send value:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("second.aspx?a=" + TextBox1.Text);
}
// Response.Redirect("second.aspx?b=" + TextBox2.Text + "&c=" + TextBox3.Text);
use the above code to send two strings at once using query string
Csharp.NET code to request value in second page and display on the webpageResponse.Write(Request["a"]);Cookies:
Cookies is a client side state mangement technique used to store data on client side.
It is used to store values which can be retreived on any webpage if cookie exists. Code to create a cookie:
HttpCookie c = new HttpCookie("msg");
c.Value = TextBox1.Text;
c.Expires = DateTime.Now.AddSeconds(30);
Response.Cookies.Add(c);
Response.Redirect("Default2.aspx");
CSharp.NET code to display data from cookie:
if (Request.Cookies["msg"] != null)
{
Label1.Text = Request.Cookies["msg"].Value;
}
cookie creation:
Response.Cookies["user1"].Value = TextBox2.Text;
Response.Cookies["user1"].Expires = DateTime.Now.AddHours(1);
CSharp.NET code to display all cookies information in grid view:
ArrayList a = new ArrayList();
for (int i = 0; i < Request.Cookies.Count; i++)
{
a.Add(Request.Cookies[i]);
}
GridView1.DataSource = a;
GridView1.DataBind();
CSharp.NET code to delete a cookie :
Response.Cookies[TextBox1.Text].Expires=DateTime.Now .AddDays (-1);
CSharp.NET code to delete all cookies:
string[] s = Request.Cookies.AllKeys;
foreach (string cookie in s)
{
Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
}
Hidden field:
It is a control that is not rendered on the page.
Any page information can be stored in it but only value.
we also have hidden field server control which acts same as html hidden field control.
It is used to store the view state value of a page.
Storing and retreiving value from hidden field:
HiddenField1.Value = "something";
Label1.Text = HiddenField1.Value;
Session state:
Session state is used to store values in the server side.
The session object is used to store the data specific to a user for the entire length of the user's visit.
Creating sessionSession["user"] = TextBox1.Text;Checking wheather session is available or notprotected void Page_Load(object sender, EventArgs e){ if (Session["user"] != null) { } else { Response.Redirect("session1.aspx"); } } Display value in a sessionResponse.Write(Session["user"].ToString());Clear session valueSession["user"] = null;Application stateApplication state is used to store values on the server side which have the life time of an application.Creating application state variables in Global.asax pagevoid Application_Start(object sender, EventArgs e){ // Code that runs on application startup Application["pageviews"] = 0; Application["onlineusers"] = 0; } Counting number of users online using Session_Start and Session_End methodsvoid Session_Start(object sender, EventArgs e){ // Code that runs when a new session is started Application["onlineusers"] = Convert.ToInt32(Application["onlineusers"]) + 1; } void Session_End(object sender, EventArgs e) { // Code that runs when a session ends Application["onlineusers"] = Convert.ToInt32(Application["onlineusers"]) - 1; } Counting pageviews on Page_Load. Displaying pageviews and onlineusers on a webpageprotected void Page_Load(object sender, EventArgs e){ Label1.Text = Application["onlineusers"].ToString(); Application.Lock(); Application["pageviews"] = Convert.ToInt32(Application["pageviews"]) + 1; Application.UnLock (); Label2.Text = Application["pageviews"].ToString(); } |
Thursday, 27 June 2013
ASP.NET State Management
ASP.NET State Management
- See more at: http://dbakings.com/ASP/StateManagement.aspx#sthash.qyhDSZUt.dpuf
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment