Creating an Interactive Web Page Lesson 2
Objectives
Events An event occurs when something happens that an object or Web control can sense, like someone clicking a button. As a Web page moves through its stages, especially when it is being processed by the server, events are triggered that allow this page flow to be controlled.
AutoEventWireup AutoEventWireup ties events to the code-behind of Visual Web Developer, C#, and Visual Basic (VB) to respond to the AutoEventWireup differently. In order to capture the many events the browser triggers during its lifecycle, it needs to monitor the page as it moves through all of its stages from the request, through processing, display, and destruction. In order to monitor a Web page’s lifecycle events, an attribute called AutoEventWireup needs to be set at the beginning of a program to tie the page’s events to the program.
Event Handler Visual Basic is unable to use all the benefits of AutoEventWireup, so this feature should be turned off. In its place, Visual Basic uses an event handler to “wire up” the events. Visual Basic’s event handler does more than handle events. It works with IntelliSense to help Visual Basic developers write code by giving them a list of events to choose from rather than forcing them to recall their names.
Page Event and Lifecycle A page event is an alert that something is different about the Web page. A page lifecycle is defined as the time the page request is given until the page is destroyed. As the server processes HTML during the page- creation stages, it processes any additional code that may have been embedded into the HTML. ASP.NET sends this additional code in the form of a file containing our code-behind. After processing the HTML and code-behind, the Web page is sent back to the browser.
Application Events An application has a lifecycle similar to the page lifecycle where a series of phases trigger events. These events are related to all the pages of the entire application rather than just the current page. Reading and writing values here can influence all users running the application.
Events Related to Handling Requests The following is a list of application events related to handling requests. They are listed in the same order in which they trigger. –BeginRequest: A request has been received. –AuthenticateRequest: Identity of the user has been established. –DefaultAuthentication: Ensures authentication has occurred. –AuthorizeRequest: User authorization has been verified. –ResolveRequestCache: Authorization has been verified to use the cache.
Events Related to Handling Requests –AcquireRequestState: Current state has been acquired. –PreRequestHandlerExecute: Event handling is about to start. –PostRequestHandlerExecute: Event handling is finished. –ReleaseRequestState: Requests for state have been saved. –UpdateRequestCache: Cache is ready for subsequent requests. –EndRequest: The request has been processed.
Commonly Programmed Application Events The most commonly programmed application events are: –Application_Start –Application_End –Application_Error –Application_LogRequest
Session Events A session has a lifecycle similar to the Page lifecycle where a series of phases triggers events. These events are related to individual browser sessions rather than the entire application. Reading and writing values here only applies to each user individually. The session lifecycle generates only two events, OnStart and OnEnd.
Global.asax The events in the application and session stages of a Web page’s lifecycle occur only on the Web server. The coding for server-side events is programmed in a specific code file ―global.asax. Global.asax is an optional file on the server that, if present, contains program code for handling session and application events. These server-side events are only processed from the Global.aspx file and nothing from this file is ever passed to the client’s browser.
Control Events A control’s lifecycle is similar to the Page lifecycle where it is re-created along with the page. In addition to the events that can be triggered in this creation lifecycle, the events for controls can also be triggered by user activities on the control.
Postback In a desktop application, the page only has to be loaded once for the user to work with it. A dynamic Web page is completely reloaded every time the server processes it. The postback property of the Web page allows the developer to separate code that needs to be run only when the page is first loaded from the pages that have been re-created from user interaction since the page was first loaded. The IsPostBack property of the ViewState object is designed to solve this problem by sensing whether a page has had a previous round trip to the server.
Navigating Between Pages In addition to the tools we have used so far, the primary tools available that allow a program to navigate and pass data between Web pages are: –Response.Redirect –Server.Transfer –Cross-page posting
Differences between Response.Redirect and Server.Transfer
Controls Visual Web Developer provides a wide variety of controls that can be added to a Web form to provide viewer interaction.
HTML Controls Input (Button): This control creates a button control on the Web form. Input (Reset): This control also creates a button. It is used to clear a form of viewer data. Input (Submit): Like the standard button control, the Submit button control is used to trigger a processing event. Typically, the action of this button is to initiate the submission of the form data to the server for processing. Input (Text): This control creates a textbox element on the Web form. Either text is displayed by the Web form or the user can type text into the control for processing.
HTML Controls Input (File): This control creates an input textbox that receives from the local computer the full pathname of a file that is to be uploaded to the server. Input (Password): This control creates an input textbox that masks any text entered into the box to protect its value. As its name implies, this control is most commonly used for passwords Input (Check box): This control creates a check box element on a Web form that can be toggled on or off by the viewer.
HTML Controls Input (Radio): This control creates a radio button element on a Web form that can be toggled on or off or included in a group of mutually exclusive radio button controls. Input (Hidden): This control creates a hidden label control that can be used to hold data without being displayed on the Web page. Textarea: This control creates a multiline text box. It can also include scroll bars and have the capability of being resized by the viewer. Table: This control creates a table declaration in a Web form with a single row containing a single cell.
HTML Controls Image: This control creates an image element on a Web form that contains null values for the alternate tag and the location of the source-image file. Select (Dropdown): This control creates a bare-bones drop-down list box element on a Web form. The element must be edited to supply the list value and the default selection to be displayed at startup. Horizontal Rule: This control inserts a horizontal line on the Web form. There is no interaction available; this element is purely for design purposes.
HTML Controls Div: To understand the function of this control, you need to understand the Flow Layout Panel control. The Div control in the HTML category creates an element that essentially creates a Flow Layout Panel control. This control can be used to arrange its contents either horizontally or vertically.
Server and Validation Controls Essentially all of the other non-HTML controls in the Toolbox are server controls. The Standard category lists all the server controls that are not grouped for a common purpose. The Validation controls are special controls used for automatically validating user input. Server controls are compiled from a programming language such as C# or Visual Basic and are then processed at the server. They are also considered server-side controls. HTML controls are client-side controls and are processed by the browser in a client-side programming language such as Javascript or VB script.
User Controls User controls, custom server controls, components, Web parts, and third-party controls are various sources for creating additional controls that can enhance Web extensions. User controls look just like the Web pages we have been creating. They include server controls with event procedures written in the code-behind window. However, they cannot be run directly in the browser like a Web page.
Choosing a Control
CLR The.NET Framework is designed to be a common platform for creating both Windows-based (desktop) and Web-based applications. The framework consists of the Common Language Runtime (CLR) and the Framework Class Library (FCL).
Machine.config The configuration system has a single file that contains all the required changes to the default settings for the entire.NET system on a given computer. This file is machine.config. It is recommended that you don’t make changes to this file unless you are well aware of the consequences.
App.config and Web.Config If the.NET defaults or machine.config settings are unacceptable, you have the option of overriding them with additional web.config files for Web-based applications or app.config files for Windows-based applications.
Summary Using AutoEventWireup in both C# and Visual Basic with different results. Monitoring and controlling stages in the page lifecycle with events. Writing event procedures to respond to form control events. Writing event procedures to interact with the server through the IsPostBack property of the page. Redirecting a Web page using Response.redirect, Server.transfer, and cross-page posting.
Summary Writing a Web page using HTML controls. Writing a Web page using Server controls. Choosing the appropriate control for a given scenario. Configuring a Web page using.NET configuration files.