Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

State Management in ASP.NET

Session , QueryString , Cookies, Hidden Filed , Application , etc.

  • Be the first to comment

State Management in ASP.NET

  1. 1. ASP.NET State management 1Shyam N. Chawda 9374928879
  2. 2. Welcome my dear students2 Shyam N. Chawda 9374928879
  3. 3. IsPostBack Property Page class’s property , which you can use to detect whether the page has already been post back to server. True/False True – Yes 2nd time For View Effects Take Dropdown and Add items in the form load Take Button 3 Shyam N. Chawda 9374928879
  4. 4. What is State management? HTTP protocol is a stateless protocol. Web server does not have any idea about the requests from where they coming On each request web pages are created and destroyed. 4 Shyam N. Chawda 9374928879
  5. 5. What is State management? A new instance of the Web page class is created each time the page is posted to the server. All information associated with the page and the controls on the page would be lost with each round trip. 5 Shyam N. Chawda 9374928879
  6. 6. What is State management? So, how do we make web pages which will remember about the user ? 6 Shyam N. Chawda 9374928879
  7. 7. What is State management? Client-Based Server-Based 7 Shyam N. Chawda 9374928879
  8. 8. What is State management? Client-Based 8 Shyam N. Chawda 9374928879
  9. 9. Shyam N. Chawda 93749288799 Client side View state Cookies Query strings Hidden fields
  10. 10. What is State management? Server-Based 10 Shyam N. Chawda 9374928879
  11. 11. Shyam N. Chawda 937492887911 Server side Application state Database Session state
  12. 12. Client Side state management Query String For understand it you must aware about Request and Response object 12 Shyam N. Chawda 9374928879
  13. 13. Response Object You can use this class to inject text into the page, jump to other page etc. Write Redirect 13 Shyam N. Chawda 9374928879
  14. 14. Request Object Provides access to the current page request, including the request headers, cookies, query string, and so on. IsSecureConnection Checks to see whether you are communicating via an HTTPS secure protocol or not. 14 Shyam N. Chawda 9374928879
  15. 15. Request Object Request.Browser.Browser Request.QueryString() 15 Shyam N. Chawda 9374928879
  16. 16. Query string Information that is appended to the end of a page URL. You can use a query string to submit data back to your page or to another page through the URL. Query strings provide a simple but limited way to maintain state information. 16 Shyam N. Chawda 9374928879
  17. 17. Query string For example in first page you collect information about your client, name and use this information in your second page. Take 2 forms Take 1 textbox and one button Response.Redirect("Page2.aspx?Name=" & TextBox1.Text Request.QueryString("Name") 17 Shyam N. Chawda 9374928879
  18. 18. Query string Response.redirect(“Formname?” & qry); Response.redirect(“Formname?name=” & value); 18 Shyam N. Chawda 9374928879
  19. 19. Query string Advantages No server resources are required Widespread support Almost all browsers and client devices support using query strings to pass values. Simple implementation 19 Shyam N. Chawda 9374928879
  20. 20. Query string Disadvantages Potential security risks The information in the query string is directly visible to the user via the browser's user interface. Limited capacity Some browsers and client devices impose a 2083-character limit on the length of URLs. 20 Shyam N. Chawda 9374928879
  21. 21. More than one Query string Using & Dim str As String str = "PageRequest.aspx?Name=" & TextBox1.Text & "&" & "Lname=" & TextBox2.Text 21 Shyam N. Chawda 9374928879
  22. 22. View State View State is used to remember controls state when page is posted back to server. You can store page-specific information 22 Shyam N. Chawda 9374928879
  23. 23. View State The view state is implemented with a hidden form field called _VIEWSTATE, which is automatically created in every Web page. 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. 23 Shyam N. Chawda 9374928879
  24. 24. View State 24 Shyam N. Chawda 9374928879
  25. 25. View State Go View Source Each control on a Web Forms page has a ViewState property. Take example of DropdownList Set Property EnableViewState=True Take also one button 25 Shyam N. Chawda 9374928879
  26. 26. View State <%@ Page Language="VB" EnableViewState="false" AutoEventWireup="false" CodeFile="frmViewState.aspx.vb" Inherits="frmViewState" %> By default true. So, even if it’s all the controls has set the property Enableviewstate=True but they are not working. 26 Shyam N. Chawda 9374928879
  27. 27. View State Take 2 Buttons and 1 TextBox Take one variable before all the coding Assign that variable in one button click and display in the textbox Click and 2nd button and write the same logic. 27 Shyam N. Chawda 9374928879
  28. 28. View State no = 5 ViewState("No") = no TextBox1.Text = ViewState ("No") 28 Shyam N. Chawda 9374928879
  29. 29. Shyam N. Chawda 937492887929 So,Moral of the story about ViewState EnableViewState property for each control EnableviewState attribute of the @Page (Page directive) View source _ViewState Store value using ViewState(Key)
  30. 30. View State Advantages No server resources are required Simple implementation View state does not require any custom programming to use. It is on by default to maintain state data on controls. 30 Shyam N. Chawda 9374928879
  31. 31. View State Enhanced security features The values in view state are hashed, compressed, and encoded for Unicode implementations, which provides more security than using hidden fields. 31 Shyam N. Chawda 9374928879
  32. 32. View State Disadvantages Performance considerations Storing large values can cause the page to slow down when users display it and when they post it. This is especially relevant for mobile devices, where bandwidth is often a limitation. 32 Shyam N. Chawda 9374928879
  33. 33. View State Device limitations Mobile devices might not have the memory capacity to store a large amount of view-state data. 33 Shyam N. Chawda 9374928879
  34. 34. Hidden Fields You can store page-specific information. This control is not visible when the application is viewed in the browser. Storage area for page specific. You must submit your pages to the server using the HTTP POST method 34 Shyam N. Chawda 9374928879
  35. 35. Shyam N. Chawda 937492887935
  36. 36. Hidden Fields Does not render in a Web browser. However, you can set the properties of the hidden field. A hidden field stores a single variable in its value property and must be explicitly added to the page. 36 Shyam N. Chawda 9374928879
  37. 37. Hidden Fields Advantages No server resources are required Widespread support Almost all browsers and client devices support forms with hidden fields. 37 Shyam N. Chawda 9374928879
  38. 38. Hidden Fields Simple implementation Hidden fields are standard HTML controls that require no complex programming logic. 38 Shyam N. Chawda 9374928879
  39. 39. Hidden Fields Disadvantages Potential security risks The information in the hidden field can be seen if the page output source is viewed directly, creating a potential security issue. Encrypt and decrypt the contents of a hidden field, but doing so requires extra coding and overhead. 39 Shyam N. Chawda 9374928879
  40. 40. Hidden Fields Performance considerations Because hidden fields are stored in the page itself, storing large values can cause the page to slow Storage limitations If the amount of data in a hidden field becomes very large, some proxies and firewalls will prevent access to the page that contains them. 40 Shyam N. Chawda 9374928879
  41. 41. Cookies A Cookie is a small text file that the browser creates and stores on the hard drive of your machine. Cookie is just one or more pieces of information stored as text strings. 41 Shyam N. Chawda 9374928879
  42. 42. Cookies Cookies are useful for storing small amounts of frequently changed information on the client. The cookie contains information the Web application can read whenever the user visits the site. Cookies are associated with a Web site, not with a specific page, so the browser and server will exchange cookie information. 42 Shyam N. Chawda 9374928879
  43. 43. Cookies Good example 1. A site conducting a poll might use a cookie simply as a Boolean value to indicate whether a user's browser has already participated in voting so that the user cannot vote twice. 2. Ebay assigns you an ID 43 Shyam N. Chawda 9374928879
  44. 44. Cookies Good example 1. A site conducting a poll might use a cookie simply as a Boolean value to indicate whether a user's browser has already participated in voting so that the user cannot vote twice. 2. Ebay assigns you an ID 44 Shyam N. Chawda 9374928879
  45. 45. Cookies Cookies can either be temporary or persistent. When creating a cookie, you specify a Name and Value. Each cookie must have a unique name. You can also set a cookie's date and time expiration. 45 Shyam N. Chawda 9374928879
  46. 46. Cookies If you do not set the cookie's expiration, the cookie is created but it is not stored on the user's hard disk. 46 Shyam N. Chawda 9374928879
  47. 47. Sets - Cookies Dim aCookie As New HttpCookie("lastVisit") aCookie.Values("Name") = txtName.Text aCookie.Values("Dt") = DateTime.Now.ToString aCookie.Expires = DateTime.Now.AddDays(1) Response.Cookies.Add(aCookie) 47 Shyam N. Chawda 9374928879
  48. 48. Gets - Cookies If Not Request.Cookies("lastVisit") Is Nothing Then txtNameR.Text = Request.Cookies("lastVisit").Values("Name").ToStri ng txtDateR.Text = Request.Cookies("lastVisit").Values("Dt").ToString End if 48 Shyam N. Chawda 9374928879
  49. 49. Cookies Advantages No server resources are required Simplicity The cookie is a lightweight, text-based structure. 49 Shyam N. Chawda 9374928879
  50. 50. Cookies Advantages Data persistence Although the durability of the cookie on a client computer is subject to cookie expiration processes on the client and user intervention, cookies are generally the most durable form of data persistence on the client. 50 Shyam N. Chawda 9374928879
  51. 51. Cookies Disadvantages Size limitations Most browsers place a 4096-byte limit on the size of a cookie, although support for 8192-byte 51 Shyam N. Chawda 9374928879
  52. 52. Cookies User-configured refusal Some users disable their browser or client device's ability to receive cookies, thereby limiting this functionality. 52 Shyam N. Chawda 9374928879
  53. 53. Cookies Potential security risks Users can manipulate cookies on their computer, which can potentially cause a security risk or cause the application that is dependent on the cookie to fail. 53 Shyam N. Chawda 9374928879
  54. 54. What is State Management? Server-Based Application state Session state Database 54 Shyam N. Chawda 9374928879
  55. 55. Application State Storing application wide-specific information such as objects and variables. All information stored in the application state is shared among all the pages of the Web application by using the HttpApplicationState class. 55 Shyam N. Chawda 9374928879
  56. 56. Application State The ideal data to insert into application state variables is data that is shared by multiple sessions and does not change often. 56 Shyam N. Chawda 9374928879
  57. 57. Application State Goto Global.asax Find Counter : Session_Start Application(“Cnt”)= Application(“Cnt”)= +1 57 Shyam N. Chawda 9374928879
  58. 58. Application State Application("Message") = "Welcome to the LJ” Application("Cnt") = 0 Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Application("Cnt") = Application("Cnt") + 1 58 Shyam N. Chawda 9374928879
  59. 59. Application State Advantages Simple implementation Application state is easy to use 59 Shyam N. Chawda 9374928879
  60. 60. Application State Application scope Because application state is accessible to all pages in an application, storing information in application state can mean keeping only a single copy of the information 60 Shyam N. Chawda 9374928879
  61. 61. Application State Disadvantages Limited durability of data Because global data that is stored in application state is volatile, it will be lost if the Web server process containing it is destroyed, such as from a server crash, upgrade, or shutdown. 61 Shyam N. Chawda 9374928879
  62. 62. Application State Resource requirements Application state requires server memory, which can affect the performance of the server as well as the scalability of the application. 62 Shyam N. Chawda 9374928879
  63. 63. Session State Use when you are storing short-lived information that is specific to an individual session and security is an issue. Do not store large quantities of information in session state. 63 Shyam N. Chawda 9374928879
  64. 64. Session State Session("FirstName") = FirstNameTextBox.Text Session("LastName") = LastNameTextBox.Text Each active ASP.NET session is identified and tracked using a 120-bit.$ SessionID values are generated using an algorithm 64 Shyam N. Chawda 9374928879
  65. 65. Session State The scope of session state is limited to the current browser session. If different users are accessing a Web application, each will have a different session state. Create Login Form 65 Shyam N. Chawda 9374928879
  66. 66. Session State The scope of session state is limited to the current browser session. If different users are accessing a Web application, each will have a different session state. Create Login Form 66 Shyam N. Chawda 9374928879
  67. 67. Session State Advantages Simple implementation Data persistence Data placed in session-state variables can be preserved through Internet Information Services (IIS) restarts and worker-process restarts without losing session data because the data is stored in another process space. 67 Shyam N. Chawda 9374928879
  68. 68. Session State Cookieless support Session state works with browsers that do not support HTTP cookies, although session state is most commonly used with cookies to provide user identification facilities to a Web application. 68 Shyam N. Chawda 9374928879
  69. 69. Session State Cookieless support Session state works with browsers that do not support HTTP cookies, although session state is most commonly used with cookies to provide user identification facilities to a Web application. 69 Shyam N. Chawda 9374928879
  70. 70. Session State Disadvantage Performance considerations Session-state variables stay in memory until they are either removed or replaced. 70 Shyam N. Chawda 9374928879
  71. 71. Database Database enables you to store large amount of information pertaining to state in your Web application. Sometimes users continually query the database by using the unique ID, you can save it in the database for use across multiple request for the pages in your site. 71 Shyam N. Chawda 9374928879
  72. 72. Database Advantages Security Access to databases requires rigorous authentication and authorization. 72 Shyam N. Chawda 9374928879
  73. 73. Database Storage capacity You can store as much information as you like in a database. Data persistence Database information can be stored as long as you like, and it is not subject to the availability of the Web server. 73 Shyam N. Chawda 9374928879
  74. 74. Database Disadvantage Complexity Using a database to support state management requires that the hardware and software configurations be more complex. 74 Shyam N. Chawda 9374928879
  75. 75. Thanks 75 Shyam N. Chawda 9374928879

×