Session Variables and Post Back

Slides:



Advertisements
Similar presentations
1 CS 3870/CS 5870: Lab4 Save with Invalid Price Keep Order After Updating.
Advertisements

11 Getting Started with ASP.NET Beginning ASP.NET 4.0 in C# 2010 Chapters 5 and 6.
11 ASP.NET Controls II Beginning ASP.NET 4.0 in C# 2010 Chapter 6.
Anatomy of an ASP.NET Page. Slide 2 My Version of the Big Picture (1) ASP Worker (Engine) Your application Runs Server Other applications User 1User 2.
Web Development in Microsoft Visual Studio Slide 2 Lecture Overview Introduce Visual Studio 2013 Create a first ASP.NET application.
Basics of ASP.NET. 2 © UW Business School, University of Washington 2004 Outline Installing ASP.NET and Web Matrix Data Types Branching Structure Procedures.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
IT533 Lectures Configuring, Deploying, Tracing and Error Handling.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
1 CS 3870/CS 5870 Static and Dynamic Web Pages ASP.NET and IIS.
1 CS 3870/CS 5870 Static and Dynamic Web Pages ASP.NET and IIS.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College
Overview of Previous Lesson(s) Over View  Server controls are small building blocks of the graphical user interface, which includes  Text boxes  Buttons.
Beginning Web Site Development Module 1 – Dynamic Web Site Development Fundamentals of building dynamic Web sites with ASP.NET 2.0 and C# Version.
Advanced Web Forms with Databases Programming Right from the Start with Visual Basic.NET 1/e 13.
Creating Web Applications Using ASP.NET Chapter Microsoft Visual Basic.NET: Reloaded 1.
Session 10: Managing State. Overview State Management Types of State Management Server-Side State Management Client-Side State Management The Global.asax.
State Management. What is State management Why State management ViewState QueryString Cookies.
© Minder Chen, ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. Framework Base Class Library ADO.NET: Data & XML.
ASP.NET application. Roadmap ASP.NET file types Bin directory Application updates Simple application from start to finish using a virtual directory Behind.
The Web Architecture and ASP.NET. Slide 2 Review of the Web (1) It began with HTTP and HTML, which delivers static Web pages to browsers which would render.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Murach’s ASP.NET 4.0/VB, C1© 2006, Mike Murach & Associates, Inc.Slide 1.
1 CS 3870/CS 5870: Note 11 Authentication and Authorization Membership Provider.
Caching Chapter 12. Caching For high-performance apps Caching: storing frequently-used items in memory –Accessed more quickly Cached Web Form bypasses:
1 CS 3870/CS 5870 Note04 Session Variables and Post Back.
Dr. Azeddine Chikh IS444: Modern tools for applications development.
Christopher M. Pascucci Basic Structural Concepts of.NET Managing State & Scope.
Global.asax file. Agenda What is Global.asax file How to add the Global.asax file What are the default events available Explanation to Application_Level.
1 CS 3870/CS 5870: Note 07 Lab 3 Lab 4 Test 1: Two Tables.
STATE MANAGEMENT.  Web Applications are based on stateless HTTP protocol which does not retain any information about user requests  The concept of state.
Module 7: Creating a Microsoft ASP.NET Web Application.
BIT 285: ( Web) Application Programming Lecture 06: Thursday, January 22, 2015 Page Object, The Lifetime of a Web Application, Application State and Session.
1 CS 3870/CS 5870: Note05 Prog3 Web Application with Database.
The Problem of State. We will look at… Sometimes web development is just plain weird! Internet / World Wide Web Aspects of their operation The role of.
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
1 CS 3870/CS 5870: Note 20 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The.
Module 4: Creating a Web Application with Web Forms
1 CS387/CS587: Note05 Lab 3. 2 Global.asax Must not be under any sub-folder Application_Start Application_End Application_Error Session_Start Session_End.
1 CS 3870/CS 5870: Note 12 Authentication and Authorization Membership Provider.
Working With ASP.NET Application. Create a new virtual directory The procedure to create a new virtual directory Internet Services Manager Right click.
ASP.NET P AGE O BJECTS.  Each ASP.NET page inherits the PAGE object  The PAGE supplies 3 built in objects:  REQUEST: All information passed to the.
MIS 3200 – Unit 3.2 Page_Load method AutoPostBack property of controls IsPostBack method of the Page object Understanding web page life cycle.
1 CS 3870/CS 5870: Note 16 Web User Controls. Prog 7 Copy Prog6 to Prog7 Modify all files for Prog7 Remove Web.config from sub-folders Make sure Prog7.
1 CS387/CS587: Note04 Lab 3. 2 Master Page All Web pages will be similar Should be created before other web pages Add New Items Controls on the Master.
CIS 375—Web App Dev II ASP.NET 5 Events. 2 The Page_Load Event The Page_Load event is triggered when a page loads. [Example]Example Sub Page_Load lbl1.Text="The.
1 CS 3870/CS 5870: Note 14. Prog5 Due 10 PM Wednesday, Oct 21 Authentication and Authorization 2.
1 CS 3870/CS 5870: Note 19 AJAX Prog8. Test 2 50 Points Thursday, Nov 12 2:00 PM – 4:00 PM Lab
ASP.NET WEB Applications. ASP.NET  Web application framework developed by Microsoft  Build dynamic data driven web applications and web services  Subset.
Sergey Sidorov PhD student, computer software chair Computer Science department.
Introduction to ASP.NET, Second Edition2 Chapter Objectives.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
Creating an Interactive Web Page Lesson 2. Objectives.
Computing with C# and the .NET Framework
CS 3870/CS 5870 Web Service.
A variable is a name for a value stored in memory.
CS 3870/CS 5870 Web User Controls.
Web Application Web Application are programs that can be executed either on a Web server or in a Web browser. An Online store accessed through a browser.
Web Application with Dataase
ASP.NET Application Framework
Unit 27 - Web Server Scripting
Web Development in Microsoft Visual Studio 2013
CS 3870/CS 5870 Web User Controls Events (II).
Application Infrastructure
Anatomy of an ASP.NET Page
Module 10: Creating a Web Application with Web Forms
Static and Dynamic Web Pages
ASP.NET 4.0 State Management Improvements – Deep Dive
Cookies A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer.
MIS 3200 – Unit 6.1 Moving between pages by redirecting
Presentation transcript:

Session Variables and Post Back 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 Developers’ Code Sub Main EXE file Class variables to maintain state information Example: values in textboxes on a form

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

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

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.

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

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

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 Session of Web Sites 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

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

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

Page.IsPostBack Property Click Here

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

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

Global.asax Application_Start Application_End Application_Error Session_Start Session_End

Prog2 All Session Variables Begin with Prog2_ 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

Initialize Session Variables <%@ Application Language="VB" %> <script runat="server"> Sub Application_Start(. . .) End Sub Sub Application_End(. . .) Sub Application_Error(. . .) Sub Session_Start(. . .) Session("Prog2_ProductID") = "" Session("Prog2_ProductIPrice") = "" Session("Prog2_ProductQuantity") = "" Session("Prog2_Computed") = False Sub Session_End(. . .) </script>

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

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

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

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

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

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

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

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

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

Web.config <?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.5" urlLinePragmas="true“ /> <customErrors mode="Off"/> <sessionState timeout="1" />

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

Page Directives AutoEventWireup <%@ Page Language="VB" AutoEventWireup="false" CodeFile="OrderingProduct.aspx.vb" 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