+ All Categories
Home > Documents > State Management in ASP.NET

State Management in ASP.NET

Date post: 24-May-2015
Category:
Upload: shyam-chawda
View: 198 times
Download: 0 times
Share this document with a friend
Description:
Session , QueryString , Cookies, Hidden Filed , Application , etc.
Popular Tags:
75
ASP.NET State management 1 Shyam N. Chawda 9374928879
Transcript
Page 1: State Management in ASP.NET

ASP.NETState management

1Shyam N. Chawda 9374928879

Page 2: State Management in ASP.NET

Shyam N. Chawda 9374928879

Welcome my dear students2

Page 3: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 4: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 5: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 6: State Management in ASP.NET

Shyam N. Chawda 9374928879

What is State management?

So, how do we make web pages which will remember about the user ?

6

Page 7: State Management in ASP.NET

Shyam N. Chawda 9374928879

What is State management?

Client-Based

Server-Based

7

Page 8: State Management in ASP.NET

Shyam N. Chawda 9374928879

What is State management?

Client-Based

8

Page 9: State Management in ASP.NET

Shyam N. Chawda 93749288799

Client side

Page 10: State Management in ASP.NET

Shyam N. Chawda 9374928879

What is State management?

Server-Based

10

Page 11: State Management in ASP.NET

Shyam N. Chawda 937492887911

Server side

Application state

DatabaseSession state

Page 12: State Management in ASP.NET

Shyam N. Chawda 9374928879

Client Side state management

Query String

For understand it you must aware about Request and Response object

12

Page 13: State Management in ASP.NET

Shyam N. Chawda 9374928879

Response Object

You can use this class to inject text into the page, jump to other page etc.

Write

Redirect

13

Page 14: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 15: State Management in ASP.NET

Shyam N. Chawda 9374928879

Request Object

Request.Browser.Browser

Request.QueryString()

15

Page 16: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 17: State Management in ASP.NET

Shyam N. Chawda 9374928879

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.TextRequest.QueryString("Name")

17

Page 18: State Management in ASP.NET

Shyam N. Chawda 9374928879

Query string

Response.redirect(“Formname?” & qry); Response.redirect(“Formname?name=” & value);

18

Page 19: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 20: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 21: State Management in ASP.NET

Shyam N. Chawda 9374928879

More than one Query string

Using & Dim str As String str = "PageRequest.aspx?Name=" & TextBox1.Text & "&" & "Lname=" & TextBox2.Text

21

Page 22: State Management in ASP.NET

Shyam N. Chawda 9374928879

View State

View State is used to remember controls state when page is posted back to server.

You can store page-specific information

22

Page 23: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 24: State Management in ASP.NET

Shyam N. Chawda 9374928879

View State24

Page 25: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 26: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 27: State Management in ASP.NET

Shyam N. Chawda 9374928879

View State

Take 2 Buttons and 1 TextBoxTake 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

Page 28: State Management in ASP.NET

Shyam N. Chawda 9374928879

View State

no = 5

ViewState("No") = no

TextBox1.Text = ViewState ("No")

28

Page 29: State Management in ASP.NET

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)

Page 30: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 31: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 32: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 33: State Management in ASP.NET

Shyam N. Chawda 9374928879

View State

Device limitations   

Mobile devices might not have the memory capacity to store a large amount of view-state data.

33

Page 34: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 35: State Management in ASP.NET

Shyam N. Chawda 937492887935

Page 36: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 37: State Management in ASP.NET

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

Page 38: State Management in ASP.NET

Hidden Fields

Simple implementation   

Hidden fields are standard HTML controls that require no complex programming logic.

38

Shyam N. Chawda 9374928879

Page 39: State Management in ASP.NET

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

Page 40: State Management in ASP.NET

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

Page 41: State Management in ASP.NET

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

Page 42: State Management in ASP.NET

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

Page 43: State Management in ASP.NET

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

Page 44: State Management in ASP.NET

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

Page 45: State Management in ASP.NET

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

Page 46: State Management in ASP.NET

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

Page 47: State Management in ASP.NET

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

Page 48: State Management in ASP.NET

Gets - Cookies

If Not Request.Cookies("lastVisit") Is Nothing Then txtNameR.Text =

Request.Cookies("lastVisit").Values("Name").ToStringtxtDateR.Text =

Request.Cookies("lastVisit").Values("Dt").ToStringEnd if

48

Shyam N. Chawda 9374928879

Page 49: State Management in ASP.NET

Cookies

Advantages

No server resources are required   

Simplicity   

The cookie is a lightweight, text-based structure.

49

Shyam N. Chawda 9374928879

Page 50: State Management in ASP.NET

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

Page 51: State Management in ASP.NET

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

Page 52: State Management in ASP.NET

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

Page 53: State Management in ASP.NET

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

Page 54: State Management in ASP.NET

What is State Management?

Server-Based

Application stateSession stateDatabase

54

Shyam N. Chawda 9374928879

Page 55: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 56: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 57: State Management in ASP.NET

Shyam N. Chawda 9374928879

Application State

Goto Global.asax

Find Counter : Session_Start

Application(“Cnt”)= Application(“Cnt”)= +1

57

Page 58: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 59: State Management in ASP.NET

Shyam N. Chawda 9374928879

Application State

Advantages

Simple implementation   Application state is easy to use

59

Page 60: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 61: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 62: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 63: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 64: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 65: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 66: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 67: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 68: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 69: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 70: State Management in ASP.NET

Shyam N. Chawda 9374928879

Session State

Disadvantage

Performance considerations   

Session-state variables stay in memory until they are either removed or replaced.

70

Page 71: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 72: State Management in ASP.NET

Shyam N. Chawda 9374928879

Database

Advantages

Security   

Access to databases requires rigorous authentication and authorization.

72

Page 73: State Management in ASP.NET

Shyam N. Chawda 9374928879

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

Page 74: State Management in ASP.NET

Shyam N. Chawda 9374928879

Database

Disadvantage

Complexity   

Using a database to support state management requires that the hardware and software configurations be more complex.

74

Page 75: State Management in ASP.NET

Shyam N. Chawda 9374928879

Thanks

75


Recommended