21 ASP.NET 2.0, Web Forms and Web Controls
A fair question should be followed by a deed in silence. If any man will draw up his case, and put his name at the foot of the first page, I will give him an immediate reply. Where he compels me to turn over the sheet, he must wait my leisure. Lord Sandwich Rule One: Our client is always right Rule Two: If you think our client is wrong, see Rule One. Anonymous A fair question should be followed by a deed in silence. Dante Alighieri
You will come here and get books that will open your eyes, and your ears, and your curiosity, and turn you inside out or outside in. Ralph Waldo Emerson
OBJECTIVES In this chapter you will learn: Web application development using ASP.NET. To create Web Forms. To create ASP.NET applications consisting of multiple Web Forms. To maintain state information about a user with session tracking and cookies. To use the Web Site Administration Tool to modify Web application configuration settings. To control user access to Web applications using forms authentication and ASP.NET login controls. To use databases in ASP.NET applications. To design a master page and content pages to create a uniform look-and-feel for a Web site.
21.1 Introduction 21.2 Simple HTTP Transactions 21.3 Multitier Application Architecture 21.4 Creating and Running a Simple Web-Form Example 21.4.1 Examining an ASPX File 21.4.2 Examining a Code-Behind File 21.4.3 Relationship Between an ASPX File and a Code- Behind File 21.4.4 How the Code in an ASP.NET Web Page Executes 21.4.5 Examining the XHTML Generated by an ASP.NET Application 21.4.6 Building an ASP.NET Web Application 21.5 Web Controls 21.5.1 Text and Graphics Controls 21.5.2 AdRotator Control 21.5.3 Validation Controls
21.6 Session Tracking 21.6.1 Cookies 21.6.2 Session Tracking with HttpSessionState 21.7 Case Study: Connecting to a Database in ASP.NET 21.7.1 Building a Web Form That Displays Data from a Database 21.7.2 Modifying the Code-Behind File for the Guestbook Application 21.8 Case Study: Secure Books Database Application 21.8.1 Examining the Completed Secure Books Database Application 21.8.2 Creating the Secure Books Database Application 21.9 Wrap-Up 21.10 Web Resources
Fig. 21. 1 | Client interacting with Web server Fig. 21.1 | Client interacting with Web server. Step 1: The GET request.
Fig. 21. 2 | Client interacting with Web server Fig. 21.2 | Client interacting with Web server. Step 2: The HTTP response.
Fig. 21.3 | Three-tier architecture.
Outline WebTime.aspx
Portability Tip 21.1 The same Web control can map to different XHTML elements, depending on the client browser and the Web control’s property settings.
Outline WebTime.aspx.cs (1 of 2)
Outline WebTime.aspx.cs (2 of 2)
Performance Tip 21.1 Once an instance of the Web page has been created, multiple clients can use it to access the page—no recompilation is necessary. The project will be recompiled only when you modify the application; changes are detected by the runtime environment, and the project is recompiled to reflect the altered content.
Outline WebTime.html
Fig. 21.7 | Creating an ASP.NET Web Site in Visual Web Developer.
Fig. 21.8 | Solution Explorer window for project WebTime. Code-behind file Properties Refresh Nest Related Files View Code View Designer Copy Web Site ASP.NET Configuration ASPX file Fig. 21.8 | Solution Explorer window for project WebTime.
Fig. 21.9 | Toolbox in Visual Web Developer.
Fig. 21.10 | Source mode of the Web Forms Designer. Design mode button Source mode button Fig. 21.10 | Source mode of the Web Forms Designer.
Fig. 21.11 | Design mode of the Web Forms Designer. Cursor Cursor’s current location Fig. 21.11 | Design mode of the Web Forms Designer.
Fig. 21. 12 | Code-behind file for Default Fig. 21.12 | Code-behind file for Default.aspx generated by Visual Web Developer.
Portability Tip 21.2 Absolute positioning is discouraged, because pages designed in this manner may not render correctly on computers with different screen resolutions and font sizes. This could cause absolutely positioned elements to overlap each other or display off-screen, requiring the client to scroll to see the full page content.
Fig. 21.13 | WebTime.aspx after inserting text and a new paragraph. Cursor position after inserting text and a new paragraph by pressing Enter Block Format drop-down list Fig. 21.13 | WebTime.aspx after inserting text and a new paragraph.
Label Fig. 21.14 | WebTime.aspx after adding a Label and setting its properties.
Fig. 21.15 | Commonly used Web controls.
Outline WebControls.aspx (1 of 6)
Outline WebControls.aspx (2 of 6)
Outline WebControls.aspx (3 of 6)
Outline WebControls.aspx (4 of 6)
Outline WebControls.aspx (5 of 6)
Outline (6 of 6) WebControls.aspx Image control TextBox control DropDownList control HyperLink control RadioButtonList control Button control WebControls.aspx (6 of 6)
Fig. 21.17 | DropDownList Tasks smart tag menu.
Outline FlagRotator.aspx (1 of 2)
Outline (2 of 2) FlagRotator.aspx (a) (b) AdRotator image AlternativeText displayed in a tooltip (b) (c) FlagRotator.aspx (2 of 2)
Outline AdRotator Information.xml (1 of 4)
Outline AdRotator Information.xml (2 of 4)
Outline AdRotator Information.xml (3 of 4)
Outline AdRotator Information.xml (4 of 4)
Outline Validation.aspx (1 of 6)
Outline Validation.aspx (2 of 6)
Outline Validation.aspx (3 of 6)
Outline Validation.aspx (4 of 6)
Outline (a) (b) Validation.aspx (5 of 6)
Outline (c) (d) Validation.aspx (6 of 6)
Outline Validation.aspx.cs (1 of 2)
Outline Validation.aspx.cs (2 of 2)
Performance Tip 21.2 Setting EnableViewState to False reduces the amount of data passed to the Web server with each request.
Outline Validation.html (1 of 9)
Outline Validation.html (2 of 9)
Outline Validation.html (3 of 9)
Outline Validation.html (4 of 9)
Outline Validation.html (5 of 9)
Outline Validation.html (6 of 9)
Outline Validation.html (7 of 9)
Outline Validation.html (8 of 9)
Outline Validation.html (9 of 9)
Portability Tip 21.3 Clients may disable cookies in their Web browsers to ensure that their privacy is protected. Such clients will experience difficulty using Web applications that depend on cookies to maintain state information.
Outline Options.aspx (1 of 4)
Outline Options.aspx (2 of 4)
Outline (a) Options.aspx (3 of 4) (b)
Outline (c) Options.aspx (4 of 4) (d)
Outline Options.aspx.cs (1 of 3)
Outline Options.aspx.cs (2 of 3)
Outline Options.aspx.cs (3 of 3)
Outline Recommendations .aspx (1 of 2)
Outline Recommendations .aspx (2 of 2)
Outline Recommendations .aspx.cs (1 of 2)
Outline Recommendations .aspx.cs (2 of 2)
Fig. 21.27 | HttpCookie properties.
Outline Options.aspx (1 of 4)
Outline Options.aspx (2 of 4)
Outline (a) Options.aspx (3 of 4) (b)
Outline (c) Options.aspx (4 of 4) (d)
Software Engineering Observation 21.1 A Web Form should not use instance variables to maintain client state information, because each new request or postback, is handled by a new instance of the page. Web Forms should maintain client state information in HttpSessionState objects, because such objects are specific to each client.
Outline Options.aspx.cs (1 of 3)
Outline Options.aspx.cs (2 of 3)
Outline Options.aspx.cs (3 of 3)
Software Engineering Observation 21.2 One of the primary benefits of using HttpSessionState objects (rather than cookies) is that HttpSessionState objects can store any type of object (not just Strings) as attribute values. This provides you with increased flexibility in determining the type of state information to maintain for clients.
Fig. 21.30 | HttpSessionState properties.
Outline Recommendations .aspx (1 of 2)
Outline Recommendations .aspx (2 of 2)
Outline Recommendations .aspx.cs (1 of 2)
Outline Recommendations .aspx.cs (2 of 2)
Fig. 21.33 | Guestbook application GUI in Design mode. GridView control Fig. 21.33 | Guestbook application GUI in Design mode.
Fig. 21.34 | Change Data Source dialog in Visual Web Developer.
Fig. 21.35 | Configuring the SELECT statement used by the SqlDataSource to retrieve data.
Fig. 21.36 | Previewing the data retrieved by the SqlDataSource.
SqlDataSource control Fig. 21.37 | Design mode displaying SqlDataSource control for a GridView.
Fig. 21.38 | Removing the MessageID column from the GridView.
Fig. 21.39 | Setting up INSERT parameters based on control values.
Outline Guestbook.aspx (1 of 7)
Outline Guestbook.aspx (2 of 7)
Outline Guestbook.aspx (3 of 7)
Outline Guestbook.aspx (4 of 7)
Outline Guestbook.aspx (5 of 7)
Outline (a) Guestbook.aspx (6 of 7)
Outline (b) Guestbook.aspx (7 of 7)
Outline Guestbook.aspx.cs (1 of 2)
Outline Guestbook.aspx.cs (2 of 2)
Fig. 21.42 | Login.aspx page of the secure books database application.
Fig. 21.43 | CreateNewUser.aspx page of the secure book database application.
Fig. 21.44 | Message displayed to indicate that a user account was created successfully.
Fig. 21.45 | Books.aspx displaying books by Harvey Deitel (by default).
Fig. 21.46 | Books.aspx displaying books by Andrew Goldberg.
Fig. 21.47 | Logging in using the Login control.
Fig. 21.48 | Error message displayed for an unsuccessful login attempt using the Login control.
Fig. 21.49 | Web Site Administration Tool for configuring a Web application.
Fig. 21.50 | Security page of the Web Site Administration Tool.
Fig. 21. 51 | Choosing the type of authentication used by an ASP Fig. 21.51 | Choosing the type of authentication used by an ASP.NET Web application.
Fig. 21.52 | Main page of the Web Site Administration Tool after enabling forms authentication.
Fig. 21.53 | Add New Access Rule page used to configure directory access.
Fig. 21.54 | Master page in Source mode.
Fig. 21.55 | Master page in Design mode.
Outline Bug2Bug.master (1 of 2)
Outline Bug2Bug.master (2 of 2)
Fig. 21.57 | Content page CreateNewUser.aspx in Source mode.
Fig. 21.58 | Content page CreateNewUser.aspx in Design mode.
Outline CreateNewUser.aspx (1 of 3)
Outline CreateNewUser.aspx (2 of 3) (a) (b)
Outline (c) CreateNewUser.aspx (3 of 3)
Outline Login.aspx (1 of 2)
Outline (a) (b) Login.aspx (2 of 2)
Fig. 21.61 | Authors DataTable in the Dataset Designer.
Fig. 21.62 | Query Builder for designing a query that selects books written by a particular author.
Fig. 21.63 | Dataset Designer after adding the TitlesTableAdapter.
Fig. 21.64 | Choosing a business object for an ObjectDataSource.
Fig. 21.65 | Choosing a data method of a business object for use with an ObjectDataSource.
Fig. 21.66 | Choosing a data source for a DropDownList.
Fig. 21.67 | Choosing the data source for a parameter in a business object’s data method.
Fig. 21.68 | Completed Books.aspx in Design mode.
Outline Books.aspx (1 of 5)
Outline Books.aspx (2 of 5)
Outline (a) Books.aspx (3 of 5)
Outline (b) Books.aspx (4 of 5)
Outline (c) Books.aspx (5 of 5)