1 / 30

CS 3870/CS 5870

CS 3870/CS 5870. Note04 Post Back. Page Directives. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="OrderingProduct.aspx.vb" Inherits="Lab2_OrderingProduct" %> AutoEventWireup. Lab 2.

toki
Télécharger la présentation

CS 3870/CS 5870

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 3870/CS 5870 Note04 Post Back

  2. Page Directives <%@ Page Language="VB" AutoEventWireup="false" CodeFile="OrderingProduct.aspx.vb" Inherits="Lab2_OrderingProduct" %> AutoEventWireup

  3. 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.

  4. Post Back • User Interaction • Clicking on a button • . . . • User Input • Event Procedures • New version of the same page generated

  5. Page.IsPostBack Property Click Here

  6. Visiting Dynamic Pages • First Visit • Return Visit • Post Back • (Call Back) • (Cross Post Back)

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

  8. 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. 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

  10. 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

  11. 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

  12. 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

  13. Global.asax • Application_Start • Application_End • Application_Error • Session_Start • Session_End

  14. 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

  15. 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

  16. Page Load Event Procedure If Not IsPostBack Then ‘ first or return visits ‘ restore Session data ‘ Else is not needed! End If

  17. 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>

  18. 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

  19. 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

  20. Button Reset • Good input • Bad input • Go to Start page • Return to Order Form • The reset button make it easier • CauseValidation: False!

  21. 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

  22. 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

  23. 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

  24. Web.config <?xml version="1.0"?> <configuration> <system.web> <compilation debug=“true" targetFramework="4.0" /> <system.web> <configuration>

  25. Web.config <?xml version="1.0"?> <configuration> <system.web> <compilation debug=“true" targetFramework="4.5" /> <system.web> <configuration>

  26. Session TimeOut • Configure file Web.config • Short timeout values make development easier <sessionState timeout="1" /> • Longer timeout values for production Web sites

  27. Web.config <?xml version="1.0"?> <configuration> <system.web> <sessionState timeout="1" /> <system.web> <configuration>

  28. 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>

  29. 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

  30. Lab 2 You lose five points for each late day! Even the server is having issues!

More Related