270 likes | 499 Views
Programming the Web Using ASP.Net. Chapter 5: Applications and Sessions Dave Mercer. Explain how an application or session affects an ASP.Net Web application List the events associated with applications and sessions List the properties, methods, and events of the HttpApplication class.
E N D
Programming the Web Using ASP.Net Chapter 5:Applications and Sessions Dave Mercer McGraw-Hill/Irwin
Explain how an application or session affects an ASP.Net Web application List the events associated with applications and sessions List the properties, methods, and events of the HttpApplication class List the properties, methods, and events of the HttpSession class Describe how the Global.asax file works Build code in application event handlers Build code in session event handlers Explain why to add code to application and session events Objectives
State • State refers to the status or condition of something that can change over time. • State is important, because changes in state can affect how we interact with programs and data. • In Windows applications, the current state of the application is always available to your code. In Web applications, the stateless nature of the HTTP protocol requires you to work a little harder to maintain and manage state.
State and Database Applications • For database applications, the state of the application can be valid or not. • For example, in a database there is a condition called referential integrity. • Referential integrity means that if a record in one table is required to refer to a record in another table, then the record being referred to cannot be removed without also removing all records referring to it.
ASP.Net Web Application State • The state of an ASP.Net Web application at any given time can be defined as the current condition of all those files plus the changes that have been made by users of the application. • As the developer, if you make design changes – even while the application is running – you are redefining the application (making a new one) and setting state back to its original values for a new application.
Maintaining State • In some cases, data must be maintained across processing occurring on a single page or across many page requests from a single user, or across all the pages and requests of all the users working with the application. • Applications and sessions provide this mechanism for maintaining state across your Web application. The Global.asax file is the developer’s access point for doing so.
The Stateless Nature of the Web • Communications on the Web are stateless, meaning there is no mechanism intrinsic to HTTP for managing state on the Web. • ASP.Net’s application and session classes provide such a mechanism for the server, but there are also methods for using the client to provide state information. • Client-side methods are important because the user can make multiple changes to controls rendered on the browser without in any way notifying the server.
Client-Side State Management • Developers employ several methods for maintaining and managing state on the client. These methods include: • The View State Property: an intrinsic property of .Net forms and controls • Hidden Form Fields: an HTML form field that is not displayed on the page • Cookies: short strings of data stored on the user's browser • Query Strings: name/value pairs attached to a clickable URL
The View State Property • An ASP.Net Web Forms Page and each of the controls on it has a ViewState property. • When the page is processed and sent to the user, the current state of all its controls is turned into a string and stored on the page in a hidden form field. For example: <input type="hidden" name="__VIEWSTATE" value="dDwxMDQ5NjEwMDE2Ozs+" /> • When the user submits the form, the value of the hidden form field is automatically sent as well.
Hidden Form Fields • Hidden form fields are created when you add a type attribute with the value “hidden” to an <input> tag in an HTML form: <input type="hidden" name="whatever" value="true"> • The name and value you give your hidden form field can be retrieved when the user submits the form. • You can maintain state across multiple page submissions this way, although the values readily appear in the Web page’s source code.
Cookies and Query Strings • Cookies are small strings of data stored on the user’s computer that can also be used to maintain state data. They can be set to expire immediately or after a very long life. However, not all users allow cookies to be set. • Query strings carry name/value pairs in which state data can be maintained, much like hidden form fields. They may be used in clickable links but not for form submissions.
Applications and Sessions • The client-side state mechanisms you’ve just seen can maintain state data across multiple page requests for the same page. • To maintain state data across page requests for different pages, or across multiple users of the application, you’ll need server-side state management mechanisms. These include: • applications (for multiple users), and • sessions (for different page requests by the same user).
Application and Session Events • When the first user requests the first page of your Web application, the application starts. Each user that requests a page from the application also starts a Session. • The start of the application and the start of each session trigger the Application and Session Start events. • These events have event handlers in which code can be placed.
Ending Applications and Sessions • Sessions end when they timeout or a user has disconnected, or when they are explicitly abandoned. • Applications end when the Application_End event occurs. This happens when the last user’s Session times out or is ended.
The Global.asax File • When you make an ASP.Net Web application using the Visual Basic projects template, a Global.asax file is created automatically. This file comes in handy for responding to events that occur for the application as a whole, or for the session as a whole. • If you make changes to Global.asax, ASP.Net will detect them and complete all existing requests before closing and restarting the application.
Event Handlers in Global.asax The Global.asax file contains event handlers in which you may place code for initializing your application, and for initializing individual sessions:
Event Handlers in Global.asax (2) • In addition to the Start and End events for applications and sessions, there are other events handlers in the Global.asax file that code can be added to. These include: • Application_BeginRequest • Application_AuthenticateRequest, and • Application_Error
The Intrinsic Application and Session Objects • An instance of the HttpApplicationState class is created when an application starts. • This instance is exposed as a property of the HttpApplication class named “Application.” • This class also exposes a property named “Session” that gets the HttpSessionState for the current request. • These objects are similar in nature to the ASP Application and Session objects, and can be worked with in a very similar fashion.
Adding Variables to an Application or Session • One of the most common uses for Application and Session objects is to contain values in variables across the entire application or an individual session. • You can create and initialize application and session variables in the Global.asax file by placing code in the appropriate event handler.
ASP.Net Sessions • When a new user makes a request for a page in your application, a 120-bit SessionID is generated. • This ID is unique and allows your application to identify and maintain state for a user while they are connected to the application. • The SessionID is stored as a cookie, or is automatically included on every link in the URL, depending upon how you configure your application.
ASP.Net Sessions (2) • Session variables, values and objects are stored in a dictionary-based, in-memory cache. • You can use out-of-proc mode to store session state in memory, or SQL mode to store session state in a SQL Server database. • A State Server for each processor or Web farm supports processes requiring session state.
Contents and StaticObjects Collections • The Session object has a Contents and a StaticObjects collection that can be accessed as properties. • The Contents collection contains all items that have been added to the session via code. • The StaticObjects collection contains all objects that have been added using the <object runat="server"> tag, with session scope. • These collections provide convenient access points to add, edit, and delete variables and objects.
CodePage Contents Count IsCookieless IsNewSession IsReadOnly IsSynchronized Item Keys LCID Mode SessionID StaticObjects SyncRoot Timeout Session Properties The Session object exposes the following Public properties:
Abandon Add Clear CopyTo Equals GetEnumerator GetHashCode GetType Remove RemoveAll RemoveAt ToString Session Methods The Session object has the following Public methods: