690 likes | 800 Views
Chapter 16. Assembling a Three-Tier Web Form Application. Objectives. In this chapter, you will: Understand the concept of state for Web applications Create an ASP.NET user control Use data binding technology Develop a Web application for Bradshaw Marina Learn about XML and Web services.
E N D
Chapter 16 Assembling a Three-Tier Web Form Application Object-Oriented Application Development Using VB .NET
Objectives In this chapter, you will: • Understand the concept of state for Web applications • Create an ASP.NET user control • Use data binding technology • Develop a Web application for Bradshaw Marina • Learn about XML and Web services Object-Oriented Application Development Using VB .NET
Understanding the Concept of State for Web Applications • Hypertext Transmission Protocol (HTTP) • Used by the Internet • A stateless protocol • Does not know whether the request is a single request or part of a sequence of requests from the same user Object-Oriented Application Development Using VB .NET
Maintaining State • Options available in HTTP for maintaining state: • Application state • Uses the Application object for information that needs to be available to all users on the site • Session state • Uses the Session object • Information persists only for the duration of the session of a particular client Object-Oriented Application Development Using VB .NET
Maintaining State • Options available in HTTP for maintaining state (continued): • Cookies • Save small amounts of information, less than 4 K, in a file on the client's computer • Information persists for an assigned length of time • Database • Saves large amounts of information • Information persists beyond the session Object-Oriented Application Development Using VB .NET
Understanding Application State and the Application Object • Application object • Can be used to save global information that can be shared among all users of a Web application • Only one is available for each Web application • Application variables in ASP.NET • Stored centrally • Can be accessed through the Application property of the ASP.NET Page class • Global.asax file • Contains the initial application state variable Object-Oriented Application Development Using VB .NET
Understanding Application State and the Application Object • Application variable values are available to all users of the Web site • Conflicts can occur if multiple users try to change the same variable’s value at the same time • Solution • Before changing the value of an application variable • Invoke the application’s Lock method • After the change has been made • Invoke the application’s Unlock method Object-Oriented Application Development Using VB .NET
Understanding Session State and the Session Object • Session object • Provides a way to keep information about a user throughout multiple requests to a Web application • A session • Begins when the user enters the Web site • Ends when the user leaves the Web application or a timeout occurs • Default timeout is typically 20 minutes Object-Oriented Application Development Using VB .NET
Understanding Session State and the Session Object • Session data is used in: • Shopping cart or basket Web applications • Web pages which offer personalization Object-Oriented Application Development Using VB .NET
Using Cookies • A cookie • A small text file saved on the computer by the Web application • Provides a way to save state beyond the current visit • Often used for keeping • User preferences • Information for logons Object-Oriented Application Development Using VB .NET
Using Cookies • Browser settings regarding cookies • Default: allow the computer to accept cookies • Can be changed to prevent cookies from being written to the computer • Expiration of a cookie • Applications can set an expiration date • Otherwise, a cookie expires when the session ends Object-Oriented Application Development Using VB .NET
Using Cookies • Cookies • Written using the response object • Retrieved using the request object • Can be • Single-value • Multiple-value Object-Oriented Application Development Using VB .NET
Using Cookies Object-Oriented Application Development Using VB .NET
Using the ASP.NET Session State Control • ASP.NET can be configured to work without cookies by using its own session state model • In this model • ASP.NET server serializes all objects in the session collection to the session state store at the end of each Web request • Storage of session state • Default settings: The session state store is kept in cached memory • Can be configured to store the session state to a database Object-Oriented Application Development Using VB .NET
Creating an ASP.NET User Control • User controls • Web forms that consist of ASP server controls and HTML code without • <HTML> tag • <HEADER> tag • <TITLE> tag • A user control file • Created within an existing application • Has an .ascx extension Object-Oriented Application Development Using VB .NET
Creating an ASP.NET User Control • An example of using user controls • A Web application navigation system • Can be easily placed in all the Web forms of the Web application Object-Oriented Application Development Using VB .NET
Creating an ASP.NET User Control Object-Oriented Application Development Using VB .NET
Using Data Binding Technology • Data binding technology • Provides a means to tie the property of a control to an underlying source of data • Data bound or data aware controls • Controls which are bound to an underlying source of data • Do not need to be repopulated when the data changes Object-Oriented Application Development Using VB .NET
Binding Data to a Drop-Down List • Boat Manufacturers Web form contains three user controls: • A drop-down list box • A label • A button Object-Oriented Application Development Using VB .NET
Binding Data to a Drop-Down List Object-Oriented Application Development Using VB .NET
Binding Data to a Drop-Down List • Drop-down list box on Boat Manufacturers Web form • Initially populated with the names of four boat manufacturers • Done using an array list • Change Value button • Used to change the values in the array list • Changes are reflected in the list box Object-Oriented Application Development Using VB .NET
Understanding the DataGrid Control • ASP.NET “data bound only” controls • Designed for presenting data on Web forms • Include • Repeater • DataList • DataGrid Object-Oriented Application Development Using VB .NET
Understanding the DataGrid Control • DataGrid control • Like an HTML table that provides enhanced features such as • Column sorting • Pagination • In-grid updating • Flexible formatting • In the Bradshaw Marina Web site • Can be used for the Boat class Object-Oriented Application Development Using VB .NET
Understanding the DataGrid Control Object-Oriented Application Development Using VB .NET
Understanding the DataGrid Control • In two-tier applications • The data grid is typically bound directly to the data sets of a database • In three-tier applications • The array list intermediates as the middle tier between the data and the presentation • An ASP.NET DataTable control • Handles the data and binds the grid to it Object-Oriented Application Development Using VB .NET
Understanding the DataGrid Control • To create a DataGrid control • Create a table • Populate the table with data from the boat array list • Bind the data grid to this data table • Configure the data grid • Use the DataGrid Properties dialog box Object-Oriented Application Development Using VB .NET
Understanding the DataGrid Control Object-Oriented Application Development Using VB .NET
Understanding the DataGrid Control • DataGrid Properties dialog box can be used to • Define the general properties of the grid • For example: how data is sorted • Set properties of • Columns • Paging behavior • Format of the grid • Border of the grid Object-Oriented Application Development Using VB .NET
Understanding the DataGrid Control Object-Oriented Application Development Using VB .NET
Understanding the DataGrid Control Object-Oriented Application Development Using VB .NET
Understanding the DataGrid Control Object-Oriented Application Development Using VB .NET
Developing a Web Application for Bradshaw Marina • Bradshaw Marina Web site • Will be based on a three-tier approach • Will use the request/response Web model Object-Oriented Application Development Using VB .NET
Developing a Web Application for Bradshaw Marina Object-Oriented Application Development Using VB .NET
Bradshaw Marina Web Site Design • Bradshaw Marina Web site design • A user control will provide basic navigation between pages • Will use Web server control buttons • Bradshaw Marina home page design • Home page controls perform most of the processing for the Web site • Home page file: default.aspx • User control files: theme.ascx and theme.ascx.vb Object-Oriented Application Development Using VB .NET
Bradshaw Marina Web Site Design Object-Oriented Application Development Using VB .NET
Designing the Bradshaw Marina Home Page • Data grid • Bound to the customer attributes • Name • Address • Phone number • Can be used to update and delete customer information Object-Oriented Application Development Using VB .NET
Designing the Bradshaw Marina Home Page • When a user successfully logs on to the Web site • The Web application writes cookies to save • Customer’s name • Customer’s address • lblReturning Customer control • Holds the name of the customer if he or she returns to the site before the cookies expire Object-Oriented Application Development Using VB .NET
Designing the Bradshaw Marina Home Page Object-Oriented Application Development Using VB .NET
Designing the Bradshaw Marina Home Page • Drop-down list box • Bound to the customer name • When the user clicks the name • Customer’s boat is retrieved • Panel to the left of the drop-down list box appears and is populated with the boat attributes of the customer • Grid resizes to show only the selected customer Object-Oriented Application Development Using VB .NET
Designing the Bradshaw Marina Home Page Object-Oriented Application Development Using VB .NET
Designing the Bradshaw Marina Home Page • List All Customers button • When clicked • Shows all the customers in the data grid • Hides the panel showing boat information • Add Customer & Boat button • Used to navigate to a Web form where a new customer and boat can be added • lblNbrUsers control • Displays the number of times the page has been visited Object-Oriented Application Development Using VB .NET
Designing the Bradshaw Marina Home Page • Navigation bar at the top of the Web page • A user control • Includes four horizontal navigation buttons • If the Edit link in a row of the data grid is clicked • The form adds text boxes to the name and address columns • On the row where Edit was clicked, the words Update and Cancel replace the word Edit Object-Oriented Application Development Using VB .NET
Designing the Bradshaw Marina Home Page Object-Oriented Application Development Using VB .NET
Examining the Code for the Default Web Form • Page_Init event handler • Establishes a connection to the database • Initializes the connection • Page_Load event handler • Checks for postback • Loads all the customers into an array list • Calls the BindToGrid method • Adds to number of site visits • Gets customer cookie if it exists • Hides the panel control and boat attributes Object-Oriented Application Development Using VB .NET
Examining the Code for the Default Web Form • BindToGrid method • Creates a table • Populates the table from the array list of customers • Binds the table to the data grid • ddlNames_SelectedIndexChanged event handler • When a name in the drop-down list box is clicked • Searches for the boat for the selected customer • Obtains the customer’s boat attributes • Displays the customer’s boat attributes Object-Oriented Application Development Using VB .NET
Examining the Code for the Default Web Form • btnAllCust_Click method • Contains the code for the button to have all the customers appear in the data grid • btnAddCustBoat_Click method • Redirects program control to the AddCustomerBoat.aspx Web form Object-Oriented Application Development Using VB .NET
Examining the Code for the Default Web Form • Edit and Delete columns of the data grid • Can be added using the Property Builder • Some code must be added to handle the required processing for • Updating • Canceling • Deleting Object-Oriented Application Development Using VB .NET
Examining the Code for the Default Web Form Object-Oriented Application Development Using VB .NET
Examining the Code for the Default Web Form • DataGrid1_Edit method • Contains the code for the Edit command • Sets the row clicked in the Edit column to the data grid’s EditItemIndex property • DataGrid1_Update method • Contains the code for the update event • Obtains values from the data grid • Creates a customer instance • Calls the Update method of the customer instance • Turns off the edit mode • Binds the data to the grid Object-Oriented Application Development Using VB .NET
Examining the Code for the Default Web Form • DataGrid1_Cancel method • Turns off the edit mode • Rebinds the data • DataGrid1_Delete method • Contains the code for deleting a record • Obtains values from the data grid • Creates a customer instance • Calls the Delete method of the customer instance • Rebinds the data to the data grid Object-Oriented Application Development Using VB .NET