You are on page 1of 17

HTTP ( Hyper Text Transfer Protocol) is a stateless

protocol. When the client disconnects from the server, the


ASP.Net engine discards the page objects. This way each
web application can scale up to serve numerous requests
simultaneously without running out of server memory.

However, there need to be some technique to store the


information between requests and to retrieve it when required.
This information i.e., the current value of all the controls and
variables for the current user in the current session is called
the State.
State management refers to the management of the state of
one or more user interface controls such as text fields, OK
buttons, radio buttons, etc. in a graphical user interface. In
this user interface programming technique, the state of one UI
control depends on thestate of other UI controls.
ASP.Net manages four types of state:

 View State
 Control State
 Session State
 Application State

State Management | Types


In ASP.NET, there are 2 state management methodologies. These
are:
Client Side State Management
Whenever we do use client side state management, the
state related information will directly get stored on the
client side. That particular information will travel back
and communicate with every request generated by the
user then afterwards provides responses after server side
communication.

This architecture is something like that:

Server Side State Management


Server side state management is different from client
side state management but the operations and
working is somewhat same in functionality. In server
side state management, all the information is stored
in the user memory. Due to this functionality, there
are more secure domains at server side in comparison
to client side state management.

The structure is something like that:

State Management | Scenario


It will be a little difficult to directly evaluate what will be better for
our application. We cannot directly say that we are going to use
client side or server side architecture of state management.

State Management | Techniques


State management techniques are based on client side and server
side. Their functionality differs according to the change in state so,
here is the hierarchy:
Client Side | Techniques
Client side state management techniques are:

 View State
 Hidden field
 Cookies
 Control State
 Query Strings

Server Side | Technique


Server side state management techniques are:

 Session State
 Application State

Now, I am defining each and every technique in detail with their


reference example.
View State

It is used for storing user data in ASP.NET. Sometimes in


ASP.NET applications, users want to maintain or store their
data temporarily after a post back. In this case, VIEW STATE
is the most used and preferred way of doing this
mechanism.

This property is enabled by default, but we can make


changes according to our functionality, what we need to do
is just change EnableViewState value to either TRUE for
enabling it or FALSE for opposite operation.
[View State Management]

Snippet
' Page Load Event

Protected Sub Page_Load(sender As Object, e As EventArgs)

If IsPostBack Then
If ViewState("count") <> "" Then

Dim ViewstateVal As Integer =


Convert.ToInt32(ViewState("count")) + 1

View.Text = ViewstateVal.ToString()

ViewState("count") = ViewstateVal.ToString()

Else

ViewState("count") = "1"

End If

End If

End Sub

' Click Event

Protected Sub Submit(sender As Object, e As EventArgs)

View.Text = ViewState("count").ToString()

End Sub
Points to Remember

Some of the features of view state are:

 It is page level state management


 Used for holding data temporarily
 Can store any type of data
 Property dependent
Hidden Field

Hidden field is used for storing small amount of data on


client side. In most simple words, it’s just a container that
contains some objects but their result does not get rendered
on our web browser. It is invisible in the browser.

It stores one value for the single variable and it is the


preferable way when a variable’s value is hanged frequently
but we don’t need to keep track of that every time in our
application or web program.
[Hidden Field Management]

Snippet

 Collapse | Copy Code
// Hidden Field

int newVal = Convert.ToInt32(HiddenField1.Value) + 1;


HiddenField1.Value = newVal.ToString();
Label2.Text = HiddenField1.Value;

Points to Remember

Some features of hidden fields are:

 Contains small amount of memory


 Direct functionality access
Cookies

Cookie is a small text file that gets stored in users hard


drive using client’s browser. Cookies are just used for the
sake of user’s identity matching as it only stores
information such as sessions ids, some frequent
navigation or postback request objects.

Whenever we get connected to the internet for accessing


particular service, that cookie file gets accessed from our
hard drive via our browser for identifying user. The
cookie access depends upon the life cycle of expiration
of that particular cookie file.
[Cookie Management]

Snippet
Dim postbacks As Integer = 0

If Request.Cookies("number") <> „“ Then

postbacks =
Convert.ToInt32(Request.Cookies("number").Value) + 1

Else

' Generating Response

postbacks = 1

End If

Response.Cookies("number").Value = postbacks.ToString()

Result.Text = Response.Cookies("number").Value

Cookie | Types
Persistent Cookie

Cookie having expiration date is called persistent cookie.


These type of cookies reach their end as their expiration
dates comes to end. In this cookie, we set an expiration
date.

cookies are stored on your computer hard disk. They stay


on your hard disk and can be accessed by web servers
until they are deleted or have expired.
Snippet

Response.Cookies("UserName").Value = "Abhishek"

Response.Cookies("UserName").Expires =
DateTime.Now.AddDays(1)

Dim aCookie As New HttpCookie("Session")

aCookie.Value = DateTime.Now.ToString()

aCookie.Expires = DateTime.Now.AddDays(1)

Response.Cookies.Add(aCookie)
Non-Persistent Cookie

Non-persistent type of cookie doesn’t get stored in client’s hard


drive permanently. It maintains user information as long as user
accesses or uses the services. It's simply the opposite procedure of
persistent cookie.

cookies are saved only while your web browser is running. They can
be used by a web server only until you close your browser. They are
not saved on your disk.

Snippet

Dim aCookie As New HttpCookie("Session")


aCookie.Value = DateTime.Now.ToString()
Response.Cookies.Add(aCookie)
Points to Remember

Some features of cookie are:

 Store information temporarily


 It’s just a simple small sized text file
 Can be changed according to requirement
 User preferred
 Requires only few bytes or KBs of space for creating
cookies
Control State

Control state is based on custom control option. For expected


results from CONTROL STATE, we need to enable the property of
view state. As I already described, you can manually change those
settings.

Points to Remember

Some features of view state are:

 Used for enabling View State Property


 Defines custom view
 View State property declaration
 Can’t be modified
 Accessed directly or disabled
Query Strings

Query strings are used for some specific purpose. These in general
case are 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 from one page to another or to the same
page as well.

[Query Strings]

Snippet

' Getting data


If Request.QueryString("number") IsNot Nothing
Then
View.Text = Request.QueryString("number")
End If
' Setting query string
Dim postbacks As Integer = 0

If Request.QueryString("number") IsNot Nothing


Then
postbacks =
Convert.ToInt32(Request.QueryString("number")) + 1
Else
postbacks = 1
End If

Response.Redirect("default.aspx?number=" +
postbacks + "&number1=420")

Points to Remember

Some of the features are:

 It's generally used for holding values


 Works temporarily
 Switches information from one to another page
 Increased performance
 It uses real and virtual path values for URL routing

You might also like