SlideShare ist ein Scribd-Unternehmen logo
1 von 5
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(quot;
NickNamequot;
);cookie.Value = quot;
Davidquot;
;Request.Cookies.Add(cookie); //Retrieving value in cookie if (Request.Cookies.Count > 0 && Request.Cookies[quot;
NickNamequot;
] != null)         lblNickName.Text = quot;
Welcomequot;
 + Request.Cookies[quot;
NickNamequot;
].ToString();else         lblNickName.Text = quot;
Welcome Guestquot;
; <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 quot;
Expiresquot;
 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 quot;
hiddenquot;
. 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 = quot;
Page No 1quot;
;//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 (quot;
NickNamequot;
, quot;
Davidquot;
);//Retrieving View stateString strNickName = ViewState [quot;
NickNamequot;
];<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[quot;
NickNamequot;
].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[quot;
NickNamequot;
] = quot;
Ambujquot;
;//Retrieving information from session statestring str = Session[quot;
NickNamequot;
]; <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[quot;
NickNamequot;
] = quot;
Davidquot;
; } //Retrieving value from application statelock (this) {       string str = Application[quot;
NickNamequot;
].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

Weitere ähnliche Inhalte

Was ist angesagt?

05 asp.net session07
05 asp.net session0705 asp.net session07
05 asp.net session07Vivek chan
 
C# cookieless session id and application state
C# cookieless session id and application stateC# cookieless session id and application state
C# cookieless session id and application stateMalav Patel
 
Session 29 - Servlets - Part 5
Session 29 - Servlets - Part 5Session 29 - Servlets - Part 5
Session 29 - Servlets - Part 5PawanMM
 
Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6PawanMM
 
Session 39 - Hibernate - Part 1
Session 39 - Hibernate - Part 1Session 39 - Hibernate - Part 1
Session 39 - Hibernate - Part 1PawanMM
 
01 session tracking
01   session tracking01   session tracking
01 session trackingdhrubo kayal
 
ASP.NET 12 - State Management
ASP.NET 12 - State ManagementASP.NET 12 - State Management
ASP.NET 12 - State ManagementRandy Connolly
 
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session 31 - Session Management, Best Practices, Design Patterns in Web AppsSession 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session 31 - Session Management, Best Practices, Design Patterns in Web AppsPawanMM
 
Session And Cookies In Servlets - Java
Session And Cookies In Servlets - JavaSession And Cookies In Servlets - Java
Session And Cookies In Servlets - JavaJainamParikh3
 
Weblogic configuration
Weblogic configurationWeblogic configuration
Weblogic configurationAditya Bhuyan
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessionsNuha Noor
 
Session 28 - Servlets - Part 4
Session 28 - Servlets - Part 4Session 28 - Servlets - Part 4
Session 28 - Servlets - Part 4PawanMM
 
Query Store and live Query Statistics
Query Store and live Query StatisticsQuery Store and live Query Statistics
Query Store and live Query StatisticsSolidQ
 
Web II - 02 - How ASP.NET Works
Web II - 02 - How ASP.NET WorksWeb II - 02 - How ASP.NET Works
Web II - 02 - How ASP.NET WorksRandy Connolly
 

Was ist angesagt? (20)

05 asp.net session07
05 asp.net session0705 asp.net session07
05 asp.net session07
 
Chapter 8 part1
Chapter 8   part1Chapter 8   part1
Chapter 8 part1
 
C# cookieless session id and application state
C# cookieless session id and application stateC# cookieless session id and application state
C# cookieless session id and application state
 
Session 29 - Servlets - Part 5
Session 29 - Servlets - Part 5Session 29 - Servlets - Part 5
Session 29 - Servlets - Part 5
 
Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6Session 30 - Servlets - Part 6
Session 30 - Servlets - Part 6
 
Cache
CacheCache
Cache
 
Session 39 - Hibernate - Part 1
Session 39 - Hibernate - Part 1Session 39 - Hibernate - Part 1
Session 39 - Hibernate - Part 1
 
ASP.NET Lecture 4
ASP.NET Lecture 4ASP.NET Lecture 4
ASP.NET Lecture 4
 
01 session tracking
01   session tracking01   session tracking
01 session tracking
 
ASP.NET 12 - State Management
ASP.NET 12 - State ManagementASP.NET 12 - State Management
ASP.NET 12 - State Management
 
Jsp
JspJsp
Jsp
 
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session 31 - Session Management, Best Practices, Design Patterns in Web AppsSession 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
 
Session And Cookies In Servlets - Java
Session And Cookies In Servlets - JavaSession And Cookies In Servlets - Java
Session And Cookies In Servlets - Java
 
Weblogic configuration
Weblogic configurationWeblogic configuration
Weblogic configuration
 
Using cookies and sessions
Using cookies and sessionsUsing cookies and sessions
Using cookies and sessions
 
Session 28 - Servlets - Part 4
Session 28 - Servlets - Part 4Session 28 - Servlets - Part 4
Session 28 - Servlets - Part 4
 
Advance Java
Advance JavaAdvance Java
Advance Java
 
Query Store and live Query Statistics
Query Store and live Query StatisticsQuery Store and live Query Statistics
Query Store and live Query Statistics
 
State management servlet
State management servletState management servlet
State management servlet
 
Web II - 02 - How ASP.NET Works
Web II - 02 - How ASP.NET WorksWeb II - 02 - How ASP.NET Works
Web II - 02 - How ASP.NET Works
 

Andere mochten auch

State management
State managementState management
State managementIblesoft
 
ASP.NET Tutorial - Presentation 1
ASP.NET Tutorial - Presentation 1ASP.NET Tutorial - Presentation 1
ASP.NET Tutorial - Presentation 1Kumar S
 
ASP.NET Session 6
ASP.NET Session 6ASP.NET Session 6
ASP.NET Session 6Sisir Ghosh
 
Cookie & Session In ASP.NET
Cookie & Session In ASP.NETCookie & Session In ASP.NET
Cookie & Session In ASP.NETShingalaKrupa
 
Concepts of Asp.Net
Concepts of Asp.NetConcepts of Asp.Net
Concepts of Asp.Netvidyamittal
 
ASP.NET Presentation
ASP.NET PresentationASP.NET Presentation
ASP.NET Presentationdimuthu22
 

Andere mochten auch (9)

State management
State managementState management
State management
 
Introduction to asp.net
Introduction to asp.netIntroduction to asp.net
Introduction to asp.net
 
ASP.NET Tutorial - Presentation 1
ASP.NET Tutorial - Presentation 1ASP.NET Tutorial - Presentation 1
ASP.NET Tutorial - Presentation 1
 
ASP.NET Session 6
ASP.NET Session 6ASP.NET Session 6
ASP.NET Session 6
 
Cookie & Session In ASP.NET
Cookie & Session In ASP.NETCookie & Session In ASP.NET
Cookie & Session In ASP.NET
 
Asp.net
 Asp.net Asp.net
Asp.net
 
Concepts of Asp.Net
Concepts of Asp.NetConcepts of Asp.Net
Concepts of Asp.Net
 
ASP.NET Presentation
ASP.NET PresentationASP.NET Presentation
ASP.NET Presentation
 
Asp.net.
Asp.net.Asp.net.
Asp.net.
 

Ähnlich wie State management

C sharp and asp.net interview questions
C sharp and asp.net interview questionsC sharp and asp.net interview questions
C sharp and asp.net interview questionsAkhil Mittal
 
StateManagement in ASP.Net.ppt
StateManagement in ASP.Net.pptStateManagement in ASP.Net.ppt
StateManagement in ASP.Net.pptcharusharma165
 
Session viii(state mngtclient)
Session viii(state mngtclient)Session viii(state mngtclient)
Session viii(state mngtclient)Shrijan Tiwari
 
05 asp.net session07
05 asp.net session0705 asp.net session07
05 asp.net session07Mani Chaubey
 
state managment
state managment state managment
state managment aniliimd
 
Session viii(state mngtserver)
Session viii(state mngtserver)Session viii(state mngtserver)
Session viii(state mngtserver)Shrijan Tiwari
 
WEB MODULE 5.pdf
WEB MODULE 5.pdfWEB MODULE 5.pdf
WEB MODULE 5.pdfDeepika A B
 
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08Vivek chan
 
State management 1
State management 1State management 1
State management 1singhadarsh
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4... Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...WebStackAcademy
 

Ähnlich wie State management (20)

Asp.net
Asp.netAsp.net
Asp.net
 
C sharp and asp.net interview questions
C sharp and asp.net interview questionsC sharp and asp.net interview questions
C sharp and asp.net interview questions
 
Chapter 8 part2
Chapter 8   part2Chapter 8   part2
Chapter 8 part2
 
StateManagement in ASP.Net.ppt
StateManagement in ASP.Net.pptStateManagement in ASP.Net.ppt
StateManagement in ASP.Net.ppt
 
Managing states
Managing statesManaging states
Managing states
 
ASP.NET Lecture 2
ASP.NET Lecture 2ASP.NET Lecture 2
ASP.NET Lecture 2
 
Session viii(state mngtclient)
Session viii(state mngtclient)Session viii(state mngtclient)
Session viii(state mngtclient)
 
Ch05 state management
Ch05 state managementCh05 state management
Ch05 state management
 
2310 b 14
2310 b 142310 b 14
2310 b 14
 
05 asp.net session07
05 asp.net session0705 asp.net session07
05 asp.net session07
 
Aspnet Caching
Aspnet CachingAspnet Caching
Aspnet Caching
 
state managment
state managment state managment
state managment
 
WEB Mod5@AzDOCUMENTS.in.pdf
WEB Mod5@AzDOCUMENTS.in.pdfWEB Mod5@AzDOCUMENTS.in.pdf
WEB Mod5@AzDOCUMENTS.in.pdf
 
Session viii(state mngtserver)
Session viii(state mngtserver)Session viii(state mngtserver)
Session viii(state mngtserver)
 
WEB MODULE 5.pdf
WEB MODULE 5.pdfWEB MODULE 5.pdf
WEB MODULE 5.pdf
 
Caching in asp.net
Caching in asp.netCaching in asp.net
Caching in asp.net
 
Caching in asp.net
Caching in asp.netCaching in asp.net
Caching in asp.net
 
06 asp.net session08
06 asp.net session0806 asp.net session08
06 asp.net session08
 
State management 1
State management 1State management 1
State management 1
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
 Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4... Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 4...
 

Mehr von Iblesoft

Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server iiIblesoft
 
MS SQL Server 1
MS SQL Server 1MS SQL Server 1
MS SQL Server 1Iblesoft
 
Master pages ppt
Master pages pptMaster pages ppt
Master pages pptIblesoft
 
Validation controls ppt
Validation controls pptValidation controls ppt
Validation controls pptIblesoft
 
Generics n delegates
Generics n delegatesGenerics n delegates
Generics n delegatesIblesoft
 
Data controls ppt
Data controls pptData controls ppt
Data controls pptIblesoft
 
Microsoft.net architecturte
Microsoft.net architecturteMicrosoft.net architecturte
Microsoft.net architecturteIblesoft
 
Asp.net architecture
Asp.net architectureAsp.net architecture
Asp.net architectureIblesoft
 
Delegates and events
Delegates and eventsDelegates and events
Delegates and eventsIblesoft
 
Javascript
JavascriptJavascript
JavascriptIblesoft
 
Exception handling
Exception handlingException handling
Exception handlingIblesoft
 

Mehr von Iblesoft (16)

Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
MS SQL Server 1
MS SQL Server 1MS SQL Server 1
MS SQL Server 1
 
Master pages ppt
Master pages pptMaster pages ppt
Master pages ppt
 
Validation controls ppt
Validation controls pptValidation controls ppt
Validation controls ppt
 
Controls
ControlsControls
Controls
 
Ado.net
Ado.netAdo.net
Ado.net
 
Generics n delegates
Generics n delegatesGenerics n delegates
Generics n delegates
 
Ajaxppt
AjaxpptAjaxppt
Ajaxppt
 
Data controls ppt
Data controls pptData controls ppt
Data controls ppt
 
Microsoft.net architecturte
Microsoft.net architecturteMicrosoft.net architecturte
Microsoft.net architecturte
 
Asp.net architecture
Asp.net architectureAsp.net architecture
Asp.net architecture
 
Generics
GenericsGenerics
Generics
 
Delegates and events
Delegates and eventsDelegates and events
Delegates and events
 
Javascript
JavascriptJavascript
Javascript
 
Html ppt
Html pptHtml ppt
Html ppt
 
Exception handling
Exception handlingException handling
Exception handling
 

Kürzlich hochgeladen

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 

Kürzlich hochgeladen (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 

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(quot; NickNamequot; );cookie.Value = quot; Davidquot; ;Request.Cookies.Add(cookie); //Retrieving value in cookie if (Request.Cookies.Count > 0 && Request.Cookies[quot; NickNamequot; ] != null)         lblNickName.Text = quot; Welcomequot; + Request.Cookies[quot; NickNamequot; ].ToString();else         lblNickName.Text = quot; Welcome Guestquot; ; <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 quot; Expiresquot; 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 quot; hiddenquot; . 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 = quot; Page No 1quot; ;//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 (quot; NickNamequot; , quot; Davidquot; );//Retrieving View stateString strNickName = ViewState [quot; NickNamequot; ];<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[quot; NickNamequot; ].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[quot; NickNamequot; ] = quot; Ambujquot; ;//Retrieving information from session statestring str = Session[quot; NickNamequot; ]; <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[quot; NickNamequot; ] = quot; Davidquot; ; } //Retrieving value from application statelock (this) {       string str = Application[quot; NickNamequot; ].ToString(); } <br />So, In the above illustrations, we understood the practical concepts of using different state management techniques in ASP.Net techonology.<br />