State Management in ASP.Net<br />Web Pages developed in ASP.Net are HTTP based and HTTP protocol is a stateless protocol. It means that web server does not have any idea about the requests from where they coming i.e from same client or new clients. On each request web pages are created and destroyed.<br />So, how do we make web pages in ASP.Net which will remember about the user, would be able to distinguish b/w old clients(requests) and new clients(requests) and users previous filled information while navigating to other web pages in web site?<br />Solution of the above problem lies in State Management.<br />ASP.Net technology offers following state management techniques.<br />Client side State Management <br />Cookies<br />Hidden Fields<br />View State<br />Query String <br />Server side State Management <br />Session State<br />Application State <br />These state management techniques can be understood and by following simple examples and illustrations of the each techniques.<br />Client Side State Management<br />Cookies<br />A cookie is a small amount of data which is either stored at client side in text file or in memory of the client browser session. Cookies are always sent with the request to the web server and information can be retrieved from the cookies at the web server. In ASP.Net, HttpRequest object contains cookies collection which is nothing but list of HttpCookie objects. Cookies are generally used for tracking the user/request in ASP.Net for example, ASP.Net internally uses cookie to store session identifier to know whether request is coming from same client or not. We can also store some information like user identifier (UserName/Nick Name etc) in the cookies and retrieve them when any request is made to the web server as described in following example. It should be noted that cookies are generally used for storing only small amount of data(i.e 1-10 KB).<br />Code Sample<br />//Storing value in cookie HttpCookie cookie = new HttpCookie(\"
NickName\"
);cookie.Value = \"
David\"
;Request.Cookies.Add(cookie); //Retrieving value in cookie if (Request.Cookies.Count > 0 && Request.Cookies[\"
NickName\"
] != null)         lblNickName.Text = \"
Welcome\"
 + Request.Cookies[\"
NickName\"
].ToString();else         lblNickName.Text = \"
Welcome Guest\"
; <br /> Cookies can be permanent in nature or temporary. ASP.Net internally stores temporary cookie at the client side for storing session identifier. By default cookies are temporary and permanent cookie can be placed by setting \"
Expires\"
 property of the cookie object.<br />Hidden Fields<br />A Hidden control is the control which does not render anything on the web page at client browser but can be used to store some information on the web page which can be used on the page.<br />HTML input control offers hidden type of control by specifying type as \"
hidden\"
. Hidden control behaves like a normal control except that it is not rendered on the page. Its properties can be specified in a similar manner as you specify properties for other controls. This control will be posted to server in HttpControl collection whenever web form/page is posted to server. Any page specific information can be stored in the hidden field by specifying value property of the control.<br />ASP.Net provides HtmlInputControl that offers hidden field functionality.<br />Code Sample<br />//Declaring a hidden variable protected HtmlInputHidden hidNickName;//Populating hidden variablehidNickName.Value = \"
Page No 1\"
;//Retrieving value stored in hidden field.string str = hidNickName.Value; <br />Note:Critical information should not be stored in hidden fields.<br />View State/Control State<br />ASP.Net technology provides View State/Control State feature to the web forms. View State is used to remember controls state when page is posted back to server. ASP.Net stores view state on client site in hidden field __ViewState in encrypted form. When page is created on web sever this hidden control is populate with state of the controls and when page is posted back to server this information is retrieved and assigned to controls. You can look at this field by looking at the source of the page (i.e by right clicking on page and selecting view source option.)<br />You do not need to worry about this as this is automatically handled by ASP.Net. You can enable and disable view state behaviour of page and its control by specifying 'enableViewState' property to true and false. You can also store custom information in the view state as described in following code sample. This information can be used in round trips to the web server.<br />Code Sample<br />//To Save Information in View State ViewState.Add (\"
NickName\"
, \"
David\"
);//Retrieving View stateString strNickName = ViewState [\"
NickName\"
];<br />Query String<br />Query string is the limited way to pass information to the web server while navigating from one page to another page. This information is passed in url of the request.  Following is an example of retrieving information from the query strings.<br />Code Sample<br />//Retrieving values from query string String nickname;//Retrieving from query stringnickName = Request.Param[\"
NickName\"
].ToString(); But remember that many browsers impose a limit of 255 characters in query strings. You need to use HTTP-Get method to post a page to server otherwise query string values will not be available.<br />Server Side State Management<br />Session State<br />Session state is used to store and retrieve information about the user as user navigates from one page to another page in ASP.Net web application. Session state is maintained per user basis in ASPNet runtime. It can be of two types in-memory and out of memory. In most of the cases small web applications in-memory session state is used. Out of process session state management technique is used for the high traffic web applications or large applications. It can be configured  with some configuration settings in web.conig file to store state information in ASPNetState.exe (windows service exposed in .Net or on SQL server.<br />In-memory Session state can be used in following manner<br />Code Sample<br />//Storing informaton in session state Session[\"
NickName\"
] = \"
Ambuj\"
;//Retrieving information from session statestring str = Session[\"
NickName\"
]; <br />Session state is being maintained automatically by ASP.Net. A new session is started when a new user sents  first request to the server. At that time session state is created and user can use it to store information and retrieve it while navigating to different web pages in ASP.Net web application.<br />ASP.Net maintains session information using the session identifier which is being transacted b/w user machine and web server on each and every request either using cookies or querystring (if cookieless session is used in web application).<br />Application State<br />Application State is used to store information which is shared among users of the ASP.Net web application. Application state is stored in the memory of the windows process which is processing user requests on the web server. Application state is useful in storing small amount of often-used data. If application state is used for such data instead of frequent trips to database, then it increases the response time/performance of the web application.<br />In ASP.Net, application state is an instance of HttpApplicationState class and it exposes key-value pairs to store information. Its instance is automatically created when a first request is made to web application by any user and same state object is being shared across all subsequent users.<br />Application state can be used in similar manner as session state but it should be noted that many user might be accessing application state simultaneously so any call to application state object needs to be thread safe. This can be easily achieved in ASP.Net by using lock keyword on the statements which are accessing application state object. This lock keyword places a mutually exclusive lock on the statements and only allows a single thread to access the application state at a time. Following is an example of using application state in an application.<br />Code Sample<br />//Stroing information in application statelock (this) {        Application[\"
NickName\"
] = \"
David\"
; } //Retrieving value from application statelock (this) {       string str = Application[\"
NickName\"
].ToString(); } <br />So, In the above illustrations, we understood the practical concepts of using different state management techniques in ASP.Net techonology.<br />
State management
State management
State management
State management

More Related Content

PPTX
PPTX
State management
PPTX
ASP.NET State management
PPT
State management
PPTX
State Management in ASP.NET
PDF
State Management In ASP.NET And ASP.NET MVC
PPT
State management in ASP.NET
PPT
Session and state management
State management
ASP.NET State management
State management
State Management in ASP.NET
State Management In ASP.NET And ASP.NET MVC
State management in ASP.NET
Session and state management

What's hot (20)

PPSX
05 asp.net session07
PPTX
Chapter 8 part1
PPTX
C# cookieless session id and application state
PPTX
Session 29 - Servlets - Part 5
PPTX
Session 30 - Servlets - Part 6
PPTX
Session 39 - Hibernate - Part 1
PPTX
ASP.NET Lecture 4
PPTX
01 session tracking
PPT
ASP.NET 12 - State Management
PPTX
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
PPTX
Session And Cookies In Servlets - Java
PDF
Weblogic configuration
PPTX
Using cookies and sessions
PPTX
Session 28 - Servlets - Part 4
PPTX
Advance Java
PPTX
Query Store and live Query Statistics
DOCX
State management servlet
PDF
Web II - 02 - How ASP.NET Works
05 asp.net session07
Chapter 8 part1
C# cookieless session id and application state
Session 29 - Servlets - Part 5
Session 30 - Servlets - Part 6
Session 39 - Hibernate - Part 1
ASP.NET Lecture 4
01 session tracking
ASP.NET 12 - State Management
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session And Cookies In Servlets - Java
Weblogic configuration
Using cookies and sessions
Session 28 - Servlets - Part 4
Advance Java
Query Store and live Query Statistics
State management servlet
Web II - 02 - How ASP.NET Works
Ad

Viewers also liked (9)

PPTX
State management
PPTX
Introduction to asp.net
PPT
ASP.NET Tutorial - Presentation 1
PPT
ASP.NET Session 6
PPTX
Cookie & Session In ASP.NET
PPT
Asp.net
PPT
Concepts of Asp.Net
PPTX
ASP.NET Presentation
PPT
Asp.net.
State management
Introduction to asp.net
ASP.NET Tutorial - Presentation 1
ASP.NET Session 6
Cookie & Session In ASP.NET
Asp.net
Concepts of Asp.Net
ASP.NET Presentation
Asp.net.
Ad

Similar to State management (20)

DOC
State management in asp
PPTX
State management
PPT
2310 b 14
PDF
state management asp.net
PDF
Asp.net state management
PPT
StateManagement in ASP.Net.ppt
PPS
05 asp.net session07
PPTX
State Management.pptx
PPS
05 asp.net session07
PPTX
PPT
Session viii(state mngtclient)
DOCX
Managing states
PPTX
81.pptx ajx fyjc semester paper 2 parrtens
PPTX
ASP.NET Lecture 2
PPTX
PPTX
Managing state in asp.net
PPT
Session viii(state mngtserver)
PPTX
Sessions&cookies
PPTX
Ch05 state management
State management in asp
State management
2310 b 14
state management asp.net
Asp.net state management
StateManagement in ASP.Net.ppt
05 asp.net session07
State Management.pptx
05 asp.net session07
Session viii(state mngtclient)
Managing states
81.pptx ajx fyjc semester paper 2 parrtens
ASP.NET Lecture 2
Managing state in asp.net
Session viii(state mngtserver)
Sessions&cookies
Ch05 state management

More from Iblesoft (16)

PPT
Ms sql server ii
PPT
MS SQL Server 1
PPTX
Master pages ppt
PPT
Validation controls ppt
PPT
Controls
PPT
Ado.net
PPT
Generics n delegates
PPT
Ajaxppt
PPT
Data controls ppt
PPTX
Microsoft.net architecturte
PPT
Asp.net architecture
PPTX
Generics
PPTX
Delegates and events
PPT
Javascript
PPT
Html ppt
PPT
Exception handling
Ms sql server ii
MS SQL Server 1
Master pages ppt
Validation controls ppt
Controls
Ado.net
Generics n delegates
Ajaxppt
Data controls ppt
Microsoft.net architecturte
Asp.net architecture
Generics
Delegates and events
Javascript
Html ppt
Exception handling

Recently uploaded (20)

PDF
Farming Based Livelihood Systems English Notes
PDF
PUBH1000 - Module 6: Global Health Tute Slides
PPTX
Integrated Management of Neonatal and Childhood Illnesses (IMNCI) – Unit IV |...
PPTX
BSCE 2 NIGHT (CHAPTER 2) just cases.pptx
PDF
Hospital Case Study .architecture design
PDF
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
PDF
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PDF
Everyday Spelling and Grammar by Kathi Wyldeck
PPTX
Diploma pharmaceutics notes..helps diploma students
PDF
Journal of Dental Science - UDMY (2020).pdf
PDF
Laparoscopic Colorectal Surgery at WLH Hospital
PDF
Diabetes Mellitus , types , clinical picture, investigation and managment
PPTX
Climate Change and Its Global Impact.pptx
PPTX
Macbeth play - analysis .pptx english lit
PPTX
4. Diagnosis and treatment planning in RPD.pptx
PPTX
PLASMA AND ITS CONSTITUENTS 123.pptx
PDF
faiz-khans about Radiotherapy Physics-02.pdf
PDF
Journal of Dental Science - UDMY (2022).pdf
PDF
Fun with Grammar (Communicative Activities for the Azar Grammar Series)
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
Farming Based Livelihood Systems English Notes
PUBH1000 - Module 6: Global Health Tute Slides
Integrated Management of Neonatal and Childhood Illnesses (IMNCI) – Unit IV |...
BSCE 2 NIGHT (CHAPTER 2) just cases.pptx
Hospital Case Study .architecture design
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
Everyday Spelling and Grammar by Kathi Wyldeck
Diploma pharmaceutics notes..helps diploma students
Journal of Dental Science - UDMY (2020).pdf
Laparoscopic Colorectal Surgery at WLH Hospital
Diabetes Mellitus , types , clinical picture, investigation and managment
Climate Change and Its Global Impact.pptx
Macbeth play - analysis .pptx english lit
4. Diagnosis and treatment planning in RPD.pptx
PLASMA AND ITS CONSTITUENTS 123.pptx
faiz-khans about Radiotherapy Physics-02.pdf
Journal of Dental Science - UDMY (2022).pdf
Fun with Grammar (Communicative Activities for the Azar Grammar Series)
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf

State management

  • 1. State Management in ASP.Net<br />Web Pages developed in ASP.Net are HTTP based and HTTP protocol is a stateless protocol. It means that web server does not have any idea about the requests from where they coming i.e from same client or new clients. On each request web pages are created and destroyed.<br />So, how do we make web pages in ASP.Net which will remember about the user, would be able to distinguish b/w old clients(requests) and new clients(requests) and users previous filled information while navigating to other web pages in web site?<br />Solution of the above problem lies in State Management.<br />ASP.Net technology offers following state management techniques.<br />Client side State Management <br />Cookies<br />Hidden Fields<br />View State<br />Query String <br />Server side State Management <br />Session State<br />Application State <br />These state management techniques can be understood and by following simple examples and illustrations of the each techniques.<br />Client Side State Management<br />Cookies<br />A cookie is a small amount of data which is either stored at client side in text file or in memory of the client browser session. Cookies are always sent with the request to the web server and information can be retrieved from the cookies at the web server. In ASP.Net, HttpRequest object contains cookies collection which is nothing but list of HttpCookie objects. Cookies are generally used for tracking the user/request in ASP.Net for example, ASP.Net internally uses cookie to store session identifier to know whether request is coming from same client or not. We can also store some information like user identifier (UserName/Nick Name etc) in the cookies and retrieve them when any request is made to the web server as described in following example. It should be noted that cookies are generally used for storing only small amount of data(i.e 1-10 KB).<br />Code Sample<br />//Storing value in cookie HttpCookie cookie = new HttpCookie(\" NickName\" );cookie.Value = \" David\" ;Request.Cookies.Add(cookie); //Retrieving value in cookie if (Request.Cookies.Count > 0 && Request.Cookies[\" NickName\" ] != null)         lblNickName.Text = \" Welcome\" + Request.Cookies[\" NickName\" ].ToString();else         lblNickName.Text = \" Welcome Guest\" ; <br /> Cookies can be permanent in nature or temporary. ASP.Net internally stores temporary cookie at the client side for storing session identifier. By default cookies are temporary and permanent cookie can be placed by setting \" Expires\" property of the cookie object.<br />Hidden Fields<br />A Hidden control is the control which does not render anything on the web page at client browser but can be used to store some information on the web page which can be used on the page.<br />HTML input control offers hidden type of control by specifying type as \" hidden\" . Hidden control behaves like a normal control except that it is not rendered on the page. Its properties can be specified in a similar manner as you specify properties for other controls. This control will be posted to server in HttpControl collection whenever web form/page is posted to server. Any page specific information can be stored in the hidden field by specifying value property of the control.<br />ASP.Net provides HtmlInputControl that offers hidden field functionality.<br />Code Sample<br />//Declaring a hidden variable protected HtmlInputHidden hidNickName;//Populating hidden variablehidNickName.Value = \" Page No 1\" ;//Retrieving value stored in hidden field.string str = hidNickName.Value; <br />Note:Critical information should not be stored in hidden fields.<br />View State/Control State<br />ASP.Net technology provides View State/Control State feature to the web forms. View State is used to remember controls state when page is posted back to server. ASP.Net stores view state on client site in hidden field __ViewState in encrypted form. When page is created on web sever this hidden control is populate with state of the controls and when page is posted back to server this information is retrieved and assigned to controls. You can look at this field by looking at the source of the page (i.e by right clicking on page and selecting view source option.)<br />You do not need to worry about this as this is automatically handled by ASP.Net. You can enable and disable view state behaviour of page and its control by specifying 'enableViewState' property to true and false. You can also store custom information in the view state as described in following code sample. This information can be used in round trips to the web server.<br />Code Sample<br />//To Save Information in View State ViewState.Add (\" NickName\" , \" David\" );//Retrieving View stateString strNickName = ViewState [\" NickName\" ];<br />Query String<br />Query string is the limited way to pass information to the web server while navigating from one page to another page. This information is passed in url of the request.  Following is an example of retrieving information from the query strings.<br />Code Sample<br />//Retrieving values from query string String nickname;//Retrieving from query stringnickName = Request.Param[\" NickName\" ].ToString(); But remember that many browsers impose a limit of 255 characters in query strings. You need to use HTTP-Get method to post a page to server otherwise query string values will not be available.<br />Server Side State Management<br />Session State<br />Session state is used to store and retrieve information about the user as user navigates from one page to another page in ASP.Net web application. Session state is maintained per user basis in ASPNet runtime. It can be of two types in-memory and out of memory. In most of the cases small web applications in-memory session state is used. Out of process session state management technique is used for the high traffic web applications or large applications. It can be configured  with some configuration settings in web.conig file to store state information in ASPNetState.exe (windows service exposed in .Net or on SQL server.<br />In-memory Session state can be used in following manner<br />Code Sample<br />//Storing informaton in session state Session[\" NickName\" ] = \" Ambuj\" ;//Retrieving information from session statestring str = Session[\" NickName\" ]; <br />Session state is being maintained automatically by ASP.Net. A new session is started when a new user sents  first request to the server. At that time session state is created and user can use it to store information and retrieve it while navigating to different web pages in ASP.Net web application.<br />ASP.Net maintains session information using the session identifier which is being transacted b/w user machine and web server on each and every request either using cookies or querystring (if cookieless session is used in web application).<br />Application State<br />Application State is used to store information which is shared among users of the ASP.Net web application. Application state is stored in the memory of the windows process which is processing user requests on the web server. Application state is useful in storing small amount of often-used data. If application state is used for such data instead of frequent trips to database, then it increases the response time/performance of the web application.<br />In ASP.Net, application state is an instance of HttpApplicationState class and it exposes key-value pairs to store information. Its instance is automatically created when a first request is made to web application by any user and same state object is being shared across all subsequent users.<br />Application state can be used in similar manner as session state but it should be noted that many user might be accessing application state simultaneously so any call to application state object needs to be thread safe. This can be easily achieved in ASP.Net by using lock keyword on the statements which are accessing application state object. This lock keyword places a mutually exclusive lock on the statements and only allows a single thread to access the application state at a time. Following is an example of using application state in an application.<br />Code Sample<br />//Stroing information in application statelock (this) {        Application[\" NickName\" ] = \" David\" ; } //Retrieving value from application statelock (this) {       string str = Application[\" NickName\" ].ToString(); } <br />So, In the above illustrations, we understood the practical concepts of using different state management techniques in ASP.Net techonology.<br />