36
1 CS 3870/CS 5870 Note04 Session Variables and Post Back

1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Embed Size (px)

Citation preview

Page 1: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

1

CS 3870/CS 5870

Note04

Session Variables and Post Back

Page 2: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Static Pages and Dynamic Pages

• Original HTML– Universal Reader– No User input

• Smart HTML– Input controls – User interactive pages

2

Page 3: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

3

ASP.NET Server Controls

• HTML Server Controls– HTML server controls are HTML tags understood by the

server

• Web Server Controls– Web server controls are special ASP.NET tags understood

by the server.

– According to the browser type and version, Web server controls can be mapped to HTML elements in different ways

• We should use Web Server Controls

• Standard on ToolBox

Page 4: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Windows Programs

• Web applications are different from Windows programs

• Developers’ Code– Sub Main– EXE file– Class variables to maintain state information– Example: employee displayed on

IndividualForm of Prog6 of CS33404

Page 5: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Web Applications

• IIS running as interface

• ASP.NET running to generate dynamic pages

• Developers’ code– No Sub Main– Classes are in DLL files– Objects are created and removed by ASP.NET

(too many objects to store in memory)– Class variables cannot keep state information

5

Page 6: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

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

6

Page 7: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

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.

7

Page 8: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

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 to any page of the site– Session end: no way to know if the user is still

there or not– Session Time Out

8

Page 9: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

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

9

Page 10: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

10

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 11: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

11

Global.asax

• Application_Start

• Application_End

• Application_Error

• Session_Start

• Session_End

Page 12: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Prog2All Session Variables Begin with Prog2_

• 6 Session variables• String

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

12

• 4 Session variables• String

– Prog2_ID– Prog2_Price– Prog2_Quantity

• Boolean

– Prog2_Computed

Page 13: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

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> 13

Page 14: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Session Variables

• Don’t need to be declared

• We need to remember the type of session variables

• Initialized at the session start– Inside file Global.asax

• Accessed and updated inside event procedures

• Restored in Page Load event procedure

14

Page 15: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

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.

15

Page 16: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Update and Restore Session Variables

Protected Sub Button1_Click(…) Handles Button1.Click

‘ Computing result

. . .

‘ Store state information

Session("Lab2_ProductID") = txtID.Text

Session("Lab2_ProductIPrice") = txtPrice.Text

End Sub

Protected Sub Page_Load(…) Handles Me.Load

txtID.Text = Session("Lab2_ProductID")

txtPrice.Text = Session("Lab2_ProductIPrice")

End Sub16

Page 17: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

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

17

Page 18: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Visiting Dynamic Pages

• First Visit

(can use the initial value of session variables)

• Return Visit from other pages

(need to restore the value of session variables)

• Post Back

(keep user input)

• (Call Back)

• (Cross Post Back)18

Page 19: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Post Back

• User Interaction– Clicking on a button– . . .

• User Input

• Event Procedures

• New version of the same page generated

19

Page 20: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Page.IsPostBack Property

Click Here

20

Page 21: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Generating Dynamic Pages

• First Visit– No user input

• Return Visit from other pages– No user input– May need to restore Session Data (Prog2)

• Post Back– Event procedures with user input– May access and/or update Session Data

21

Page 22: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

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

22

Page 23: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

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

23

Page 24: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Update and Restore Session Variables

Protected Sub Button1_Click(…) Handles Button1.Click

‘ Computing result

. . .

Session("Lab2_ProductID") = txtID.Text

Session("Lab2_ProductIPrice") = txtPrice.Text

End Sub

Protected Sub Page_Load(…) Handles Me.Load

If Not IsPostBack Then

txtID.Text = Session("Lab2_ProductID")

txtPrice.Text = Session("Lab2_ProductIPrice")

End If

End Sub24

Page 25: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Button Reset

•The reset button make it easier

•CauseValidation: False!

•Reset session variables

25

Page 26: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

26

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 27: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Web.config

<?xml version="1.0"?>

<configuration>

<system.web>

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

<system.web>

<configuration>

27

Page 28: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Web.config

<?xml version="1.0"?>

<configuration>

<system.web>

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

<system.web>

<configuration>

28

Page 29: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

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" />

29

Page 30: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Web.config

<?xml version="1.0"?>

<configuration>

<system.web>

<sessionState timeout="1" />

<system.web>

<configuration>

30

Page 31: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

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>

31

Page 32: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

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

32

Page 33: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

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 events33

Page 34: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

Style

Display

z-index

Width: 15%

Padding-left: 10%

34

Page 35: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

35

<br /> <br />

<asp:TextBox ID="txtID" runat="server" style=“position: relative; margin-left: 17.5%; width: 15%; display: inline-block" BorderStyle="Solid" AutoPostBack="false" TabIndex="0"></asp:TextBox> <asp:TextBox ID="txtPrice" runat="server" style="position: relative; margin-left:10%; width: 15%; display: inline-block; text-align:right" BorderStyle="Solid" AutoPostBack="false" tabindex="1"></asp:TextBox>

<br /> <br />

Page 36: 1 CS 3870/CS 5870 Note04 Session Variables and Post Back

36

Prog2

You lose five points for each late day!

Even the server is having issues!