1 / 31

CS 3870/CS 5870

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. Windows Programs. Sub Main Programs are compiled to EXE files

anjanettep
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 Session Variables and Post Back

  2. Static Pages and Dynamic Pages • Original HTML • Universal Reader • No User input • Smart HTML • Input controls • User interactive pages

  3. Windows Programs • Sub Main • Programs are compiled to EXE files • Variables to maintain state information • Example: values in textboxes on a form

  4. ASP.NET Web Applications • IIS running as interface • ASP.NET running to generate dynamic pages • No Sub Main • Page classes are compiled to in DLL files • Pages are created and removed for each request • Page class variables cannot keep state information

  5. Prog2 • Visit the ordering page • Calculate result • Go to page Default • Come back to the ordering page • Data lost!

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

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

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

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

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

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

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

  13. Page.IsPostBack Property Click Here

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

  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 Item • C# • Global Application Class

  16. Global.asax <%@ Application Language="C#" %> <script runat="server"> void Application_Start(object sender, EventArgs e) { } void Application_End(object sender, EventArgs e) { } void Application_Error(object sender, EventArgs e) { } void Session_Start(object sender, EventArgs e) { } void Session_End(object sender, EventArgs e) { } </script>

  17. Prog2All Session Variables Begin with Prog2_ • 6 Session variables • String • Prog2_ID • Prog2_Price • Prog2_Quantity • Prog2_SubTotal • Prog2_Tax • Prog2_GrandTotal • 4 Session variables • String • Prog2_ID • Prog2_Price • Prog2_Quantity • Boolean • Prog2_Computed

  18. Initialize Session Variables <%@ Application Language="C#" %> <script runat="server"> . . . void Session_Start(object sender, EventArgs e) { //Prog2 Session["Prog2_ProductID"] = ""; Session["Prog2_ProductPrice"] = ""; Session["Prog2_ProductQuantity"] = ""; Session["Prog2_Computed"] = false; } </script>

  19. Update Session Variables protected void btnCompute_Click(object sender, EventArgs e) { CalculateTotals(); Session["Prog2_ProductPrice"] = txtPrice.Text; Session["Prog2_ProductQuantity"] = txtQuantity.Text; Session["Prog2_ProductID"] = txtID.Text; Session["Prog2_Computed"] = true; } protected void btnReset_Click(object sender, EventArgs e) . . .

  20. Page Load Event Procedure If Not IsPostBack first or return visits when to restore Session data? Computed! Else Let event procedures handle the user input

  21. Page Load Event Procedure If Not IsPostBack & Computed // restore Session data // Else is not needed! End If

  22. Restore Session Variables protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack && (bool)Session["Prog2_Computed"]) { txtID.Text = (string)Session["Prog2_ProductID"]; txtQuantity.Text = (string)Session["Prog2_ProductQuantity"]; txtPrice.Text = (string)Session["Prog2_ProductPrice"]; CalculateTotals(); } txtID.Focus(); }

  23. User Input Will Be Lost! // For each request, the event procedure will be invoked // and we never get the user input. protected void Page_Load(object sender, EventArgs e) { txtID.Text = (string)Session["Prog2_ProductID"]; txtQuantity.Text = (string)Session["Prog2_ProductQuantity"]; txtPrice.Text = (string)Session["Prog2_ProductPrice"]; . . . } Must Check PostBack!

  24. Button Reset • The reset button make it easier • CauseValidation: False! • Reset session variables

  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

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

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

  28. 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. 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. 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 Directives <%@ Page Language=“C#" AutoEventWireup=“true" CodeFile="OrderingProduct.aspx.cs" Inherits="Prog2_OrderingProduct" %> AutoEventWireup True: 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

More Related