SlideShare a Scribd company logo
Managing States
Hyper Text Transfer Protocol (HTTP) 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 needs 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.
ASP.NET manages four types of states:
 View State
 Control State
 Session State
 Application State
View State
The view state is the state of the page and all its controls. It is automatically maintained across
posts by ASP.NET framework.
When a page is sent back to the client, the changes in the properties of the page and its controls
are determined, and stored in the value of a hidden input field named _VIEWSTATE. When the
page is again posted back, the _VIEWSTATE field is sent to the server with the HTTP request.
The view state could be enabled or disabled for:
 The entire application by setting the EnableViewState property in the <pages> section
of web.config file.
 A page by setting the EnableViewState attribute of the Page directive, as <%@ Page
Language="C#" EnableViewState="false" %>
 A control by setting the Control.EnableViewState property.
It is implemented using a view state object defined by the StateBag class which defines a
collection of view state items. The state bag is a data structure containing attribute-value pairs,
stored as strings associated with objects.
Example
The following example demonstrates the concept of storing view state. Let us keep a counter,
which is incremented each time the page is posted back by clicking a button on the page. A label
control shows the value in the counter.
The markup file code is as follows:
<%@ Page Language="C#"
AutoEventWireup="true"
CodeBehind="Default.aspx.cs"
Inherits="statedemo._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>View State demo</h3>
Page Counter:
<asp:Label ID="lblCounter" runat="server" />
<asp:Button ID="btnIncrement" runat="server"
Text="Add Count"
onclick="btnIncrement_Click" />
</div>
</form>
</body>
</html>
The code behind file for the example is shown here:
public partial class _Default : System.Web.UI.Page
{
public int counter
{
get
{
if (ViewState["pcounter"] != null)
{
return ((int)ViewState["pcounter"]);
}
else
{
return 0;
}
}
set
{
ViewState["pcounter"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
lblCounter.Text = counter.ToString();
counter++;
}
}
Control State
Control state cannot be modified, accessed directly, or disabled.
SessionState
When a user connects to an ASP.NET website, a new session object is created. When session
state is turned on, a new session state object is created for each new request. This session state
object becomes part of the context and it is available through the page.
Session state is generally used for storing application data such as inventory, supplier list,
customer record, or shopping cart. It can also keep information about the user and his
preferences, and keep the track of pending operations.
Sessions are identified and tracked with a 120-bit SessionID, which is passed from client to
server and back as cookie or a modified URL. The SessionID is globally unique and random.
The session state object is created from the HttpSessionState class, which defines a collection of
session state items.
The session state object is a name-value pair to store and retrieve some information from the
session state object. You could use the following code for the same:
void StoreSessionInfo()
{
String fromuser = TextBox1.Text;
Session["fromuser"] = fromuser;
}
void RetrieveSessionInfo()
{
String fromuser = Session["fromuser"];
Label1.Text = fromuser;
}
The above code stores only strings in the Session dictionary object, however, it can store all the
primitive data types and arrays composed of primitive data types, as well as the DataSet,
DataTable, HashTable, and Image objects, as well as any user-defined class that inherits from the
ISerializable object.
Example
The following example demonstrates the concept of storing session state. There are two buttons
on the page, a text box to enter string and a label to display the text stored from last session.
The mark up file code is as follows:
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
&nbsp; &nbsp; &nbsp;
<table style="width: 568px; height: 103px">
<tr>
<td style="width: 209px">
<asp:Label ID="lblstr" runat="server"
Text="Enter a String" style="width:94px">
</asp:Label>
</td>
<td style="width: 317px">
<asp:TextBox ID="txtstr" runat="server" style="width:227px">
</asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 209px"></td>
<td style="width: 317px"></td>
</tr>
<tr>
<td style="width: 209px">
<asp:Button ID="btnnrm" runat="server"
Text="No action button" style="width:128px" />
</td>
<td style="width: 317px">
<asp:Button ID="btnstr" runat="server"
OnClick="btnstr_Click" Text="Submit the String" />
</td>
</tr>
<tr>
<td style="width: 209px">
</td>
<td style="width: 317px">
</td>
</tr>
<tr>
<td style="width: 209px">
<asp:Label ID="lblsession" runat="server"
style="width:231px">
</asp:Label>
</td>
<td style="width: 317px">
</td>
</tr>
<tr>
<td style="width: 209px">
<asp:Label ID="lblshstr" runat="server">
</asp:Label>
</td>
<td style="width: 317px">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
The code behind file is given here:
public partial class _Default : System.Web.UI.Page
{
String mystr;
protected void Page_Load(object sender, EventArgs e)
{
this.lblshstr.Text = this.mystr;
this.lblsession.Text = (String)this.Session["str"];
}
protected void btnstr_Click(object sender, EventArgs e)
{
this.mystr = this.txtstr.Text;
this.Session["str"] = this.txtstr.Text;
this.lblshstr.Text = this.mystr;
this.lblsession.Text = (String)this.Session["str"];
}
}
Application State
TheASP.NET application is the collection of all web pages, code and other files within a single
virtual directory on a web server. When information is stored in application state, it is available
to all the users.
To provide for the use of application state, ASP.NET creates an application state object for each
application from the HTTPApplicationState class and stores this object in server memory. This
object is represented by class file global.asax.
Application State is mostly used to store hit counters and other statistical data, global application
data like tax rate, discount rate etc. and to keep the track of users visiting the site.

More Related Content

PPTX
JSON and XML
PPTX
Ch05 state management
PPTX
State management
PPT
State management
PPT
ASP.NET 12 - State Management
PPTX
Ajax and Jquery
PPTX
Java script Advance
JSON and XML
Ch05 state management
State management
State management
ASP.NET 12 - State Management
Ajax and Jquery
Java script Advance

What's hot (20)

DOCX
Asp.docx(.net --3 year) programming
PPTX
State management
PPTX
AdRotator and AdRepeater Control in Asp.Net for Msc CS
PPT
jQuery Ajax
PPTX
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
KEY
Summer - The HTML5 Library for Java and Scala
PPTX
Chapter 8 part1
DOCX
Microstrategy Intermediate Tables
PPTX
Web Technologies - forms and actions
PDF
ASP.net Image Slideshow
PPTX
State Management in ASP.NET
PPT
PDF
Training in Asp.net mvc3 platform-apextgi,noidaAspnetmvc3 j query
PPT
Csphtp1 20
PDF
PPTX
Javascript
PDF
Webdevelopment
PPTX
Training in Asp.net mvc3 platform-apextgi,noida
PDF
Introductionto asp net-ppt
PPTX
Presentation
Asp.docx(.net --3 year) programming
State management
AdRotator and AdRepeater Control in Asp.Net for Msc CS
jQuery Ajax
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Summer - The HTML5 Library for Java and Scala
Chapter 8 part1
Microstrategy Intermediate Tables
Web Technologies - forms and actions
ASP.net Image Slideshow
State Management in ASP.NET
Training in Asp.net mvc3 platform-apextgi,noidaAspnetmvc3 j query
Csphtp1 20
Javascript
Webdevelopment
Training in Asp.net mvc3 platform-apextgi,noida
Introductionto asp net-ppt
Presentation
Ad

Similar to Managing states (20)

PDF
Asp.net state management
PPTX
Managing state in asp.net
DOC
State management in asp
PPTX
State management
PPTX
C# cookieless session id and application state
PPT
StateManagement in ASP.Net.ppt
PPS
05 asp.net session07
DOCX
State management
PPSX
05 asp.net session07
PPTX
81.pptx ajx fyjc semester paper 2 parrtens
PPS
05 asp.net session07
PDF
State management 1
PPT
Session and state management
PPTX
State Management.pptx
PDF
state management asp.net
PPTX
ASP.NET Lecture 2
PPT
2310 b 14
PPT
Session viii(state mngtclient)
PPSX
ASP.Net Presentation Part3
PPT
State management in ASP.NET
Asp.net state management
Managing state in asp.net
State management in asp
State management
C# cookieless session id and application state
StateManagement in ASP.Net.ppt
05 asp.net session07
State management
05 asp.net session07
81.pptx ajx fyjc semester paper 2 parrtens
05 asp.net session07
State management 1
Session and state management
State Management.pptx
state management asp.net
ASP.NET Lecture 2
2310 b 14
Session viii(state mngtclient)
ASP.Net Presentation Part3
State management in ASP.NET
Ad

More from Paneliya Prince (20)

PPTX
140120107044 ins ala.ppt
DOCX
To create a web service
PPT
Master pages
DOCX
Master page
PPT
Introduction to ado.net
DOCX
Grid view control
PPTX
Asp.net validation
PPT
Asp.net control
DOC
Wt oep visiting card
DOCX
SE OEP online car service booking
PPTX
creating jdbc connection
PPTX
processing control input
PPT
static dictionary technique
DOCX
Ajava oep
DOC
Ajava oep shopping application
PPT
creating jdbc connection
PPTX
PPTX
static dictionary
PPT
ADO.net control
PDF
web technology
140120107044 ins ala.ppt
To create a web service
Master pages
Master page
Introduction to ado.net
Grid view control
Asp.net validation
Asp.net control
Wt oep visiting card
SE OEP online car service booking
creating jdbc connection
processing control input
static dictionary technique
Ajava oep
Ajava oep shopping application
creating jdbc connection
static dictionary
ADO.net control
web technology

Recently uploaded (20)

PDF
737-MAX_SRG.pdf student reference guides
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PPTX
UNIT - 3 Total quality Management .pptx
PPTX
Nature of X-rays, X- Ray Equipment, Fluoroscopy
PDF
Visual Aids for Exploratory Data Analysis.pdf
PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PDF
Integrating Fractal Dimension and Time Series Analysis for Optimized Hyperspe...
PPTX
communication and presentation skills 01
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPT
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
PDF
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PPT
Total quality management ppt for engineering students
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
737-MAX_SRG.pdf student reference guides
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Automation-in-Manufacturing-Chapter-Introduction.pdf
Exploratory_Data_Analysis_Fundamentals.pdf
R24 SURVEYING LAB MANUAL for civil enggi
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
UNIT - 3 Total quality Management .pptx
Nature of X-rays, X- Ray Equipment, Fluoroscopy
Visual Aids for Exploratory Data Analysis.pdf
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
Integrating Fractal Dimension and Time Series Analysis for Optimized Hyperspe...
communication and presentation skills 01
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Total quality management ppt for engineering students
Fundamentals of safety and accident prevention -final (1).pptx

Managing states

  • 1. Managing States Hyper Text Transfer Protocol (HTTP) 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 needs 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. ASP.NET manages four types of states:  View State  Control State  Session State  Application State View State The view state is the state of the page and all its controls. It is automatically maintained across posts by ASP.NET framework. When a page is sent back to the client, the changes in the properties of the page and its controls are determined, and stored in the value of a hidden input field named _VIEWSTATE. When the page is again posted back, the _VIEWSTATE field is sent to the server with the HTTP request. The view state could be enabled or disabled for:  The entire application by setting the EnableViewState property in the <pages> section of web.config file.  A page by setting the EnableViewState attribute of the Page directive, as <%@ Page Language="C#" EnableViewState="false" %>  A control by setting the Control.EnableViewState property. It is implemented using a view state object defined by the StateBag class which defines a collection of view state items. The state bag is a data structure containing attribute-value pairs, stored as strings associated with objects. Example The following example demonstrates the concept of storing view state. Let us keep a counter, which is incremented each time the page is posted back by clicking a button on the page. A label control shows the value in the counter. The markup file code is as follows: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="statedemo._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  • 2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <h3>View State demo</h3> Page Counter: <asp:Label ID="lblCounter" runat="server" /> <asp:Button ID="btnIncrement" runat="server" Text="Add Count" onclick="btnIncrement_Click" /> </div> </form> </body> </html> The code behind file for the example is shown here: public partial class _Default : System.Web.UI.Page { public int counter { get { if (ViewState["pcounter"] != null) { return ((int)ViewState["pcounter"]); } else { return 0; } } set { ViewState["pcounter"] = value; } } protected void Page_Load(object sender, EventArgs e) { lblCounter.Text = counter.ToString(); counter++;
  • 3. } } Control State Control state cannot be modified, accessed directly, or disabled. SessionState When a user connects to an ASP.NET website, a new session object is created. When session state is turned on, a new session state object is created for each new request. This session state object becomes part of the context and it is available through the page. Session state is generally used for storing application data such as inventory, supplier list, customer record, or shopping cart. It can also keep information about the user and his preferences, and keep the track of pending operations. Sessions are identified and tracked with a 120-bit SessionID, which is passed from client to server and back as cookie or a modified URL. The SessionID is globally unique and random. The session state object is created from the HttpSessionState class, which defines a collection of session state items. The session state object is a name-value pair to store and retrieve some information from the session state object. You could use the following code for the same: void StoreSessionInfo() { String fromuser = TextBox1.Text; Session["fromuser"] = fromuser; } void RetrieveSessionInfo() { String fromuser = Session["fromuser"]; Label1.Text = fromuser; } The above code stores only strings in the Session dictionary object, however, it can store all the primitive data types and arrays composed of primitive data types, as well as the DataSet, DataTable, HashTable, and Image objects, as well as any user-defined class that inherits from the ISerializable object. Example The following example demonstrates the concept of storing session state. There are two buttons on the page, a text box to enter string and a label to display the text stored from last session. The mark up file code is as follows: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
  • 4. Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> &nbsp; &nbsp; &nbsp; <table style="width: 568px; height: 103px"> <tr> <td style="width: 209px"> <asp:Label ID="lblstr" runat="server" Text="Enter a String" style="width:94px"> </asp:Label> </td> <td style="width: 317px"> <asp:TextBox ID="txtstr" runat="server" style="width:227px"> </asp:TextBox> </td> </tr> <tr> <td style="width: 209px"></td> <td style="width: 317px"></td> </tr> <tr> <td style="width: 209px"> <asp:Button ID="btnnrm" runat="server" Text="No action button" style="width:128px" /> </td> <td style="width: 317px"> <asp:Button ID="btnstr" runat="server" OnClick="btnstr_Click" Text="Submit the String" /> </td> </tr> <tr> <td style="width: 209px"> </td> <td style="width: 317px"> </td> </tr> <tr> <td style="width: 209px">
  • 5. <asp:Label ID="lblsession" runat="server" style="width:231px"> </asp:Label> </td> <td style="width: 317px"> </td> </tr> <tr> <td style="width: 209px"> <asp:Label ID="lblshstr" runat="server"> </asp:Label> </td> <td style="width: 317px"> </td> </tr> </table> </div> </form> </body> </html> The code behind file is given here: public partial class _Default : System.Web.UI.Page { String mystr; protected void Page_Load(object sender, EventArgs e) { this.lblshstr.Text = this.mystr; this.lblsession.Text = (String)this.Session["str"]; } protected void btnstr_Click(object sender, EventArgs e) { this.mystr = this.txtstr.Text; this.Session["str"] = this.txtstr.Text; this.lblshstr.Text = this.mystr; this.lblsession.Text = (String)this.Session["str"]; } } Application State TheASP.NET application is the collection of all web pages, code and other files within a single virtual directory on a web server. When information is stored in application state, it is available to all the users.
  • 6. To provide for the use of application state, ASP.NET creates an application state object for each application from the HTTPApplicationState class and stores this object in server memory. This object is represented by class file global.asax. Application State is mostly used to store hit counters and other statistical data, global application data like tax rate, discount rate etc. and to keep the track of users visiting the site.