State Management
22:34Introduction
State Management is the very important part of Asp.Net. In this article I try to cover the whole part and its importants of State Management.What Is State & Stateless?
Whenever we visit any website using HTTP/HTTPS, our browser communicates with the respective server depending on our requested functionality. That time we get some resources i.e. Sessions ID, Objects, Allocated Memory, URL information etc this called stateIf after getting the response we closing our browser and again visit that website we can not track the previous resources. Because HTTP/HTTPS is a stateless protocol that means it can not store the previous resources/references.
Why State Management?
State Management can track and store the previous resources.Types Of State Management
There are two types of state management:A) Client Side State Management
B) Server Side State Management
A. Client Side State Management
In the Client-Side State Management, the state related information will directly get stored on the client-side.Client side state management techniques are:
i) View State
ii) Hidden field
iii) Cookies
iv) Control State
v) Query Strings
i) View State
It stores the page related data. During post back all the page related value go to the hidden field and after post back it comes to the proper place from that particular hidden field.When we use View State:
If we wants to maintain or store data temporarily after a post-back. In that case VIEW STATE is the most useful and preferred.
Store Value In View State:
ViewState["{Key}"] = {Value};
Example:
ViewState.aspx
<asp:Button ID="btnTestViewState" runat="server" Text="Button" OnClick="btnTestViewState_Click" /> <br /> <br /> <asp:Label ID="Label1" runat="server"></asp:Label>ViewState.aspx.cs
protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { if (ViewState["viewstatekey"] != null) { int ViewstateVal = Convert.ToInt32(ViewState["viewstatekey"]) + 1; Label1.Text = ViewstateVal.ToString(); ViewState["viewstatekey"] = ViewstateVal.ToString(); } else { ViewState["viewstatekey"] = "1"; } } } protected void btnTestViewState_Click(object sender, EventArgs e) { Label1.Text = "Update view state value: " + ViewState["viewstatekey"].ToString(); }Output:
Update view state value: 1
ii) Hidden field
Hidden field is invisible control and it used to store small amounts of data.When we use Hidden Field:
It store one value for the variable and it is a preferable way when a variable's value is changed frequently.
Example:
HiddenField.aspx
<asp:HiddenField ID="HiddenField1" runat="server" /> <asp:Button ID="btnHiddenField" runat="server" Text="Submit" OnClick="btnHiddenField_Click" /> <br /> <br /> <asp:Label ID="Label1" runat="server"></asp:Label>HiddenField.aspx.cs
protected void Page_Load(object sender, EventArgs e) { HiddenField1.Value = "1"; } protected void btnHiddenField_Click(object sender, EventArgs e) { if (HiddenField1.Value != null) { int val = Convert.ToInt32(HiddenField1.Value) + 1; HiddenField1.Value = val.ToString(); Label1.Text = "Hidden Foeld Value: " + val.ToString(); } }Output:
Hidden Foeld Value: 2
iii) Cookies
Cookie is a small text file which is created by the client's browser and also stored on the client hard disk by the browser. It does not use server memory. Generally a cookie is used to identify users.How it works:
A cookie is a small file that stores user information. Whenever a user makes a request for a page the first time, the server creates a cookie and sends it to the client along with the requested page and the client browser receives that cookie and stores it on the client machine either permanently or temporarily (persistent or non persistence). The next time the user makes a request for the same site, either the same or another page, the browser checks the existence of the cookie for that site in the folder. If the cookie exists it sends a request with the same cookie, else that request is treated as a new request.
Types of Cookies:
There are two types of cookies:
a) Persistence Cookie: Cookies having an expiration date is called a persistent cookie. This type of cookie reaches their end as their expiration dates comes to an end. In this cookie we can set an expiration date.
a) Non Persistence Cookie:
Example: (Persistance Coookie & Non Persistance Coookie)
Cookies.aspx
<p> Persistance Coookie: </p> <asp:Button ID="btnPersistenceCookie" runat="server" Text="Persistence Cookie" OnClick="btnPersistenceCookie_Click" /> <br /> <asp:Label ID="Label1" runat="server"></asp:Label> <br /><br /> <p> Non Persistance Coookie: </p> <asp:Button ID="btnNonPersistenceCookie" runat="server" Text="Non Persistence Cookie" OnClick="btnNonPersistenceCookie_Click" /> <br /> <asp:Label ID="Label2" runat="server"></asp:Label>Cookies.aspx.cs
HttpCookie pCook; HttpCookie nonpCook; protected void Page_Load(object sender, EventArgs e) { pCook = new HttpCookie("PersistanceName"); pCook.Value = "This is A Persistance Cookie"; pCook.Expires = DateTime.Now.AddSeconds(10); Response.Cookies.Add(pCook); nonpCook = new HttpCookie("NonPersistanceName"); nonpCook.Value = "This is A Non Persistance Cookie"; Response.Cookies.Add(nonpCook); } protected void btnPersistenceCookie_Click(object sender, EventArgs e) { if (Request.Cookies["PersistanceName"] != null) { Label1.Text = Request.Cookies["PersistanceName"].Value; } } protected void btnNonPersistenceCookie_Click(object sender, EventArgs e) { if (Request.Cookies["NonPersistanceName"] != null) { Label2.Text = Request.Cookies["NonPersistanceName"].Value; } }Output:
This is A Persistance Cookie
This is A Non Persistance Cookie
iv) Control State
Control State is another client side state management technique and it is separate from view state.When we use Control State:
Whenever we develop a custom control and want to preserve some information, we can use view state but suppose view state is disabled explicitly by the user, the control will not work. That time we need to use Control State property.
v) Query Strings
Query strings is used for holding some value from a different page and move these values to the different page. The information stored in it can be easily navigated to one page to another or to the same page as well.Example:
Button code of Cookies.aspx
protected void btnQueryStrings_Click(object sender, EventArgs e) { int val = 10; Response.Redirect("Cookies1.aspx?value=" + val); }After click button value go to the Cookies1.aspx page. We get query string value like below:
protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["value"] != null) { var val = Request.QueryString["value"].ToString(); Label1.Text = "Query String Value Is: " + val; } }Output:
Query String Value Is: 10
B. Server Side State Management
In Server-Side State Management all the information is stored in the user memory. So it is more secure then Client-side state Management.Server side state management techniques are:
i) Session State
ii) Application State
Session
Session management is a very strong technique to maintain state. Generally session is used to store user's information and/or uniquely identify a user or say browser. The server maintains the state of user information by using a session ID. When users makes a request without a session ID, ASP.NET creates a session ID and sends it with every request and response to the same user.Types Of Session Mode: There are 4 types of session Mode.
a) In Proc mode
b) State Server mode
c) SQL Server mode
d) Custom mode
Example:
Session.aspx
<asp:Button ID="btnSession" runat="server" OnClick="btnSession_Click" /> <br /> <br /> <asp:Label ID="Label1" runat="server"></asp:Label>Session.aspx.cs
protected void Page_Load(object sender, EventArgs e) { Session["value"] = Convert.ToInt32(Session["value"]) + 1; } protected void btnSession_Click(object sender, EventArgs e) { Label1.Text = Session["value"].ToString(); }Output:
Session Value Is: 2
Application State
Data stored in the application should be of small size. The date stored in application state value is common for all users of that particular ASP.NET application and can be accessed anywhere in the application. It is also called application level state management.when our application first run, we can set application value in Application_Start event of the Global.asax file.
Example:
We should write application value in Application_Start event of Global.asax file
protected void Application_Start(object sender, EventArgs e) { Application["Value"] = 5; }Output:
Application Value Is: 5
0 comments