Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2006 Pearson Education, Inc. All rights reserved. 1 21 ASP.NET 2.0, Web Forms and Web Controls.

Similar presentations


Presentation on theme: " 2006 Pearson Education, Inc. All rights reserved. 1 21 ASP.NET 2.0, Web Forms and Web Controls."— Presentation transcript:

1  2006 Pearson Education, Inc. All rights reserved. 1 21 ASP.NET 2.0, Web Forms and Web Controls

2  2006 Pearson Education, Inc. All rights reserved. 2 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

3  2006 Pearson Education, Inc. All rights reserved. 3 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

4  2006 Pearson Education, Inc. All rights reserved. 4 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.

5  2006 Pearson Education, Inc. All rights reserved. 5 21.1Introduction 21.2Simple HTTP Transactions 21.3Multitier Application Architecture 21.4Creating and Running a Simple Web-Form Example 21.4.1Examining an ASPX File 21.4.2Examining a Code-Behind File 21.4.3Relationship Between an ASPX File and a Code- Behind File 21.4.4How the Code in an ASP.NET Web Page Executes 21.4.5Examining the XHTML Generated by an ASP.NET Application 21.4.6Building an ASP.NET Web Application 21.5Web Controls 21.5.1Text and Graphics Controls 21.5.2 AdRotator Control 21.5.3Validation Controls

6  2006 Pearson Education, Inc. All rights reserved. 6 21.6Session Tracking 21.6.1Cookies 21.6.2Session Tracking with HttpSessionState 21.7Case Study: Connecting to a Database in ASP.NET 21.7.1Building a Web Form That Displays Data from a Database 21.7.2Modifying the Code-Behind File for the Guestbook Application 21.8Case Study: Secure Books Database Application 21.8.1Examining the Completed Secure Books Database Application 21.8.2Creating the Secure Books Database Application 21.9Wrap-Up 21.10Web Resources

7  2006 Pearson Education, Inc. All rights reserved. 7 21.1 Introduction ASP.NET 2.0 and Web Forms and Controls – Web application development with Microsoft’s ASP.NET 2.0 technology – Web Form files have the filename extension.aspx and contain the Web page’s GUI – Every ASPX file created in Visual Studio has a corresponding class written in a.NET language, such as C# Contains event handlers, initialization code, utility methods and other supporting code Called the code-behind file Provides the ASPX file’s programmatic implementation

8  2006 Pearson Education, Inc. All rights reserved. 8 21.2 Simple HTTP Transactions Simple HTTP Transactions – HTTP specifies a set of methods and headers that allow clients and servers to interact and exchange information – A Web page is nothing more than an XHTML document – A plain text file containing markup that describe to a Web browser how to display and format the document’s information – Any XHTML document available for viewing over the Web has a corresponding URL An address indicating the location of an Internet resource – When requesting ASP.NET Web applications, the Web server is usually Microsoft Internet Information Services (IIS) – The information provided in the header specifies the Multipurpose Internet Mail Extensions (MIME) type of the content that the server is transmitting to the browser Internet standard that specifies data formats – Programs can interpret data correctly

9  2006 Pearson Education, Inc. All rights reserved. 9 Fig. 21.1 | Client interacting with Web server. Step 1: The GET request.

10  2006 Pearson Education, Inc. All rights reserved. 10 Fig. 21.2 | Client interacting with Web server. Step 2: The HTTP response.

11  2006 Pearson Education, Inc. All rights reserved. 11 21.3 Multitier Application Architecture Web-based applications are multitier applications – Multitier applications divide functionality into separate tiers Bottom (information) tier – Maintains data pertaining to the application; typically stores data in a RDBMS Middle tier – Implements business, controller and presentation logic to control interactions between the application’s clients and data – Acts as an intermediary between data in the information tier and the application’s clients Top (client) tier – Application’s user interface: gathers input and displays output – Interacts with the middle tier to make requests and to retrieve data from the information tier Displays the data retrieved from the middle tier to the user The client tier never directly interacts with the information tier

12  2006 Pearson Education, Inc. All rights reserved. 12 Fig. 21.3 | Three-tier architecture.

13  2006 Pearson Education, Inc. All rights reserved. 13 21.4.1 Examining an ASPX File Examining an ASPX File – ASP.NET comments begin with – Page directive Specifies the language of the code-behind file – AutoEventWireup attribute Determines how Web Form events are handle – Inherits attribute Specifies the class in the code-behind file from which this ASP.NET class inherits – ASP.NET markup is not case-sensitive Using a different case is not problematic

14  2006 Pearson Education, Inc. All rights reserved. 14 21.4.1 Examining an ASPX File (Cont.) – runat attribute Indicates that when a client requests this ASPX file: – Process the head element and its nested elements on the server – Generate the corresponding XHTML Sent to the client – asp: tag prefix in a control declaration Indicates that it is an ASP.NET Web control – Each Web control maps to a corresponding XHTML element When processing a Web control, ASP.NET generates XHTML markup that will be sent to the client to represent that control in a Web browser – span element Contains text that is displayed in a Web page

15  2006 Pearson Education, Inc. All rights reserved. 15 Outline WebTime.aspx ASP.NET comments Page directive to specify information needed by ASP.NET to process this file Document type declaration Mark up of a label web control

16  2006 Pearson Education, Inc. All rights reserved. 16 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.

17  2006 Pearson Education, Inc. All rights reserved. 17 21.4.2 Examining a Code-Behind File Examining a Code-Behind File – System.Web.UI Contains classes and controls that assist in building Web- based applications Class Page – Provides event handlers and objects necessary for creating Web-based applications – Method Page_Init Handles the Init event Indicates that a page is ready to be initialized Class Control – The base class that provides common functionality for all Web controls

18  2006 Pearson Education, Inc. All rights reserved. 18 Outline WebTime.aspx.cs (1 of 2) Retrieve the current time and formats it as HH:MM:SS

19  2006 Pearson Education, Inc. All rights reserved. 19 Outline WebTime.aspx.cs (2 of 2)

20  2006 Pearson Education, Inc. All rights reserved. 20 21.4.3 Relationship Between an ASPX File and a Code-Behind File ASP.NET creates two classes behind the scenes – A Partial class containing the remainder of the class Based on the markup in the ASPX file – A class based on the ASPX file Defines the page’s visual representation The first time the Web page is requested, the class is compiled, and an instance is created Web controls – System.Web.UI.WebControls – Derive from class WebControl

21  2006 Pearson Education, Inc. All rights reserved. 21 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.

22  2006 Pearson Education, Inc. All rights reserved. 22 21.4.4 How the Code in an ASP.NET Web Page Executes How the Code in an ASP.NET Web Page Executes – Init event Occurs when an instance of a page is created – Load event occurs and the Page_Load event handler executes Inherited from class Page – Typically overridden to perform any processing that is necessary to restore data from previous requests – The page processes any events raised by the page’s controls after Page_Load finishes executing – Unload event occurs when a Web Form object is ready for garbage collection Event handler Page_Unload – Inherited from class Page – Contains any code that releases resources

23  2006 Pearson Education, Inc. All rights reserved. 23 21.4.5 Examining the XHTML Generated by an ASP.NET Application XHTML Generated by an ASP.NET Application – XHTML forms can contain visual and nonvisual components – Attribute method Specifies the method by which the Web browser submits the form to the server – Attribute action Identifies the name and location of the resource that will be requested when this form is submitted – The runat attribute is removed when the form is processed on the server The method and action attributes are added The resulting XHTML form is sent to the client browser

24  2006 Pearson Education, Inc. All rights reserved. 24 Outline WebTime.html Declaration for a non-visual component: hidden input Represents the text in the label

25  2006 Pearson Education, Inc. All rights reserved. 25 21.4.6 Building an ASP.NET Web Application Step 1: Creating the Web Application Project (Fig. 21.7) – http://localhost (IIS’s root directory) corresponds to C:\InetPub\wwwroot http://localhost Step 2: Examining the Solution Explorer of the Newly Created Project (Fig. 21.8) Step 3: Examining the Toolbox in Visual Web Developer (Fig. 21.9) Step 4: Examining the Web Forms Designer (Fig. 21.10-11) Step 5: Examining the Code-Behind File in the IDE (Fig. 21.12) Step 6: Renaming the ASPX File Step 7: Renaming the Class in the Code-Behind File and Updating the ASPX File Step 8: Changing the Title of the Page

26  2006 Pearson Education, Inc. All rights reserved. 26 21.4.6 Building an ASP.NET Web Application (Cont.) Step 9: Designing the Page (Fig. 21.13) – Relative positioning Positions of controls and other elements are relative to the Web Form’s upper-left corner – Absolute positioning Controls are located exactly where they are dropped on the Web Form – Visual Web Developer is a WYSIWYG editor “What You See Is What You Get” Step 10: Adding Page Logic – Web.config stores configuration settings for an ASP.NET Web application Step 11: Running the Program

27  2006 Pearson Education, Inc. All rights reserved. 27 Fig. 21.7 | Creating an ASP.NET Web Site in Visual Web Developer.

28  2006 Pearson Education, Inc. All rights reserved. 28 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

29  2006 Pearson Education, Inc. All rights reserved. 29 Fig. 21.9 | Toolbox in Visual Web Developer. (a) (b)

30  2006 Pearson Education, Inc. All rights reserved. 30 Fig. 21.10 | Source mode of the Web Forms Designer. Design mode button Source mode button

31  2006 Pearson Education, Inc. All rights reserved. 31 Fig. 21.11 | Design mode of the Web Forms Designer. Cursor Cursor’s current location

32  2006 Pearson Education, Inc. All rights reserved. 32 Fig. 21.12 | Code-behind file for Default.aspx generated by Visual Web Developer.

33  2006 Pearson Education, Inc. All rights reserved. 33 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.

34  2006 Pearson Education, Inc. All rights reserved. 34 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

35  2006 Pearson Education, Inc. All rights reserved. 35 Fig. 21.14 | WebTime.aspx after adding a Label and setting its properties. Label

36  2006 Pearson Education, Inc. All rights reserved. 36 Fig. 21.15 | Commonly used Web controls.

37  2006 Pearson Education, Inc. All rights reserved. 37 21.5.1 Text and Graphics Controls Examining Web Controls on a Sample Registration Form – Image control Inserts an image into a Web page – ImageUrl property specifies the file location of the image to display – TextBox control Allows the you to obtain text from the user and display text to the user – DropDownList control Provides a list of options to the user Each item in the drop-down list is defined by a ListItem element – HyperLink control Adds a hyperlink to a Web page – NavigateUrl property specifies the resource that is requested – RadioButtonList control Provides a series of radio buttons for the user – Button control Represents a button that triggers an action when clicked – Visual Web Developer displays smart tag menus for many ASP.NET controls to facilitate performing common tasks

38  2006 Pearson Education, Inc. All rights reserved. 38 Outline WebControls.aspx (1 of 6) Define an Image control Set the color of a specific piece of text

39  2006 Pearson Education, Inc. All rights reserved. 39 Outline WebControls.aspx (2 of 6) Define a TextBox control used to collect the user’s first name

40  2006 Pearson Education, Inc. All rights reserved. 40 Outline WebControls.aspx (3 of 6) Defines a DropDownList Each item in the drop- down list is defined by a ListItem element

41  2006 Pearson Education, Inc. All rights reserved. 41 Outline WebControls.aspx (4 of 6) Adds a hyperlink to the web page Defines a RadioButtonList control Each item in the drop- down list is defined by a ListItem element

42  2006 Pearson Education, Inc. All rights reserved. 42 Outline WebControls.aspx (5 of 6) Defines a Button web control

43  2006 Pearson Education, Inc. All rights reserved. 43 Outline WebControls.aspx (6 of 6) Image control TextBox control DropDownList control HyperLink control RadioButtonList control Button control

44  2006 Pearson Education, Inc. All rights reserved. 44 Fig. 21.17 | DropDownList Tasks smart tag menu.

45  2006 Pearson Education, Inc. All rights reserved. 45 21.5.2 AdRotator Control AdRotator control – Randomly selects an image to display – Generates a hyperlink to the Web page associated with that image – Browsers that do not support images display alternate text that is specified in the XML document – The browser loads the Web page associated with that image when a user clicks the image or substituted text

46  2006 Pearson Education, Inc. All rights reserved. 46 21.5.2 AdRotator Control (Cont.) AdRotator control – Accesses an XML file to determine what advertisement image, hyperlink URL and alternate text to display and include in the page Create an XmlDataSource control to connect to the XML file – ImageUrl Element Specifies the path (location) of the advertisement’s image – NavigateUrl Element Specifies the URL for the Web page that loads when a user clicks the advertisement – The URL cannot contain any whitespace – AlternateText Element Contains text that displays in place of the image when the browser cannot locate or render the image – Also a tooltip – Impressions Element Specifies how often a particular image appears, relative to the other images

47  2006 Pearson Education, Inc. All rights reserved. 47 Outline FlagRotator.aspx (1 of 2) Defines an AdRotator control Defines an XmlDataSource control, which supplies the data to the AdRotator control

48  2006 Pearson Education, Inc. All rights reserved. 48 Outline FlagRotator.aspx (2 of 2) (a) AdRotator image AlternativeText displayed in a tooltip (b) (c)

49  2006 Pearson Education, Inc. All rights reserved. 49 Outline AdRotator Information.xml (1 of 4) Each Ad element represents an advertisement that will be displayed by the AdRotator control Specifies the path of the advertisement’s image Specifies the URL for the Web page that loads when user clicks on the advertisement Text that displays in place of the image when the browser cannot render the image Specifies how often a particular image appears

50  2006 Pearson Education, Inc. All rights reserved. 50 Outline AdRotator Information.xml (2 of 4)

51  2006 Pearson Education, Inc. All rights reserved. 51 Outline AdRotator Information.xml (3 of 4)

52  2006 Pearson Education, Inc. All rights reserved. 52 Outline AdRotator Information.xml (4 of 4)

53  2006 Pearson Education, Inc. All rights reserved. 53 21.5.3 Validation Controls Validation Control – Determines whether the data in another Web control is in the proper format – Provide a mechanism for validating user input on the client – The validator is converted into ECMAScript when the XHTML for the page is created ECMAScript is a scripting language that enhances the functionality and appearance of Web pages – For security reasons, validation is always performed on the server

54  2006 Pearson Education, Inc. All rights reserved. 54 21.5.3 Validation Controls (Cont.) – ControlToValidate property Indicates Validator verifies a control’s contents – ErrorMessage Property Text displayed if the validation fails – Display property Determines if the validator is displayed only when validation fails – RegularExpressionValidator Matches a Web control’s content against a regular expression – That expression is assigned to ValidationExpression property – IsPostBack property Determines whether the page is being loaded due to a postback – The current page reloads when the user submits the form EnableViewState attribute – Determines whether a Web control’s state persists when a postback occurs

55  2006 Pearson Education, Inc. All rights reserved. 55 Outline Validation.aspx (1 of 6)

56  2006 Pearson Education, Inc. All rights reserved. 56 Outline Validation.aspx (2 of 6) Defines a RequiredFieldValidator which confirms the TextBox is not empty

57  2006 Pearson Education, Inc. All rights reserved. 57 Outline Validation.aspx (3 of 6) Defines a RegularExpressionValidator control to determine whether data is entered in a valid format Defines a RequiredFieldValidator which confirms the TextBox is not empty

58  2006 Pearson Education, Inc. All rights reserved. 58 Outline Validation.aspx (4 of 6) Defines a RegularExpressionValidator control to determine whether data is entered in a valid format

59  2006 Pearson Education, Inc. All rights reserved. 59 Outline Validation.aspx (5 of 6) (a) (b)

60  2006 Pearson Education, Inc. All rights reserved. 60 Outline Validation.aspx (6 of 6) (c) (d)

61  2006 Pearson Education, Inc. All rights reserved. 61 Outline Validation.aspx.cs (1 of 2) Determines whether the page is being loaded due to a postback Uses the Request object to retrieve the values of the TextBox es

62  2006 Pearson Education, Inc. All rights reserved. 62 Outline Validation.aspx.cs (2 of 2)

63  2006 Pearson Education, Inc. All rights reserved. 63 Performance Tip 21.2 Setting EnableViewState to False reduces the amount of data passed to the Web server with each request.

64  2006 Pearson Education, Inc. All rights reserved. 64 Outline Validation.html (1 of 9) A hidden input in the XHTML document contains the data of the controls on this page ECMAScript that provides the implementation for the validation controls and for performing the postback

65  2006 Pearson Education, Inc. All rights reserved. 65 Outline Validation.html (2 of 9)

66  2006 Pearson Education, Inc. All rights reserved. 66 Outline Validation.html (3 of 9)

67  2006 Pearson Education, Inc. All rights reserved. 67 Outline Validation.html (4 of 9) ECMAScript that provides the implementation for the validation controls and for performing the postback

68  2006 Pearson Education, Inc. All rights reserved. 68 Outline Validation.html (5 of 9) ECMAScript that provides the implementation for the validation controls and for performing the postback

69  2006 Pearson Education, Inc. All rights reserved. 69 Outline Validation.html (6 of 9) ECMAScript that provides the implementation for the validation controls and for performing the postback

70  2006 Pearson Education, Inc. All rights reserved. 70 Outline Validation.html (7 of 9) ECMAScript that provides the implementation for the validation controls and for performing the postback

71  2006 Pearson Education, Inc. All rights reserved. 71 Outline Validation.html (8 of 9) ECMAScript that provides the implementation for the validation controls and for performing the postback

72  2006 Pearson Education, Inc. All rights reserved. 72 Outline Validation.html (9 of 9) ECMAScript that provides the implementation for the validation controls and for performing the postback

73  2006 Pearson Education, Inc. All rights reserved. 73 21.6 Session Tracking Session Tracking – Tracking individual clients – The request/response system on which the Web operates is facilitated by HTTP HTTP is a stateless protocol – Does not support persistent connections Web servers cannot maintain state information regarding particular clients – A session represents a unique client on a Web site The client will still be recognized as the same user if the client leaves a site and returns later – Each client must identify itself to the server Help the server distinguish among clients

74  2006 Pearson Education, Inc. All rights reserved. 74 21.6.1 Cookies Cookies – Piece of data stored in a small text file on the user’s computer Maintains information about the client during and between browser sessions – Are sent and received as a collection of type HttpCookieCollection – Web Forms can examine the cookies it sent to the client during previous communications – The expiration date determines how long the cookie remains on the client’s computer The Web browser maintains the cookie for the duration of the browsing session if there is no expiration date – Response object’s Cookies property Used to write cookies to a client Used to access cookies – Can be read by an application only if they were created in the domain in which the application is running

75  2006 Pearson Education, Inc. All rights reserved. 75 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.

76  2006 Pearson Education, Inc. All rights reserved. 76 Outline Options.aspx (1 of 4) Declare a RadioButtonList with 5 elements

77  2006 Pearson Education, Inc. All rights reserved. 77 Outline Options.aspx (2 of 4) Declare two hyperlinks Does not cause a postback to occur

78  2006 Pearson Education, Inc. All rights reserved. 78 Outline Options.aspx (3 of 4) (a) (b)

79  2006 Pearson Education, Inc. All rights reserved. 79 Outline Options.aspx (4 of 4) (c) (d)

80  2006 Pearson Education, Inc. All rights reserved. 80 Outline Options.aspx.cs (1 of 3) A data structure to hold book representations Add each book element into the hash table

81  2006 Pearson Education, Inc. All rights reserved. 81 Outline Options.aspx.cs (2 of 3) Determine whether user selected a language Display the appropriate output

82  2006 Pearson Education, Inc. All rights reserved. 82 Outline Options.aspx.cs (3 of 3) Retrieve the value that corresponds to the key contained in language A new cookie object is created to store the language and its corresponding ISBN number The cookie is added to the Cookies collection sent as part of the HTTP response header

83  2006 Pearson Education, Inc. All rights reserved. 83 Outline Recommendations.aspx (1 of 2) Display the recommendations created by the code-behind file

84  2006 Pearson Education, Inc. All rights reserved. 84 Outline Recommendations.aspx (2 of 2)

85  2006 Pearson Education, Inc. All rights reserved. 85 Outline Recommendations.aspx.cs (1 of 2) Retrieves the cookies from the client Add the information in the cookie(s) to the booksListBox

86  2006 Pearson Education, Inc. All rights reserved. 86 Outline Recommendations.aspx.cs (2 of 2)

87  2006 Pearson Education, Inc. All rights reserved. 87 Fig. 21.27 | HttpCookie properties.

88  2006 Pearson Education, Inc. All rights reserved. 88 21.6.2 Session Tracking with HttpSessionState Class HttpSessionState – Provides session-tracking – Every Web Form includes an HttpSessionState object Accessible through property Session of class Page – Can store name–value pairs using method Add – When adding an attribute that has the same name, the object associated with that attribute is replaced

89  2006 Pearson Education, Inc. All rights reserved. 89 21.6.2 Session Tracking with HttpSessionState (Cont.) – Property SessionID Contains the unique session ID – A sequence of random letters and numbers The first time a client connects to server, a unique session ID is created for that client – Property Timeout Specifies the maximum amount of time that an HttpSessionState object can be inactive before it is discarded – Property Count Provides the number of session items contained in a Session object – Property Keys Returns a collection containing all the session’s keys

90  2006 Pearson Education, Inc. All rights reserved. 90 Outline Options.aspx (1 of 4)

91  2006 Pearson Education, Inc. All rights reserved. 91 Outline Options.aspx (2 of 4) Hidden labels to display tracking information

92  2006 Pearson Education, Inc. All rights reserved. 92 Outline Options.aspx (3 of 4) (a) (b)

93  2006 Pearson Education, Inc. All rights reserved. 93 Outline Options.aspx (4 of 4) (c) (d)

94  2006 Pearson Education, Inc. All rights reserved. 94 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.

95  2006 Pearson Education, Inc. All rights reserved. 95 Outline Options.aspx.cs (1 of 3)

96  2006 Pearson Education, Inc. All rights reserved. 96 Outline Options.aspx.cs (2 of 3) Returns the unique session ID

97  2006 Pearson Education, Inc. All rights reserved. 97 Outline Options.aspx.cs (3 of 3) Returns the maximum amount of time that an HttpSessionState object can be inactive before it is discarded Place the language and its corresponding ISBN number in the HttpSessionState object

98  2006 Pearson Education, Inc. All rights reserved. 98 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 String s) as attribute values. This provides you with increased flexibility in determining the type of state information to maintain for clients.

99  2006 Pearson Education, Inc. All rights reserved. 99 Fig. 21.30 | HttpSessionState properties.

100  2006 Pearson Education, Inc. All rights reserved. 100 Outline Recommendations.aspx (1 of 2) Used to present the recommendations to the user

101  2006 Pearson Education, Inc. All rights reserved. 101 Outline Recommendations.aspx (2 of 2)

102  2006 Pearson Education, Inc. All rights reserved. 102 Outline Recommendations.aspx.cs (1 of 2) Provides the number of session items contained in a Session object Indexes the collection containing all the keys in the session to receive the current key

103  2006 Pearson Education, Inc. All rights reserved. 103 Outline Recommendations.aspx.cs (2 of 2) Concatenate and display the string that is the recommendation in the ListBox

104  2006 Pearson Education, Inc. All rights reserved. 104 21.7.1 Building a Web Form That Displays Data from a Database Step 1: Creating the Project Step 2: Creating the Form for User Input (Fig. 21.33) Step 3: Adding a GridView Control to the Web Form Step 4: Adding a Database to an ASP.NET Web Application – Database used by an ASP.NET Web site should be located in the project’s App_Data folder Step 5: Binding the GridView to the Messages Table of the Guestbook Database (Fig. 21.34-37) Step 6: Modifying the Columns of the Data Source Displayed in the GridView (Fig. 21.38) Step 7: Modifying the Way the SqlDataSource Control Inserts Data (Fig. 21.39)

105  2006 Pearson Education, Inc. All rights reserved. 105 Fig. 21.33 | Guestbook application GUI in Design mode. GridView control

106  2006 Pearson Education, Inc. All rights reserved. 106 Fig. 21.34 | Change Data Source dialog in Visual Web Developer.

107  2006 Pearson Education, Inc. All rights reserved. 107 Fig. 21.35 | Configuring the SELECT statement used by the SqlDataSource to retrieve data.

108  2006 Pearson Education, Inc. All rights reserved. 108 Fig. 21.36 | Previewing the data retrieved by the SqlDataSource.

109  2006 Pearson Education, Inc. All rights reserved. 109 Fig. 21.37 | Design mode displaying SqlDataSource control for a GridView. SqlDataSource control

110  2006 Pearson Education, Inc. All rights reserved. 110 Fig. 21.38 | Removing the MessageID column from the GridView.

111  2006 Pearson Education, Inc. All rights reserved. 111 Fig. 21.39 | Setting up INSERT parameters based on control values.

112  2006 Pearson Education, Inc. All rights reserved. 112 21.7.1 Building a Web Form That Displays Data from a Database (Cont.) GridView – DataSourceID property Identifies the data source that is used to fill the GridView with data at runtime – HeaderText property Indicates the text that appears as the column header – By default, this is the name of the column in the data source – Each column is represented as a BoundField

113  2006 Pearson Education, Inc. All rights reserved. 113 21.7.1 Building a Web Form That Displays Data from a Database (Cont.) SqlDataSource – ConnectionString property Indicates the connection that the SqlDataSource control interacts with The value of this property uses an ASP.NET expression, delimited by – SelectCommand, DeleteCommand, InsertCommand and UpdateCommand properties Contain the SELECT, DELETE, INSERT and UPDATE SQL statements, respectively – Parameters that are set programmatically are defined by Parameter elements – Parameters that obtain their values from controls are defined by ControlParameter elements

114  2006 Pearson Education, Inc. All rights reserved. 114 Outline Guestbook.aspx (1 of 7)

115  2006 Pearson Education, Inc. All rights reserved. 115 Outline Guestbook.aspx (2 of 7)

116  2006 Pearson Education, Inc. All rights reserved. 116 Outline Guestbook.aspx (3 of 7) Declare the GridView control Identifies the data source that is used to fill the GridView with data at runtime Properties that set various aspects of the GridView ’s appearance and behavior

117  2006 Pearson Education, Inc. All rights reserved. 117 Outline Guestbook.aspx (4 of 7) Define the columns that appear in the GridView Each BoundField represents a column Define a SqlDataSource control Indicates the connection through which the SqlDataSource control interacts which the database Defines SQL command statements

118  2006 Pearson Education, Inc. All rights reserved. 118 Outline Guestbook.aspx (5 of 7) Indicates the control from which the parameter gets its value Specifies the property that contains the actual value to be used as the parameter value Parameters that obtain their values from controls Parameters that are set programmatically

119  2006 Pearson Education, Inc. All rights reserved. 119 Outline Guestbook.aspx (6 of 7) (a)

120  2006 Pearson Education, Inc. All rights reserved. 120 Outline Guestbook.aspx (7 of 7) (b)

121  2006 Pearson Education, Inc. All rights reserved. 121 21.7.2 Modifying the Code-Behind File for the Guestbook Application SqlDataSource – InsertParameters collection Contains an item corresponding to each parameter in the SqlDataSource ’s INSERT command – Insert method Executes the control’s INSERT command against the database GridView method DataBind – Refreshes the information displayed in the GridView

122  2006 Pearson Education, Inc. All rights reserved. 122 Outline Guestbook.aspx.cs (1 of 2) Assign a string representation of the current data to a new object of type Parameter Remove the Date item in the InsertParameters collection Add our Date parameter

123  2006 Pearson Education, Inc. All rights reserved. 123 Outline Guestbook.aspx.cs (2 of 2) Executes the INSERT command against the database: add a row to the Messages table

124  2006 Pearson Education, Inc. All rights reserved. 124 21.8.1 Examining the Completed Secure Books Database Application Form authentication – Protect a page so that only users known to the site can access it Example of visual inheritance: – Master page defines common GUI elements that are inherited by each page in a set of content pages

125  2006 Pearson Education, Inc. All rights reserved. 125 Fig. 21.42 | Login.aspx page of the secure books database application.

126  2006 Pearson Education, Inc. All rights reserved. 126 Fig. 21.43 | CreateNewUser.aspx page of the secure book database application.

127  2006 Pearson Education, Inc. All rights reserved. 127 Fig. 21.44 | Message displayed to indicate that a user account was created successfully.

128  2006 Pearson Education, Inc. All rights reserved. 128 Fig. 21.45 | Books.aspx displaying books by Harvey Deitel (by default).

129  2006 Pearson Education, Inc. All rights reserved. 129 Fig. 21.46 | Books.aspx displaying books by Andrew Goldberg.

130  2006 Pearson Education, Inc. All rights reserved. 130 Fig. 21.47 | Logging in using the Login control.

131  2006 Pearson Education, Inc. All rights reserved. 131 21.8.2 Creating the Secure Books Database Application Step 1: Creating the Web Site Step 2: Setting Up the Web Site’s Folders Step 3: Configuring the Application’s Security Settings (Fig. 21.49-53) – Access rule grants or denies access to a particular Web application directory for a specific user Step 4: Examining the Auto-Generated Web.config Files – authorization element indicates who is authorized to access a folder over the Web – mode attribute species that we want to use forms authentication – deny element specifies the users to whom we wish to deny access Step 5: Creating a Master Page (Fig. 21.54-56) – Master directive specifies that this file defines a master page – script element stores the code that is usually in the code behind file – ContentPlaceHolder control serves as a placeholder for content that will be defined

132  2006 Pearson Education, Inc. All rights reserved. 132 21.8.2 Creating the Secure Books Database Application (Cont.) Step 6: Creating a Content Page (Fig. 21.57-58) – Content control will place page-specific content that will replace the master page’s ContentPlaceHolder Step 7: Adding a CreateUserWizard Control to a Content Page (Fig. 21.59) Step 8: Creating a Login Page (Fig. 21.60) – ASP.NET writes an encrypted cookie containing data about an authenticated user – Authenticated user is redirected to the page specified by the Login control’s DestinationPageUrl property Step 9: Creating a Content Page That Only Authenticated Users Can Access Step 10: Customizing the Secure Page – LoginName control displays the current authenticated user name

133  2006 Pearson Education, Inc. All rights reserved. 133 21.8.2 Creating the Secure Books Database Application (Cont.) Step 11: Connecting the CreateUserWizard and Login Controls to the Secure Page Step 12: Generating a DataSet Based on the Books.mdf Database – ObjectDataSource encapsulates an object that provides access to a data source Step 13: Creating and Configuring an AuthorsTableAdapter (Fig. 21.61) Step 14: Creating and Configuring a TitlesTableAdapter (Fig. 21.62-63)

134  2006 Pearson Education, Inc. All rights reserved. 134 21.8.2 Creating the Secure Books Database Application (Cont.) Step 15: Adding a DropDownList Containing Authors’ First and Last Names (Fig. 21.64-66) – A business object is an object that accesses data through another object – DropDownList ’s AutoPostBack property indicates that a postback occurs each time the user selects an item Step 16: Creating a GridView to Display the Selected Author’s Books (Fig. 21.67-68) – Enable Sorting causes the column headings in the GridView to turn into hyperlinks that allow users to sort the data – Enable Paging causes the GridView to split across multiple pages – PageSize property determines the number of entries per page Step 17: Examining the Markup in Books.aspx (Fig. 21.69)

135  2006 Pearson Education, Inc. All rights reserved. 135 Fig. 21.48 | Error message displayed for an unsuccessful login attempt using the Login control.

136  2006 Pearson Education, Inc. All rights reserved. 136 Fig. 21.49 | Web Site Administration Tool for configuring a Web application.

137  2006 Pearson Education, Inc. All rights reserved. 137 Fig. 21.50 | Security page of the Web Site Administration Tool.

138  2006 Pearson Education, Inc. All rights reserved. 138 Fig. 21.51 | Choosing the type of authentication used by an ASP.NET Web application.

139  2006 Pearson Education, Inc. All rights reserved. 139 Fig. 21.52 | Main page of the Web Site Administration Tool after enabling forms authentication.

140  2006 Pearson Education, Inc. All rights reserved. 140 Fig. 21.53 | Add New Access Rule page used to configure directory access.

141  2006 Pearson Education, Inc. All rights reserved. 141 Fig. 21.54 | Master page in Source mode.

142  2006 Pearson Education, Inc. All rights reserved. 142 Fig. 21.55 | Master page in Design mode.

143  2006 Pearson Education, Inc. All rights reserved. 143 Outline Bug2Bug.master (1 of 2) Specify the language is C# Display logo picture

144  2006 Pearson Education, Inc. All rights reserved. 144 Outline Bug2Bug.master (2 of 2) Define a ContentPlaceHolder control

145  2006 Pearson Education, Inc. All rights reserved. 145 Fig. 21.57 | Content page CreateNewUser.aspx in Source mode.

146  2006 Pearson Education, Inc. All rights reserved. 146 Fig. 21.58 | Content page CreateNewUser.aspx in Design mode.

147  2006 Pearson Education, Inc. All rights reserved. 147 Outline CreateNewUser.aspx (1 of 3) Specify language, master page, and title Contains several properties that specify formatting styles for the control Elements that define additional styles used to format specific parts of the control Replaces the master page’s ContentPlaceHolder

148  2006 Pearson Education, Inc. All rights reserved. 148 Outline CreateNewUser.aspx (2 of 3) (a) (b) Classes that encapsulate the details of creating a user and issuing a confirmation message

149  2006 Pearson Education, Inc. All rights reserved. 149 Outline CreateNewUser.aspx (3 of 3) (c)

150  2006 Pearson Education, Inc. All rights reserved. 150 Outline Login.aspx (1 of 2) Specify language, master page, and title Replaces the master page’s ContentPlaceHolder Create a Login control The elements define various formatting style applied to parts of the control

151  2006 Pearson Education, Inc. All rights reserved. 151 Outline Login.aspx (2 of 2) (a) (b)

152  2006 Pearson Education, Inc. All rights reserved. 152 Fig. 21.61 | Authors DataTable in the Dataset Designer.

153  2006 Pearson Education, Inc. All rights reserved. 153 Fig. 21.62 | Query Builder for designing a query that selects books written by a particular author.

154  2006 Pearson Education, Inc. All rights reserved. 154 Fig. 21.63 | Dataset Designer after adding the TitlesTableAdapter.

155  2006 Pearson Education, Inc. All rights reserved. 155 Fig. 21.64 | Choosing a business object for an ObjectDataSource.

156  2006 Pearson Education, Inc. All rights reserved. 156 Fig. 21.65 | Choosing a data method of a business object for use with an ObjectDataSource.

157  2006 Pearson Education, Inc. All rights reserved. 157 Fig. 21.66 | Choosing a data source for a DropDownList.

158  2006 Pearson Education, Inc. All rights reserved. 158 Fig. 21.67 | Choosing the data source for a parameter in a business object’s data method.

159  2006 Pearson Education, Inc. All rights reserved. 159 Fig. 21.68 | Completed Books.aspx in Design mode.

160  2006 Pearson Education, Inc. All rights reserved. 160 Outline Books.aspx (1 of 5) Create a LoginName control; displays the authenticated user’s name when the page is requested and viewed in a browser Create a LoginStatus control; redirect user to login page after logging out Indicates that changing the selected item in the list causes a postback to occur Specifies that this ObjectDataSource accesses the Books database by calling GetData of BooksDataSet ’s AuthorsTableAdapter Create the GridView that displays information about the books written by the selected author

161  2006 Pearson Education, Inc. All rights reserved. 161 Outline Books.aspx (2 of 5) Define the ObjectDataSource used to fill the GridView with data Specifies that the value of method GetDataByAuthorID’s parameter comes from the SelectedValue property of authorsDropDownList

162  2006 Pearson Education, Inc. All rights reserved. 162 Outline Books.aspx (3 of 5) (a)

163  2006 Pearson Education, Inc. All rights reserved. 163 Outline Books.aspx (4 of 5) (b)

164  2006 Pearson Education, Inc. All rights reserved. 164 Outline Books.aspx (5 of 5) (c)


Download ppt " 2006 Pearson Education, Inc. All rights reserved. 1 21 ASP.NET 2.0, Web Forms and Web Controls."

Similar presentations


Ads by Google