Chapter 16 Objectives Assembling a Three-Tier Application 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 1 2 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 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 3 4 Maintaining State Understanding Application State and the Application Object 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 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 class Global.asax file Contains the initial application state variable 5 6
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 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 7 8 Understanding Session State and the Session Object Using Cookies Session data is used in: Shopping cart or basket Web applications Web pages which offer personalization 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 9 10 Using Cookies 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 Cookies Written using the response object Retrieved using the request object Can be Single-value Multiple-value 11 12
Using Cookies 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 13 14 Creating an ASP.NET User Control 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 An example of using user controls A Web application navigation system Can be easily placed in all the Web forms of the Web application 15 16 Creating an ASP.NET User Control 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 17 18
Binding Data to a Drop-Down List Binding Data to a Drop-Down List Boat Manufacturers Web form contains three user controls: A drop-down list box A label A button 19 20 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 ASP.NET data bound only controls Designed for presenting data on Web forms Include Repeater DataList DataGrid 21 22 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 23 24
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 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 25 26 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 27 28 29 30
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 31 32 Developing a Web Application for Bradshaw Marina 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 33 34 Bradshaw Marina Web Site Design Designing the Bradshaw Marina Home Data grid Bound to the customer attributes Name Address Phone number Can be used to update and delete customer information 35 36
Designing the Bradshaw Marina Home Designing the Bradshaw Marina Home 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 37 38 Designing the Bradshaw Marina Home Designing the Bradshaw Marina Home 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 39 40 Designing the Bradshaw Marina Home 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 Designing the Bradshaw Marina Home 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 41 42
Designing the Bradshaw Marina Home Examining the Code for the Default _Init event handler Establishes a connection to the database Initializes the connection _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 43 44 Examining the Code for the Default Examining the Code for the Default 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 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 45 46 Examining the Code for the Default Examining the Code for the Default 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 47 48
Examining the Code for the Default 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 Examining the Code for the Default 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 49 50 Designing the AddCustomerBoat Web Form AddCustomerBoat Web form Uses Web validation controls to validate data RequiredFieldValidator control Used in each text box to ensure that a value is entered into the text box CompareValidator control Validates entries for» Boat length» Model year» Phone number Designing the AddCustomerBoat Web Form AddCustomerBoat Web form Bottom part of the panel Used to enter boat data Contains two panels Uses a radio button list control with» A Sailboat radio button» A Powerboat radio button Sets the default boat type to sailboat 51 52 Designing the AddCustomerBoat Web Form Designing the AddCustomerBoat Web Form 53 54
Designing the Startup Designing the Startup Login Web form Ensures that data has been entered into the text boxes Uses customer phone number to check whether the person logging on is a customer If the person is a customer An acknowledgment message is displayed If the customer is not found in the customer table A message indicating that the customer could not be found appears 55 56 Designing the AboutBradshawMarina Designing the AboutBradshawMarina Horizontal navigation bar About Bradshaw Marina button Opens the AboutBradshawMarina A static Web page Provides information about Bradshaw Marina Register button Opens a Web form where users can register Not yet implemented 57 58 Introduction to XML and Web Services Introduction to XML and Web Services XML (Extensible Markup Language) provides a way to Integrate disparate data sources within an organization Distribute applications across enterprises via the Internet XML Developed to overcome the following limitations of HTML HTML cannot handle hierarchical structures HTML tags do not relate to the content Web developers are limited to using the existing fixed HTML tag set 59 60
XML Example Application XML Example Application XML can be used with the Bradshaw Marina Web site An example Retrieves customer and boat information from a database Stores the information in a dataset A dataset s WriteXML method Creates an XML file Stores the dataset values in XML format ReadXML method Retrieves the data from an XML file Populates the dataset 61 62 Understanding Web Services Understanding Web Services A Web service Technically The resulting service for any HTTP request Common view A Web service is based on the Internet and XML (essentially XML Web services) A Web service performs a specific functional requirement The.NET framework provides tools for creating Web services Technologies involved in creating Web services Simple Object Access Protocol (SOAP) Web Services Description Language (WSDL) Universal Description, Integration, and Discovery (UDDI) 63 64 Understanding Web Services Understanding Web Services Simple Object Access Protocol (SOAP) A messaging protocol Provides a way to wrap information required to be sent with a request to a Web service Serves as a wrapper for the information returned from the Web service Web Services Description Language (WSDL) Defines all of the interactions of a Web service regardless of the computing platform on which the Web service is running Universal Description, Integration, and Discovery (UDDI) Provides a mechanism for discovering available Web services 65 66
Developing a Web Service for Bradshaw Marina A Web service developed for Bradshaw Marina Determines the rental charges for its slips Logic Rental charges are based on the length of the boat for a dock Boats up to 30 feet in length can use a standard dock with a monthly rental fee of three dollars per foot Boats longer than 30 feet require a special dock and rent for five dollars per foot, starting at 30 feet No boats over 50 feet can be docked Can be used by other marinas with the same logic for rentals Summary HTTP is a stateless protocol Application state, Session state, and cookies assist in maintaining state between the client and the server Both two tier and three tier Web processing relies on the request/response model ASP.NET user controls provide consistency to Web forms ASP.NET s data binding technology allows properties of Web server controls to be bound to an underlying data source 67 68 Summary XML, a self-describing data formatting technology, provides a text-based data storage format that supports hierarchical data structures Web services distribute logic over the Internet SOAP provides wrappers around structures sent to a Web service and returned from a Web service WSDL is a platform-independent standard for describing data for Web services UDDI assists in locating and using Web services 69