State Management.pptx

DrMonikaPatel2 50 views 31 slides Sep 11, 2022
Slide 1
Slide 1 of 31
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31

About This Presentation

Related to how State is managed in ASP.net


Slide Content

State Management Dr. Monika Patel

State Overview Browsers are generally stateless.  Stateless means, whenever we visit a website, our browser communicates with the respective server depending on our requested functionality or the request. The browser communicates with the respective server using the HTTP or HTTPs protocol. But after that response, what's next or what will happen when we visit that website again after closing our web browser? In this case HTTP/HTTPs doesn't remember what website or URL we visited. In other words we can say it doesn't hold the state of a previous website that we visited before closing our browser, that is called stateless.

State Management State management is very important and useful in ASP.NET. ASP.NET State management is a preserve state control and object in an application because ASP.NET web applications are stateless. A new instance of the Web page class is created each time the page is posted to the server. If a user enters information into a web application, that information would be lost in the round trip from the browser (MSDN). State management maintains and stores the information of any user till the end of the user session.

Types of State Management

Client-Side State Management Whenever we use Client-Side State Management, the state related information will directly get stored on the client-side. That specific information will travel back and communicate with every request generated by the user then afterwards provides responses after server-side communication.

Continue…

Server-Side State Management In Server-Side State Management all the information is stored in the user memory. Due to this functionality there is more secure domains at the server side in comparison to Client-Side State Management.

Continue…

Client-side Techniques Client-Side State Management techniques are, View State Hidden field Cookies Control State Query Strings

ViewState ViewState is a important client side state management technique. View State is the method to preserve the Value of the Page and Controls between round trips. Preserving the value of page controls between client to server and server to client is called  "roundtrip" . It stores the page or control value in the mode of hidden fields.

Continue… The ViewState is a dictionary kind of object which stores the value on the basis of KEY/VALUE. By default, the ViewState is enabled, but we can enable or disable ViewState on Page level or Control level. There are two properties on Page Level ViewStateMode EnableVIewState ViewStateEncryptionMode = “Always” / “Auto” / “Never” There is one property on Control Level. EnableViewState = “True” / “False”

Continue… Each web page and the controls on the page have the EnableViewState property. User can set ViewState on/off for each control using EnableViewState property. ASP.NET framework uses the ViewState property to automatically save the values of the Web page and each control on the Web page prior to rendering the page. User can also disable ViewState for the entire page by adding EnableViewState = false to @page directive.

Enable/Disable Viewstate <%@ Page Language="C#" AutoEventWireup ="true" CodeFile =“ WebStud.aspx.cs " EnableViewState ="true" Inherits="WebApplication1.Student" %>

How to store the value in ViewState ? ViewState stores the value in a Key/Value pair basis. Syntax ViewState [“ KeyName ”] = Object / String  .etc.

Data Types Can Store In Viewstate Integers String Boolean Array/ ArrayList Hash Table

Features Of View State Retains the value of the Control after post-back without using a session. Stores the value of Pages and Control Properties defined in the page. Creates a custom View State Provider that lets you store View State Information in a SQL Server Database or in another data store.

Advantages of View State Easy to Implement. No server resources are required: The View State is contained in a structure within the page load. Enhanced security features: It can be encoded and compressed or Unicode implementation.

Disadvantages of View State Security Risk: The Information of View State can be seen in the page output source directly. You can manually encrypt and decrypt the contents of a Hidden Field, but It requires extra coding. If security is a concern then consider using a Server-Based state Mechanism so that no sensitive information is sent to the client. Performance: Performance is not good if we use a large amount of data because View State is stored in the page itself and storing a large value can cause the page to be slow. Device limitation: Mobile Devices might not have the memory capacity to store a large amount of View State data. It can store values for the same page only.

When We Should Use View State When the data to be stored is small. Try to avoid secure data.

Querystring A query string is one of the techniques in Web applications to send data from one webform to another through the URL. A query string consists of two parts, field and value, and each of pair separated by ampersand (&). The ?(question mark) in a query string indicates the beginning of a query string and it's value. There is a limit on the Query string length. Hence, Query strings cannot be used to send very long data. Some browsers and client devices impose a 2083 character limit on the length of the URL. Most browsers impose a limit of 255 characters on URL length. Query strings are visible to the user, hence should not be used to send sensitive information such as a username and password but we can also encrypt query values.

Advantages Simple to Implement No server resources are required Widespread support Almost all browsers and client devices support using query strings to pass values. Query strings contain the HTTP request for a specific URL.

Disadvantages Cross paging functionality makes it redundant Easily modified by end user Potential security risks due to Human Readable: 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.

Sending Only One Querystring Value Sender Page: Response.Redirect (“ PageName+Keyname =Value”); Receiver Page: Str = Request.QueryString [“ Keyname ”];

Sending More Than One Querystring Value Sender Page: Response.Redirect (“PageName+Keyname1=Value1&KeyName2=Value2”); Receiver Page: Str1 = Request.QueryString [“Keyname1”]; Str2 = Request.QueryString [“Keyname2”];

Cookies A cookie is a small amount of data that is stored either in a text file on the client file system or in-memory in the client browser session. This file is located on client machines "C:\Document and Settings\ Currently_Login user\Cookie" path.  It is used to store user preference information like Username, Password,City and PhoneNo etc on client machines. We need to import namespace called  Systen.Web.HttpCookie before we use cookie.

Continue… Usually a cookie can have a maximum size of 4KB. A particular named cookie can store a single value or a collection of name/value pairs. Each cookie must have a unique name. User can set a cookie’s date and time expiration. If user does not set the cookie’s expiration, the cookie is created but it is not stored on the user’s hard disk.

Types of Cookies There are two types of cookies: Persistence Cookie Non-Persistence Cookie 1. Persistence Cookie Cookies which have an expiry date time are called persistence cookies. This types of cookies are stored on user’s hard drive permanently till the date time we set.

Continue… 2. Non-Persistence Cookie This types of cookies are not permanently stored on user hard drive. It stores the information up the user accessing the same browser. When user close the browser the cookies will be automatically deleted.

Continue… Store Value: HttpCookie Cookiename = new HttpCookie (“ Storecookiename ”); Cookiename.Values [“ Keyname ”] = Value; Cookiename.Expires = DateTime.Now.AddDays (1); Response.Cookies.Add ( Cookiename ); Fetch Value: Variable name = Request.Cookies [“ Storecookiename ”].Values[" KeyName "]. ToString ();

Advantages Its clear text so user can able to read it. We can store user preference information on the client machine. Its easy way to maintain. Fast accessing.

Disadvantages Size limitations Most browsers place a 4096-byte limit on the size of a cookie, although support for 8192-byte Most browsers allow only 20 cookies per site; if you try to store more, the oldest cookies are discarded. Cookies can be disabled on user browsers Cookies are transmitted for each HTTP request/response causing overhead on bandwidth. Inappropriate for sensitive data due to security is not provided.
Tags