+ All Categories
Home > Documents > CS 3870/CS 5870

CS 3870/CS 5870

Date post: 07-Jan-2016
Category:
Upload: toki
View: 22 times
Download: 0 times
Share this document with a friend
Description:
CS 3870/CS 5870. Note04 Post Back. Page Directives. AutoEventWireup. Lab 2. - PowerPoint PPT Presentation
32
1 CS 3870/CS 5870 Note04 Session Variables and Post Back
Transcript
Page 1: CS 3870/CS 5870

1

CS 3870/CS 5870

Note04

Session Variables and Post Back

Page 2: CS 3870/CS 5870

Static Pages and Dynamic Pages

• Original HTML– Universal Reader– No User input

• Smart HTML– Input controls – User interactive pages

2

Page 3: CS 3870/CS 5870

Windows Programs

• Developers’ Code– Sub Main– EXE file– Class variables to maintain state information– Example: values in textboxes on a form

3

Page 4: CS 3870/CS 5870

ASP.NET Web Applications

• IIS running as interface

• ASP.NET running to generate dynamic pages

• Developers’ code– No Sub Main– Page classes are in DLL files– Pages are created and removed for each request– Page class variables cannot keep state information

4

Page 5: CS 3870/CS 5870

Prog2

• Visit the ordering page

• Calculate result

• Go to page Default

• Come back to the ordering page– Data lost!

5

Page 6: CS 3870/CS 5870

Prog2

After a calculation is completed successfully, if the user goes to the start page then comes back to page OrderingProduct.aspx, then all textboxes should show the same data as when the user left the page.

All textboxes will be blank if the previous calculation is failed.

6

Page 7: CS 3870/CS 5870

Generating Dynamic Pages

• First Visit– No user input– Generating pages based on the DLL file

• Post Back– Requesting the same page– With user input

• Return Visit from other pages– No information on the visited page

7

Page 8: CS 3870/CS 5870

Session Variables

• To maintain state information for each user on each Web site

• Maintained by ASP.NET

• Not by developers’ code

• Defined inside Global.asax

8

Page 9: CS 3870/CS 5870

Session State

ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. HTTP is a stateless protocol. This means that a Web server treats each HTTP request for a page as an independent request. The server retains no knowledge of variable values that were used during previous requests. ASP.NET session state identifies requests from the same browser during a limited time window as a session, and provides a way to persist variable values for the duration of that session. By default, ASP.NET session state is enabled for all ASP.NET applications.

9

Page 10: CS 3870/CS 5870

What is a Session?• Running a Windows program

– The time period between starting a program and terminating the program

• Session of Web Sites– Session start: the first visit of a user to any page of

the site– Cookies to identify the users– Session end: no way to know if the user is still

there or not– Session Time Out

10

Page 11: CS 3870/CS 5870

Session Time Out

• Length of a session– Default value: 30 mins– Set in Web.config file– Discussed later

• Session variables are initialized when session starts and updated in event procedures

• Session variables could be cleared at session end

11

Page 12: CS 3870/CS 5870

Visiting Dynamic Pages

• First Visit– Can use the initial value of session variables

• Post Back– Use user input– Don’t use session variables to overwrite user input

• Return Visit from other pages– Need to restore the value of session variables

12

Page 13: CS 3870/CS 5870

Page.IsPostBack Property

Click Here

13

Page 14: CS 3870/CS 5870

Session Variables

• Don’t need to be declared• We need to remember the type of session variables• Initialized inside Session_Start of Global.asax• Accessed and updated inside event procedures• Restored in Page Load event procedure

14

Page 15: CS 3870/CS 5870

15

Creating Global.asax

• Must be inside the Web site main folder, not under any sub-folder

• Only one global file each Web site• Right click Solution or Project• Add• Add New• Global Application Class

Page 16: CS 3870/CS 5870

16

Global.asax

• Application_Start

• Application_End

• Application_Error

• Session_Start

• Session_End

Page 17: CS 3870/CS 5870

Prog2All Session Variables Begin with Prog2_

• 6 Session variables• String

– Prog2_ID– Prog2_Price– Prog2_Quantity– Prog2_SubTotal– Prog2_Tax– Prog2_GrandTotal

17

• 4 Session variables• String

– Prog2_ID– Prog2_Price– Prog2_Quantity

• Boolean

– Prog2_Computed

Page 18: CS 3870/CS 5870

Initialize Session Variables<%@ Application Language="VB" %>

<script runat="server">

Sub Application_Start(. . .)

End Sub

Sub Application_End(. . .)

End Sub

Sub Application_Error(. . .)

End Sub

Sub Session_Start(. . .)

Session("Prog2_ProductID") = ""

Session("Prog2_ProductIPrice") = ""

Session("Prog2_ProductQuantity") = ""

Session("Prog2_Computed") = False

End Sub

Sub Session_End(. . .)

End Sub

</script> 18

Page 19: CS 3870/CS 5870

Update Session Variables

Protected Sub Button1_Click(…) Handles Button1.Click

‘ Exit if invalid input

‘ Computing result

. . .

Session("Lab2_ProductID") = txtID.Text

Session("Lab2_ProductIPrice") = txtPrice.Text

Session("Prog2_Computed") = True

End Sub

19

Page 20: CS 3870/CS 5870

Page Load Event Procedure

If Not IsPostBack Then

‘ first or return visits

‘ restore Session data

Else

‘ Let event procedures

‘ handle the user input

End If

20

Page 21: CS 3870/CS 5870

Page Load Event Procedure

If Not IsPostBack Then

‘ first or return visits

‘ restore Session data

‘ Else is not needed!Else is not needed!

End If

21

Page 22: CS 3870/CS 5870

Restore Session Variables

Protected Sub Page_Load(…) Handles Me.Load

If Not IsPostBack Then

‘If Session("Prog2_Computed") = True Then

txtID.Text = Session("Lab2_ProductID")

txtPrice.Text = Session("Lab2_ProductIPrice")

‘End If

End If

End Sub

22

Page 23: CS 3870/CS 5870

User Input Will Be Lost!

‘ For each request, the event procedure will be invoked

‘ and we never get the user input.

Protected Sub Page_Load(…) Handles Me.Load

txtID.Text = Session("Lab2_ProductID")

txtPrice.Text = Session("Lab2_ProductIPrice")

End Sub

23

Page 24: CS 3870/CS 5870

Button Reset

•The reset button make it easier

•CauseValidation: False!

•Reset session variables

24

Page 25: CS 3870/CS 5870

25

Web Configuration File

• Creating Web.config file– Right click the web site

– Add new item

– Web Configuration file

• Many settings

• Different levels– Machine– Site– Folder

Page 26: CS 3870/CS 5870

Web.config

<?xml version="1.0"?>

<configuration>

<system.web>

<compilation debug=“true" targetFramework="4.0" />

<system.web>

<configuration>

26

Page 27: CS 3870/CS 5870

Web.config

<?xml version="1.0"?>

<configuration>

<system.web>

<compilation debug=“true" targetFramework="4.5" />

<system.web>

<configuration>

27

Page 28: CS 3870/CS 5870

Session TimeOut

• Configure file Web.config

• Short timeout values make development easier

<sessionState timeout="1" />

• Longer timeout values for production Web sites

<sessionState timeout=“30" />

28

Page 29: CS 3870/CS 5870

Web.config

<?xml version="1.0"?>

<configuration>

<system.web>

<sessionState timeout="1" />

<system.web>

<configuration>

29

Page 30: CS 3870/CS 5870

Web.config<?xml version="1.0"?>

<configuration>

<system.web>

<compilation debug="true" targetFramework="4.5"

urlLinePragmas="true“ />

<customErrors mode="Off"/>

<sessionState timeout="1" />

<system.web>

<configuration>

30

Page 31: CS 3870/CS 5870

Compiling Web Pages

• All code files are precompiled into a single assembly (dll file).

• Web pages are compiled at the first request

• Dynamic compilation causes some delay for the first visit

31

Page 32: CS 3870/CS 5870

Page Directives<%@ Page Language="VB" AutoEventWireup="false"

CodeFile="OrderingProduct.aspx.vb"

Inherits="Prog2_OrderingProduct" %>

AutoEventWireupTrue: No “handles” for page event procedures

(ASP.NET uses procedure name and signature)

False: page event procedures must have “handles”

Only for page events, not control events

32


Recommended