300 likes | 415 Views
CS 3870/CS 5870. Note04 Post Back. Page Directives. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="OrderingProduct.aspx.vb" Inherits="Lab2_OrderingProduct" %> AutoEventWireup. Lab 2.
E N D
CS 3870/CS 5870 Note04 Post Back
Page Directives <%@ Page Language="VB" AutoEventWireup="false" CodeFile="OrderingProduct.aspx.vb" Inherits="Lab2_OrderingProduct" %> AutoEventWireup
Lab 2 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.
Post Back • User Interaction • Clicking on a button • . . . • User Input • Event Procedures • New version of the same page generated
Page.IsPostBack Property Click Here
Visiting Dynamic Pages • First Visit • Return Visit • Post Back • (Call Back) • (Cross Post Back)
Generating Dynamic Pages • First Visit • No user input • Return Visit • No user input • May need to restore Session Data (Lab2) • Post Back • Event procedures with user input • May access and/or update Session Data
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.
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
Session Time Out • Length of a session • Default value: 30 mins • Set in Web.config file • Discussed later • Session variables are initialed when session starts and updated in event procedures • Session variables could be cleared at session end
Session Variables • Don’t need to be declared • Initialized at the session start • Inside file Global.asax • Accessed and updated inside event procedures • Restored in Page Load event procedure
Creating Global.asax • Must be inside the Web site main folder, not under any sub-folder • Only one global file • Right click Solution or Project • Add • Add New • Global Application Class
Global.asax • Application_Start • Application_End • Application_Error • Session_Start • Session_End
Lab 2 • Non-Post Back • First Visit: No user input • Return Visit: No user input • Restore Session Data • Page Load event procedure • Using initial Session Data for first visit • Post Back • Event procedures with user input • Don’t need to do any thing in Page Load
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
Page Load Event Procedure If Not IsPostBack Then ‘ first or return visits ‘ restore Session data ‘ Else is not needed! End If
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(“Lab2_Input”) = “” Session(“Lab2_Sqrt”) = “” End Sub Sub Session_End(. . .) End Sub </script>
Session Variables Begin with “Lab2_” Sub Session_Start(. . .) Session(“Lab2_Input”) = “” Session(“Lab2_Sqrt”) = “” End Sub Protected Sub Page_Load(…) Handles Me.Load If Not IsPostBack Then TextBox1.Text = Session(“Lab2_Input”) TextBox2.Text = Session(“Lab2_Sqrt”) ‘ Correct for the first time End If End Sub
Update and Restore Session Variables Protected Sub Button1_Click(…) Handles Button1.Click ‘ Computing result Session(“Lab2_Input”) = TextBox1.Text Session(“Lab2_Sqrt”) = TextBox2.Text End Sub Protected Sub Page_Load(…) Handles Me.Load If Not IsPostBack Then TextBox1.Text = Session(“Lab2_Input”) TextBox2.Text = Session(“Lab2_Sqrt”) ‘ Correct all the times End If End Sub
Button Reset • Good input • Bad input • Go to Start page • Return to Order Form • The reset button make it easier • CauseValidation: False!
Button Reset Protected Sub Button2_Click(…) Handles Button2.Click TextBox1.Text = “” TextBox2.Text = “” Session(“Lab2_Input”) = “” Session(“Lab2_Sqrt”) = “” ‘ Make input box not read only End Sub Protected Sub Button1_Click(…) Handles Button1.Click ‘ Computing result ‘ Save Session variables ‘ Make input box read only End Sub
Lab 2 • 6 Session variables • String • Lab2_ID • Lab2_Price • Lab2_Quantity • Lab2_SubTotal • Lab2_Tax • Lab2_GrandTotal • 4 Session variables • String • Lab2_ID • Lab2_Price • Lab2_Quantity • Boolean • Lab2_Computed
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
Web.config <?xml version="1.0"?> <configuration> <system.web> <compilation debug=“true" targetFramework="4.0" /> <system.web> <configuration>
Web.config <?xml version="1.0"?> <configuration> <system.web> <compilation debug=“true" targetFramework="4.5" /> <system.web> <configuration>
Session TimeOut • Configure file Web.config • Short timeout values make development easier <sessionState timeout="1" /> • Longer timeout values for production Web sites
Web.config <?xml version="1.0"?> <configuration> <system.web> <sessionState timeout="1" /> <system.web> <configuration>
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>
Compiling Web Pages • All code files are precompiled into a single assembly. • Web pages are compiled at the first request • Dynamic compilation causes some delay for the first visit
Lab 2 You lose five points for each late day! Even the server is having issues!