In my previous article,
State Management Technique In ASP.NET - Part One , we have learned the Server side state management technique with various types of sessions, which are Inproc session, state Server session, SQL Server session, and custom session, and its events such as session_start, session_end, and the application-related events like application_start , application_end and application_error.
In this article, I will focus on the client side state management technique.
State Management in ASP.NET
As we know that the applications are developed using ASP.NET, it depends on the protocol such as HTTP, a stateless protocol. If the user is trying to register a form by filling the user details and when the user clicks on the submit button, the information will be lost because ASP.NET supports stateless protocol.
For post back requests, the Web Application or the Webpage does not hold the previous user's information. In order to overcome the above problem, we have an alternative; i.e., State Management technique.
State Management
It is a process of storing the user's information, when the user submits the request to the Web Server. We have two types of State Management techniques, which are given below.
- Server Side State Management
- Client Side State Management
Server Side State Management
Please refer to my previous article State Management Technique In ASP.NET- Part One.
Client Side State Management
It is a process of storing the user details or the user information within the client machine or the client's Browser memory. Whenever we want to save state related information on the client machine, instead of hitting the Web Server every time, we will go for the client side State Management.
When the user requests a Web page, instead of hitting the Server every time, we can get it from the client machine. Execution in the client side State Management will be faster compared to the Server side state management because the data will be received from the client machine or the Browser memory.
There is less security in the client side state management as the data stores into the client machine or the Browser's memory. We cannot implement the client side state management technique for confidential data like banking applications, where user's bank information should be secure such as user login id, password or pin number etc.
We can implement the client side State Management technique in four different ways They are given below
- Cookies
- Query String
- Hidden Field
- View State
Cookies
Cookie is a variable, which will be created within the client machine to store the user's data or the user's information. Cookie variable and session variable is almost similar functionality and the only difference is cookie variable, which will be created within the client's machine and session variable will be created within the Web Server.
Cookie variable will be the same for all the Webpages for the particular user session. To implement cookies, we need to make HttpCookie class.
Cookie is used mainly to track which user has visited the particular Application.
There are two types of cookies, which are given below.
- Persistent cookie
Whenever we create a cookie variable, if we/programmer gives lifetime for a cookie variable with its expiration property, then it is said to be a Persistent cookie.
or
While creating a cookie variable, if the programmer sets the lifetime of a cookie variable by using expiration property, then it is said to be a Persistent cookie.
Even though the Browser is closed, it means that the current user session is closed, but Persistent cookie will be available for the lifetime which is assigned by the programmer.
- Non-Persistent cookie
Whenever we create a cookie variable, if the programmer did not give lifetime value for a cookie variable with its expiration property, then it is said to be a Non-Persistent cookie.
or
While creating a cookie variable, if the programmer did not set the lifetime of a cookie variable by using expiration property, then it is said to be a Non- Persistent cookie.
This Non-Persistent cookie is available till the Browser opens. Once the Browser is closed, the current user session will expire.
Webform1.aspx.cs
- class webform1 {
- Login_click() {
- HttpCookie cookie = new Httpcookie("user");
- cookie["username"] = TextUsername.Text;
- cookie["password"] = TextPassword.Text;
- cookie.Expires = DateTime.Now.AddHours(5);
- }
- Response.Cookies.Add(cookie);
- Response.Reditect("webform2.aspx");
- }
- Webform2.aspx.cs
- class webform2 {
- Page_Load() {
- Httpcookie cookie = Request.Cookies["user"];
- if (cookie != null) {
- LabelUsername.Text = Username is: "+cookie["
- username "];
- }
- }
- }
Query String
Whenever we want to share/forward the user data or user information from one Webpage to another Webpage, then we make use of query string. We can forward the data only for limited Webpages.
The main drawback of the query string is that the user information will be highlighted within the Browser's URL. Whenever the user enter's some data like its username or its password into the text box, then these values will be available within the URL, which is the main drawback of the query string.
For example
When the user enters some value in Google's text box, say, "Wikipedia", then the value entered by the user is available in the Browser's URL.
From the above figure, the entered value that is Wikipedia is highlighted in the Browser's URL.
Webform1.aspx.cs
- class Webform1 {
- Submit_click()
- {
- Response.Redirect("webform2.aspx ? EmployeeNo=" + TextEmployeeNo.Text + " EmployeeName+" = +TextEmployeeName.Text);
- }
- }
- Webform2.aspx.cs
- class webform2 {
- Page_Load() {
- LabelEmployeeNo.Text = EmployeeNo is: "+Request.Querystring["
- EmployeeNo "];
- LabelEmployeeName.Text = EmployeeName is: "+Request.Querystring["
- EmployeeName "];
- }
- }
Hidden Field
To implement the hidden field, we have to use Hidden field control. It is an invisible control, which will be visible in design time i.e. in Designer Windows of .ASPX, which will be invisible in run time. Whenever we want to access the data in some other place within a current Webpage, we can access the value from the hidden field control.
We can store the value into the hidden field control and whenever this value is required, we can access it from the hidden field control. The drawback is that the user data, which we stored in the hidden field will not be shared to other pages, which we can share only in current Webpage i.e. locally.
Webform1.aspx.cs
- class webform1 {
- Submit_Click() {
- HiddenField1.Value = TextUsername.Text;
- HiddenField2.Value = TextPassword.Text;
- }
- }
- Webform2.aspx.cs
- class Webform2 {
- Page_Load() {
- LabelEmployeeNo.Text = EmployeeNo is: "+HiddenField1.Value;
- LabelPassword.Text = Password is: "+HiddenField2.Value;
- }
- }
Here value of Username and password fields will be stored into the value property of hidden Field control.
View state
View state is similar to an object, which can store any type of data. View state is used with Post back requests to remember the controls on the Webpage. View state is used to store the data and as well as forwarding this data, whenever required on the Webpage.
Example
- viewState["Username"]=TextUsername.Text;
- viewState["EmailAddress"]=TextEmail.Text;
Please share your feedback in the comment box. Thanks and I hope this article helps you.