State management

Page 1

1


2


What is expected? You are developing a web site and following is what functionality that you expect from your web site: 1. An user requests for a login page of your web site 2. Server processes the request and send the desired page to the user 3. User gets the page 4. User enters user id and password 5. Then submits the page 6. User is redirected after successful login to another page 7. The user gets the second page as response with a welcome message on the page displayed using user’s user name entered in the first text box in the previous page. Conclusion: You want to transfer data from one page to another page.

3


4


5


6


7


8


9


10


11


12


13


14


15


16


17


18


19


Notes: Size restrictions: There is a limitation of 4096-byte on the size of a cookie in most browsers. Currently, support for 8192-byte cookie size is becoming common in latest browsers and client-devices

20


How to write a cookie by creating an instance of the HttpCookie object:

21


22


23


You might occasionally need to read through all the cookies available to the page. To read the names and values of all the cookies available to the page, you can loop through the Cookie collection using above code.

24


You can read a cookie's HasKeys property to determine whether the cookie has subkeys. If so, you can read the subkey collection to get individual subkey names and values. You can read subkey values from the Values collection directly by index value. The corresponding subkey names are available in the AllKeys member of the Values collection, which returns an array of strings. You can also use the Keys member of the Values collection. However, the AllKeys property is cached the first time it is accessed. In contrast, the Keys property builds an array each time it is accessed. For this reason, the AllKeys property is much faster on subsequent accesses within the context of the same page request.

25


26


27


28


Notes: Note that information is always retrieved as a string, which can then be converted to another simple data type.

29


30


31


32


33


34


35


36


37


Notes: Session State is available as the HtpSessionState class. Example: To add a specific user name (for eg., the value of the variable loggedInUser), to session state use the following code: Session *“userName”+ = loggedInUser; To retrieve this logged in user from the session state use the following code: string currentUser; currentUser = int.Parse( Session* “UserId ”+.ToString() );

38


39


40


41


42


43


44


45


46


Notes: Example: Consider a example where you need to add a static set of data like USA states. This can be done as:

Application *“States”+ = statesList; In the Page_Load the DataView is again retrieved from Application state as: ArrayList states = (ArrayList) Application* “States”+ ;

47


48


49


50


51


52


53


54


55


56


57


58


59


60


Notes: If you use hidden fields, it is recommended to store only small amounts of frequently changed data on the client.

61


62


By default, the Session Manager works in-process and utilizes cookies. This makes it perfectly aligned with the behavior of the ASP Session object. You can make it work outside the IIS process, on the same machine, or on a different one. This clearly results in slower performance, but it's more reliable. The SqlServer option lets you use a local or remote SQL Server database to store the data. The SqlServer option is out-ofprocess with respect to IIS. The out-of-process options (both StateServer and SqlServer) also make the use of Session suitable for Web farms. This feature may have a significant impact on scalability. If you plan to deploy an application on a Web farm, using Session soon becomes a more attractive option. You don't have to worry about the physical distribution of the machines since the Session Manager does that for you. In addition, any alternative to using Session, such as persisting to XML on local disk, requires you to use a shared path or to know the physical address of the machine that is actually processing your request.

63


64


65


66


67


68


69


Reference

70


71


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.