t u t o r i a l 2009 pearson education, inc. all rights reserved. 1 29 bookstore web application:...

42
T U T O R I A L 2009 Pearson Education, Inc. All rights rese 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

Upload: andrew-stephens

Post on 19-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

T U T O R I A L

2009 Pearson Education, Inc. All rights reserved.

1

29Bookstore

Web Application: Client Tier

Introducing Web Controls

Page 2: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

2

Outline

29.1 Analyzing the Bookstore Web Application

29.2 Creating ASPX Pages

29.3 Designing the Books.aspx Page

29.4 Designing the BookInformation.aspx Page

Page 3: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

3

In this tutorial you will learn: ■ Create an ASP.NET Web Site project in Visual

Web Developer 2008 Express.■ Create and design ASPX pages.■ Use Web Form controls.■ Specify the look-and-feel of controls, using the CssStyle attribute and control properties.

Objectives

Page 4: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

4

When the Books page is requestedRetrieve the book titles from the databaseDisplay book titles in a ListBox

When the user selects a book title from the ListBox Display the book’s cover image below the ListBox in an

Image

When the user clicks the View Information ButtonIf the user did not select a book from the ListBox

Display an error messageOtherwise

Store the selected book’s product IDRedirect the browser to the BookInformation page

29.1 Analyzing the Bookstore Web Application

Page 5: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

5

When the BookInformation page is requestedRetrieve the selected book’s information from the

database Display the book title in a LabelDisplay the authors in a LabelDisplay the cover art in an ImageDisplay the remaining information in a DetailsView

When the user clicks the Return to Book List Button on the BookInformation page

Redirect the browser back to the Books page

29.1 Analyzing the Bookstore Web Application

Page 6: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

6

Action Control/Object Event

Label the Books page availableLabel, instructionsLabel

Page Load (for Books.aspx)

Retrieve the book titles from the database

linqDataSource

Display book titles in a ListBox bookTitlesListBox

bookTitlesListBox SelectedIndexChanged

Display the book’s cover image below the ListBox in an Image

bookTitlesListBox, db (an object of class BookInformationDBDataContext), coverImage

■ This ACE table (Fig. 29.1) lists the actions, controls and events that help you complete your own version of this application.

Figure 29.1 | ACE table for the web-based Bookstore web application. (Part 1 of 3.)

Action/Control/Event (ACE) Table for theBookstore Web Application

Page 7: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

7

Action Control/Object Event

informationButton Click

I f the user did not select a book from the ListBox

bookTitlesListBox

Display an error message bookRequiredFieldValidator

Otherwise

Store the selected book’s product ID in a session variable

bookTitlesListBox, Session

Redirect the browser to the BookInformation page

Response

Action/Control/Event (ACE) Table for theBookstore Web Application (Cont.)

Figure 29.1 | ACE table for the web-based Bookstore web application. (Part 2 of 3.)

Page 8: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

8

Action Control/Object Event

Page Load (for Book-Information.aspx)

Retrieve the selected book’s information from the database

db (an object of class BookInformationDBDataContext), Session

Display the book title in a Label bookTitleLabel

Display the authors in a Label authorsLabel

Display the cover art in an Image bookImage

Display the remaining information in a DetailsView

linqDataSource, bookDetailsView

bookListButton Click

Redirect the client browser back to the Books page

Response

Action/Control/Event (ACE) Table for theBookstore Web Application (Cont.)

Figure 29.1 | ACE table for the web-based Bookstore web application. (Part 3 of 3.)

Page 9: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

9

■ In Visual Web Developer, select File > New Web Site....

■ Select ASP.NET Web Site in the Templates: pane (Fig. 29.2).

■ Then select File System in the Location: and choose a location on your local disk drive. Select Visual Basic for the Language.

Creating an ASP.NET Web Site Project

Page 10: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

10

Creating an ASP.NET Web Site Project (Cont.)

Figure 29.2 | Creating an ASP.NET Web Site in Visual Web Developer.

ASP.NET Web Site template

Type C:\SimplyVB2008\Bookstore in this TextBox

Location where projectwill be created

Page 11: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

11

■ The Solution Explorer window for the web application is shown in Figure 29.3.

■ Default.aspx is the default name for the ASPX page and web.config specifies configuration options.

Figure 29.3 | Solution Explorer window for the Bookstore project.

Project name and location

Default ASPX page name

Creating an ASP.NET Web Site Project (Cont.)

Page 12: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

12

■ ASPX pages can be customized by using web controls, which are used in in much the same way as Windows controls.

■ Figure 29.4 shows the Standard controls listed in the Toolbox.

Creating an ASP.NET Web Site Project (Cont.)

Figure 29.4 | Web Forms tab in Toolbox.

Standard tab

Page 13: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

13

■ When you create a web application, an ASPX page is displayed in the Web Form Designer (Fig. 29.5).

■ The Web Form Designer contains three viewing modes:

– Design mode (Fig. 29.5) is a WYSIWYG (what you see is what you get) editor.

– Use Design mode to create the GUI by dragging and dropping web controls.

Figure 29.5 | Design mode of Web Form Designer.

Source mode

Creating an ASP.NET Web Site Project (Cont.)

Split mode

Design mode (selected)

Page 14: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

14

– Source mode shows the ASP.NET markup code.

– Click to enter Split mode, which shows both modes.

■ ASPX pages are defined using a combination of HTML and ASP.NET markup.

■ The ASP.NET web controls that you add to your page are placed in the div element.

Creating an ASP.NET Web Site Project (Cont.)

Page 15: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

15

Creating an ASP.NET Web Site Project (Cont.)

Figure 29.6 | Split mode of Web Form Designer.

This delimiter is highlighted bydefault in Visual Web Developer

(This is an example of ASP markup

To display line numbers, select Tools > Options..., then uncheckthe Show all settings CheckBox in

the dialog that appears. Select the General category and check the

Line numbers CheckBox.

HTML markup

div element in Design view

Split mode (selected)

Page 16: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

16

■ Select the Default.aspx file in the Solution Explorer window and rename it to Books.aspx.

■ Change Untitled Page’s Title to Book List.

Figure 29.7 | Setting the Title property of Books.aspx.

Component Object Box displays DOCUMENT for

ASPX pages

Title property changed to Book List

Creating the Books.aspx Page

Page 17: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

17

Good Programming Practice

Change the ASPX page’s name to a unique and meaningful name for easy identification.

Page 18: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

18

■ Select Design mode in the IDE, then select Format > New Style... to display the New Style dialog.

■ Select body from the Selector ComboBox, then click Background in the Category ListBox.

– Type #CCFFFF into the background-color ComboBox and click OK (Fig. 29.8).

Creating the Books.aspx Page (Cont.)

Page 19: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

19

Figure 29.8 | Setting the background color of the page.

style elementcontaining a style for the

page’s body element

Creating the Books.aspx Page (Cont.)

div element in which ASP.NET web controls are placed

background color of page changed to #CCFFFF

Page 20: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

20

Portability Tip

When specifying colors for web applications, it is best to use a color from the web-safe colors shown in the More Colors dialog to ensure that colors display correctly in a web browser.

Page 21: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

21

■ Select the dotted rectangle that represents the div element, then select Format > New Style....

■ In the dialog, check the Apply new style to document selection CheckBox.

■ Replace the default style name (.newStyle1) with .mainDiv.

■ Click Block for the Category, then select center from the text-align ComboBox.

Creating the Books.aspx Page (Cont.)

Figure 29.9 | div element after applying a style.

div with cursor showing that the div’s content will be

centered

Page 22: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

22

Common Programming Error

Not checking the CheckBox Apply new style to document selection causes the style to be created but not applied to any controls.

Page 23: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

23

■ With the dotted rectangle selected, double click the Label web control in the Toolbox (Fig. 29.10).

■ In the Properties window, set the ID property to availableLabel.

■ Change the Label’s Text property from Label to Available Books.

Creating the Books.aspx Page (Cont.)

Figure 29.10 | Label control displayed in the ASPX page.

Label web control

Page 24: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

24

■ Apply a new style to the Label named style.pageTitle.

■ Set the Label’s font-family to Arial, Helvetica, sans-serif and set its font-size to XX-Large (Fig. 29.11).

Figure 29.11 | availableLabel control after applying its style.

availableLabel control after applying its style

Creating the Books.aspx Page (Cont.)

Page 25: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

25

■ Place the cursor to the right of the availableLabel and press Enter, then create another Label with the ID instructionsLabel (Fig. 29.12).

■ Apply a style named .instructions to this Label with font-size Medium.

– Select Box in the Category list, then uncheck the Same for all CheckBox.

– Set the top and bottom margins to 10.

Creating the Books.aspx Page (Cont.)

Figure 29.12 | instructionsLabel control.

instructionsLabel control after applying its style

Page 26: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

26

■ Add a ListBox control with the ID bookTitlesListBox.

■ Apply a new style named .bookList (Fig. 29.13). – Select Box in the Category list, then uncheck the

Same for all CheckBox for the margin and set the top and bottom margins to 10.

– Next, Select Position in the Category list, and set the width to 350.

Creating the Books.aspx Page (Cont.)

Figure 29.13 | bookTitlesListBox control.

bookTitlesListBox control after applying its style

Page 27: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

27

■ Add an Image control with the ID coverImage.

■ Set its BorderStyle to Outset and its BorderWidth to 5 pixels.

■ Set the Image’s Visible property to False(Fig. 29.14).

Figure 29.14 | coverImage control.

coverImage control with no image displayed

Creating the Books.aspx Page (Cont.)

Page 28: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

28

■ Add a Button control with the ID informationButton.

■ Apply a new style named .viewInfoButton (Fig. 29.15).

– Select Box in the Category list, uncheck the Same for all CheckBox for the margin.

– Set the top, right, bottom and left margins to 10, 0, 10 and 0, respectively.

Creating the Books.aspx Page (Cont.)

Page 29: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

29

Figure 29.15 | informationButton control.

informationButton control after applying its style

Creating the Books.aspx Page (Cont.)

Page 30: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

30

■ Add the RequiredFieldValidator control from the Validation group in the

Toolbox.

■ Change the RequiredFieldValidator’s ID to bookRequiredFieldValidator.

■ Apply a new style named .validatorText.

■ Set the font-size to large and set the font-weight to bold.

Creating the Books.aspx Page (Cont.)

Page 31: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

31

■ Set the control’s ControlToValidate property to bookTitles ListBox.

– This triggers the error message when nothing is selected.

■ Set the ErrorMessage property to Please select a book (Fig. 29.16).

Figure 29.16 | Books.aspx page after all controls have been added.

bookRequiredFieldValidator control after configuring its

ErrorMessage property

Creating the Books.aspx Page (Cont.)

Page 32: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

32

Common Programming Error

Not setting the ControlToValidate property of a validation control results in an application error when you run your ASP.NET application.

Page 33: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

33

■ Select Website > Add New Item....

■ Select Web Form in the Templates: pane, and rename the ASPX page to BookInformation.aspx (Fig. 29.17).

■ Make sure Visual Basic is selected in the Language: drop-down list, then click Add.

Creating the BookInformation.aspx Page

Page 34: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

34

Figure 29.17 | Add New Item - Bookstore dialog.

Change the Name to BookInformation.aspx

Creating the BookInformation.aspx Page (Cont.)

Web Form template

Page 35: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

35

■ Change the background color of this page to light blue.

■ Set the page’s Title property to Book List.

■ Create a new Label with the ID bookTitleLabel.

■ Apply a new style named .bookTitle (Fig. 29.18).– Set the font-family to Arial, Helvetica, sans-

serif.

– Set the font-size to x-large, the font-weight to bold and the color to #0000FF.

Creating the BookInformation.aspx Page (Cont.)

Figure 29.18 | bookTitleLabel control.

bookTitleLabel control after applying its style

Page 36: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

36

■ Add another Label with the ID authorsLabel and clear its Text property.

■ Apply a new style named .authors (Fig. 29.19).– Set the font-size to large and the font-weight to

bold.

Figure 29.19 | authorsLabel control.

authorsLabel control after applying its style

Creating the BookInformation.aspx Page (Cont.)

Page 37: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

37

■ Add an Image with the ID bookImage.

■ Set the BorderStyle to Outset and theBorderWidth to 5 pixels.

■ Apply a new style named .bookCover (Fig. 29.20).– In the Box category, set the top, right, bottom and

left margins to 10, 10, 10 and 0.

– In the Layout category, set the float to left.

Creating the BookInformation.aspx Page (Cont.)

Figure 29.20 | bookImage control.

bookImage control after applying its style

Page 38: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

38

■ Add the DetailsView control from the Toolbox’s Data group.

– Change the ID to bookDetailsView and clear the Width and Height properties.

■ Apply a style named .detailsView (Fig. 29.21).– In the Box category, set the top, right, bottom and

left margins to 10, 10, 10 and 0.

– In the Border category, set the border-style to outset and the border-width to 5.

– In the Position category, set the width to 350.

Creating the BookInformation.aspx Page (Cont.)

Page 39: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

39

Figure 29.21 | bookDetailsView control.

bookDetailsView controlafter applying its style

Creating the BookInformation.aspx Page (Cont.)

Page 40: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

40

■ Select the DetailsView, then open the DetailsView Tasks menu (in the upper-right corner of the control).

■ Click the Auto Format... link to display the AutoFormat dialog, then select Classic (Fig. 29.22).

Creating the BookInformation.aspx Page (Cont.)

Figure 29.22 | bookDetailsView control with auto formatting applied.

bookDetailsView controlafter auto formatting

Page 41: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

41

■ Add a Button with the ID bookListButton (Fig. 29.23).

■ Select the Button and open the New Style dialog. – Create a style named .returnButton.

– In the Box category, set the top margin to 10.

– In the Layout category, set clear to both.

Creating the BookInformation.aspx Page (Cont.)

bookListButton control after applying its style

Figure 29.23 | bookListButton control.

Page 42: T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 29 Bookstore Web Application: Client Tier Introducing Web Controls

2009 Pearson Education, Inc. All rights reserved.

42

■ Run your application (Fig. 29.24).

■ The ListBox does not contain any book titles because you have not yet set up the database connections.

Running the Bookstore Web Application

Figure 29.24 | Empty ListBox of the Books.aspx page.

Empty ListBox control