State Management
State Management
By
SRIRAM. B
State Management Overview
State Management
Server Side Options
Application State
Session State
Profile Properties
Database Support
Client Side Options
View State
Control State
Hidden Field
Cookie
Query String
State Management
Describes how to store information between page requests.
Application state
Session state
Profile properties
Database support
Application State
ASP.NET allows you to save values using application state — which is an
instance of the HttpApplicationState class — for each active Web
application.
Store session-specific data on the server for use across multiple browser or
client-device requests within the same session.
Once you add your application-specific information to session state, the server
manages this object. Depending on which options you specify, session
information can be stored in cookies, on an out-of-process server, or on a
computer running Microsoft SQL Server
Session State
Session Identifier –> SessionID
Session Events –> Session_OnStart, Session_OnEnd
Session Methods –> Session.Abandon(), Session.TimeOut()
Advantages
Simple Implementation
Session Specific events
Data Persistance
Cookieless Support
Dis-Advantages
<sessionState mode="StateServer"
stateConnectionString="tcpip=SampleStateServer:42424"
cookieless="false" timeout="20"/>
ASP session state has no inherent solution to work with web farms.
ASP.NET session can be stored in State Server & SQL Server which can support
multiple server.
ASP session will functions only when the browser supports cookies.
ASP.NET session can be used with browser side cookies or independent of it.
Session State..
To create a session in the web form
Session[“id”] = TextBox1.Text;
For accesing the session variable in another web
form
Label1.Text = Session[“id”].ToString();
Profile Properties
ASP.NET provides a feature called profile properties, which allows you
to store user-specific data.
Data maintenance
Database Support
This is the default method that the page uses to preserve page
and control property values between round trips.
When the page is processed, the current state of the page and
controls is hashed into a string and saved in the page as a
hidden field, or multiple hidden fields if the amount of data
stored in the ViewState property exceeds the specified value in
the MaxPageStateFieldLength property. When the page is posted
back to the server, the page parses the view-state string at page
initialization and restores property information in the page.
View State..
Advantages
Dis-Advantages
Performance :- Page loading and Posting performance decreases
when large values are stored because the view state is stored in
the page.
Security Risks :- The view state is stored in one or more hidden
fields on the page. Although view state stores data in a hashed
format, it can still be tampered with. The information in the hidden
field can be seen if the page output source is viewed directly,
creating a potential security issue.
Control State
Sometimes you need to store control-state data in order for a control to work
properly.
For example, if you have written a custom control that has different tabs that
show different information, in order for that control to work as expected, the
control needs to know which tab is selected between round trips. The ViewState
property can be used for this purpose, but view state can be turned off at a page
level by developers, effectively breaking your control. To solve this, the ASP.NET
page framework exposes a feature in ASP.NET called control state.
A hidden field does not render visibly in the browser, but you
can set its properties just as you can with a standard control.
When a page is submitted to the server, the content of a hidden
field is sent in the HTTP form collection along with the values of
other controls.
It contains site-specific information that the server sends to the client along with
page output. Cookies can be temporary (with specific expiration times and dates)
or persistent.
You can use cookies to store information about a particular client, session, or
application. The cookies are saved on the client device, and when the browser
requests a page, the client sends the information in the cookie along with the
request information. The server can read the cookie and extract its value.
A typical use is to store a token (perhaps encrypted) indicating that the user has
already been authenticated in your application.
Cookie
Advantages
No server resources are requiered as they stored in client.
They are light weight and easy to use.
Dis-Advantages
To set a cookie
HttpCookie myCookie = new HttpCookie("myCookie1");
myCookie.Value=TextBox1.Text;
Response.Cookies.Add(myCookie);
Response.Write(myCookie.Name);
Response.Write(myCookie.Value.Length);
Response.Write(myCookie.Expires);
In the URL path above, the query string starts with a question
mark (?) and includes two attribute/value pairs, one called
"category" and the other called "price."