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

I am not the author of this file but wanted to collect it for study purpose.

  • Be the first to comment

State management

  1. 2. ? Why State Management?
  2. 3. <ul><ul><li>Web form pages are HTTP-Based, they are stateless, which means they don’t know whether the requests are all from the same client. </li></ul></ul><ul><ul><li>pages are destroyed and recreated with each round trip to the server. </li></ul></ul><ul><ul><li>information will be lost. </li></ul></ul>Why State Management?
  3. 4. What is State Management?
  4. 5. What is State Management? <ul><li>The state Management is a storage mechanism that is store user’s data across the moves between pages and postback of page. It’s accessible from One or all the web pages that were requested by a web browser. </li></ul><ul><li>The state Management is perfect for storing specific information that is connected to a specific user or Application Level. </li></ul><ul><li>Some state Management techniques has a timeout period and after the timeout the data is lost .The timeout occurs when the user is no longer active for a period of time. </li></ul>
  5. 6. Different Ways to Manage State? <ul><li>Client-side state management . </li></ul><ul><li>Server-side state management. </li></ul>
  6. 7. Client-side State Management There is no information maintained on the server between round trips. Information will be stored in the page or on the client’s computer With Help Of .. <ul><li>Query Strings. </li></ul><ul><li>Hidden Field. </li></ul><ul><li>Cookies. </li></ul><ul><li>View State . </li></ul><ul><li>Control State. </li></ul>
  7. 8. Server-side State Management <ul><li>Applications object. </li></ul><ul><li>Session object. </li></ul><ul><li>Cache Object </li></ul><ul><li>Database. </li></ul>Information will be stored on the server, it has higher security but it can use more web server resources.
  8. 9. Client-side State Management Query Strings What are Query Strings? Query strings are data that is appended to the end of a page URL. They are commonly used to hold String data like page numbers or search terms or other data that isn't confidential. The user can see the values which the query string holds without using special operations. An example of a query string can look like;b=2. The Query String Structure As written earlier, query strings are appended to the end of a URL. First a question mark is appended to the URL's end and then every parameter that we want to hold in the query string . The parameters declare the parameter name followed by = symbol which followed by the data to hold. Every parameter is separated with the ampersand symbol(&).
  9. 10. <ul><li>Query String Limitations </li></ul><ul><ul><ul><li>You can use query string technique when passing Only String Type Data. </li></ul></ul></ul><ul><ul><ul><li>Risk When you pass non secure data. </li></ul></ul></ul><ul><ul><ul><li>Increase Network traffic. </li></ul></ul></ul><ul><ul><ul><li>There are a few browser limitation when using query strings that impose </li></ul></ul></ul><ul><ul><li> a length limitation on the query string( 2083-character followed by Length Limit of URL ) . </li></ul></ul><ul><li>Query String Advantage </li></ul><ul><ul><ul><li>Query strings provide a simple way to maintain state information. </li></ul></ul></ul><ul><ul><ul><li>Does not required server side resources. </li></ul></ul></ul>How To Use Query Strings When you need to use a query string data you do it in the following way:       string queryStringData = Request.QueryString[&quot;data&quot;]; Query Strings Client-side State Management
  10. 11. Client-side State Management Hidden Field What are Hidden Fields ? A hidden field does not render visibly in the browser, but you can set its properties just as you can with a standard control. When a page is submitted to the server, the content of a hidden field is sent in the HTTP Form collection along with the values of other controls. A hidden field acts as a repository for any page-specific information that you would like to store directly in the page. Hidden field stores a single variable in its value property and must be explicitly added it to the page.ASP.NET provides the HtmlInputHidden control that offers hidden field functionality. Hidden Fields Values Hidden fields store only one value in their value property. The value is saved as a string and therefore in order to use it for other types you need to perform casting. You can see the stored data easily by using the View Source operation of the browser. You can see it by clicking the right mouse button and then choosing View Source from the menu (if the operation is available). The values has page context and therefore when you leave a page the data stored in the hidden fields is disposed.
  11. 12. <ul><li>Hidden Field Limitations </li></ul><ul><ul><ul><li>Hidden fields store only one value in their value property. The value is Only String Type Data and therefore in order to use it for other types you need to perform casting .  </li></ul></ul></ul><ul><ul><ul><li>Risk When you pass non secure data. </li></ul></ul></ul><ul><ul><ul><li>Increase Network traffic. </li></ul></ul></ul><ul><li>Hidden Field Advantage </li></ul><ul><ul><ul><li>Does not required server side resources so performance is high. </li></ul></ul></ul><ul><ul><ul><li>Can contain huge amount of data. </li></ul></ul></ul>How To Use Hidden Field protected System.Web.UI.HtmlControls.HtmlInputHidden Hidden1; //to assign a value to Hidden field Hidden1.Value=”this is a test”; //to retrieve a value string str=Hidden1.Value;
  12. 13. Cookies Client-side State Management What are Cookies? A cookie is a piece of data that is saved either in the memory of the client’s web browser or as a text file in the file system of the client. Cookies are used to identify a user, to store state information, preferences of the user and etc. How Does Cookies Mechanism Work? The mechanism of cookies is simple. When a client request a web page from a server the first request isn't containing a cookie . The server identify that the client has no cookie and generate . Then the server sends the cookie to the client and from now on the client will send the cookie in every request and the server will send the cookie in every response.
  13. 14. <ul><li>write the cookie to the response </li></ul><ul><ul><li>Response.Cookies[&quot;cookie&quot;].Value = &quot;value“; </li></ul></ul><ul><ul><li>In the HttpCookie class there is a Expires property. If you don't use it (like in the example) the cookie is saved in memory and discarded if the user closes the browser. If you use the property you must define the amount of time the cookie is saved. In order to delete a saved cookie you need to use the Expires property and pass a past expiration date. There is no way to delete a cookie otherwise. </li></ul></ul><ul><ul><li>In the previous example the cookie saves a single value. The cookie can save multiple values like in the next example. Response.Cookies[&quot;cookie&quot;][&quot;key1&quot;].Value = &quot;value1&quot;; </li></ul></ul><ul><ul><li>Response.Cookies[&quot;cookie&quot;][&quot;key2&quot;].Value = &quot;value2&quot;; </li></ul></ul><ul><ul><li>Response.Cookies[&quot;cookie&quot;][&quot;key3&quot;].Value = &quot;value3&quot;; </li></ul></ul>
  14. 15. <ul><li>How To Use Cookies? </li></ul><ul><ul><li>Cookies are sent in the HTTP header of the response. To create a cookie you need to use the Response.Cookies property. To read a cookie value from the cookie use the Request.Cookies property.   The following example shows how to read a cookie named &quot; cookie &quot; and how to write the cookie : </li></ul></ul><ul><ul><li>// it can be different from null only is </li></ul></ul><ul><ul><li>// a request was made before </li></ul></ul><ul><ul><li>if (Request.Cookies[&quot;cookie&quot;] != null) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>   // get the cookie </li></ul></ul><ul><ul><li>   HttpCookie cookie = Request.Cookies[&quot;cookie&quot;]; </li></ul></ul><ul><li>   // get the cookie value </li></ul><ul><li>   string value = Server.HtmlEncode(cookie.Value); </li></ul><ul><li>} </li></ul><ul><li>  </li></ul>
  15. 16. Cookies Limitations The clients can disable cookies . One way to check if cookies are disabled is to write a cookie to the Response and in the next Request to check if the cookie exists. If the cookie doesn't exists you need to assume that cookies are disabled. <ul><li>Most browsers support cookies(Storage Limit) of up to 4096 bytes. This limitation makes the cookie s a way to store only small amount of data. </li></ul><ul><li>Browsers have a limit on the amount of cookies. A web site can save only 20 cookies . </li></ul><ul><li>The user can set the browser to disable cookies. </li></ul><ul><li>Less Secure. </li></ul>Cookies Advantage <ul><li>Does not require server resources. </li></ul><ul><li>Store on Client's machine so performance is high. </li></ul><ul><ul><li>Determine If a Browser Accepts Cookies </li></ul></ul>
  16. 17. View State Client-side State Management What is View State? ViewSate is a technique to preserve controls data and maintain their properties during multiple requests for the same page how, the ViewState property provides a dictionary object for retaining values between multiple requests for the same page. When an ASP.NET page is processed, the current state of the page and controls is hashed into a string and saved in the page as a hidden filed. How to use View State? You can use the ViewState property of the Control or Page class to add values to View State. The ViewState property exposes a dictionary of key and value pairs. For example, the following statement adds the string Hello World! to View State: ViewState(“message”) = “Hello World!”
  17. 18. View State Limitations <ul><li>If this field gets too big, it can have a significant impact on your page’s performance. </li></ul><ul><li>Use Can read view state through page view source . </li></ul>View State Advantage <ul><li>Does not require server resources. </li></ul><ul><li>Technically, User can add an instance of any serializable class to View State </li></ul>
  18. 20. Questions?
  19. 21. Further references Online Books