Chapter 2. Using the Visual Studio .NET Designer

Visual Studio .NET enables you to build Web pages with standard elements, such as forms, hypertext links, and tables. The development environment also provides advanced features, such as support for different browser versions, framesets, Cascading Style Sheets, and client-side scripts. In this chapter, you’ll learn how to take advantage of these features to create the visual portion of the Web pages for your applications.

This chapter is divided into two parts, corresponding to the two methods that Visual Studio .NET supports for creating the visual interface for Web pages. If you prefer, you can build Web pages with Visual Studio .NET by using Design View. When you use Design View, you can quickly create a complicated Web page user interface by using a drag-and-drop interface.

If, on the other hand, you want to get your hands dirty and work directly with HTML code, you can take advantage of HTML View. When you work with HTML using HTML View, you have complete control over your HTML code. When using HTML View, you use the HTML editor that supports such advanced features as HTML validation and automatic formatting.

In this chapter, you will learn

  • How to create Web pages in Design View

  • How to create Web pages with the HTML Editor

  • How to add advanced features to your pages, such as client-side scripts, style sheets, and framesets

Creating a New HTML Page

There are two situations in which you’ll need to work with HTML in your Web applications. First, and most obvious, you’ll need to work with HTML when creating static HTML pages. However, you’ll also need to work with HTML when building Web Form Pages (Web Form Pages are described in detail in Chapter 3, “Creating Basic Web Form Pages”).

You can use the methods for working with HTML described in this chapter when working with either static HTML pages or Web Form Pages. The only difference is that in this chapter you’ll be working with files with the .htm extension rather than the .aspx extension. We’ll be concentrating on static HTML pages in this chapter, but you’ll need to use the same methods when building Web Form Pages in the remainder of this book.

To add a new static HTML page to a project, perform the following steps:

  1. Select Add HTML Page from the Project menu.

  2. Enter a name for the HTML page in the Name text box.

  3. Click Open.

After the new HTML page is created, it will appear in the Solution Explorer window (see Figure 2.1).

HTML page opened in Design View.

Figure 2.1. HTML page opened in Design View.

Note

You should add the HTML page to an open project. To learn how to create new projects with Visual Studio .NET, see the first section of Chapter 1, “Getting Familiar with the Development Environment.”

Previewing an HTML Page in a Browser

While designing an HTML page, you can preview the page in a Web browser. To open an HTML page in a Web browser, right-click the HTML file in the Solution Explorer window and select View in Browser. A new tab will appear with the HTML output of the page.

Instead of previewing an HTML page in the default browser, you can open the page in another browser, such as Netscape Navigator. To open an HTML page in another browser, right-click the HTML page in the Solution Explorer window and select Browse With. A list of all the browsers configured on your machine will appear.

Finally, if you want to view the page using different resolutions, such as 640×480 or 800×400, you can select different browser window sizes in the Browse With dialog box.

Switching Between Design View and HTML View

Visual Studio .NET supplies two interfaces for editing HTML documents—Design View and HTML View. You can use any one of the following methods to switch between the two views:

  • Click the Design or HTML tab at the bottom of the editor window.

  • Select the menu option View, HTML Source in Design View or the menu option View, Design in HTML View.

  • Right-click the editor window and select View HTML Source in Design View or View Design in HTML View.

  • Use the keyboard shortcut Ctrl+PgDn to switch back and forth between Design View and HTML View.

Figure 2.2 illustrates how the editor appears in Design View, and Figure 2.3 illustrates how the editor appears in HTML View.

Viewing an HTML page in Design View.

Figure 2.2. Viewing an HTML page in Design View.

Viewing an HTML page in HTML View.

Figure 2.3. Viewing an HTML page in HTML View.

Tip

You can specify a default view to edit your HTML documents. For example, you might want all of your HTML documents to open in HTML View by default. To set the default view, do the following steps:

  1. Select Tools, Options.

  2. Select the folder labeled HTML Designer.

  3. Select the General page.

Setting the Target Schema Property

Different browser types, such as Internet Explorer and Netscape Navigator, and different browser versions, such as Internet Explorer 3.0 and Internet Explorer 5.0, support different browser features. For example, support for Cascading Style Sheets varies wildly among different types of browsers and browser versions.

Within the Visual Studio .NET environment, you specify the target browser for an HTML document by using the targetSchema property. The targetSchema property determines, among other things, which HTML elements are available in the Toolbox, which properties are displayed for an HTML element, and which HTML attributes are displayed in the HTML editor when using automatic statement completion.

targetSchema can be set to any one of the following values:

  • Internet Explorer 3.02/Navigator 3.0

  • Navigator 4.0

  • Internet Explorer 5.0

One of the most important features that the targetSchema property affects is support for Cascading Style Sheets. Unless the targetSchema is set to a browser that supports HTML 4.0 or later, support for Cascading Style Sheets is disabled.

The targetSchema property also affects whether support for client-side scripts is enabled or disabled. If the targetSchema is set below HTML 4.0, all support for client-side scripts is disabled.

Note

Modifying the targetSchema property alters the design features available in the Visual Studio .NET environment. It does not affect the behavior of Web Server controls. A Web Server control, such as RequiredFieldValidator, will use Cascading Style Sheets and client-side script with Internet Explorer 5.0, even when the targetSchema property is set to HTML 3.0 compatibility.

You can modify the targetSchema property for an HTML document by completing the following steps:

  1. In Design View, right-click the main body of the HTML page and select Properties.

  2. Click the General tab in the DOCUMENT Property Pages dialog box (see Figure 2.4).

    Viewing the DOCUMENT Property Pages dialog box.

    Figure 2.4. Viewing the DOCUMENT Property Pages dialog box.

  3. Select a value in the Target Schema drop-down list.

Alternatively, you can select DOCUMENT from the drop-down list in the Property window and alter the targetSchema property from the property list.

Note

When you modify the targetSchema property, you modify the vs_targetSchema meta tag in the source of the HTML page. In general, you should not edit this or any other Visual Studio .NET meta tag directly in the HTML editor.

Working with HTML Pages in Design View

When working with an HTML page in Design View, you can drag and drop your way to a fancy HTML page. Design View supports two methods of visually designing a page; you can add elements to a page by using Flow layout mode or Grid layout mode.

When you use Flow layout mode, you treat an HTML page just like a normal word processor document, such as Microsoft Word. You can type text directly onto the HTML page. You can also use standard word processor formatting commands to align the text on the page.

Building an HTML page in Grid layout mode, on the other hand, is more like working with a graphic design program, such as Photoshop. In Grid layout mode, you can add elements to a page by using precise x and y coordinates. However, you cannot directly type text onto the page.

Tip

In the “Mixing Flow and Grid Layout Modes” section later in this chapter, you’ll learn how to take advantage of the features of both layout modes in the same page.

In the following sections, you’ll learn how to design pages by using both layout modes.

Selecting Flow or Grid Layout Mode

You can choose to design an HTML page by using either Flow or Grid layout mode by completing the following steps:

  1. In the Properties window, select DOCUMENT from the drop-down list. If the Properties window is not visible, select Properties Window from the View menu.

  2. In the Properties window, set the Page Layout property to either FlowLayout or GridLayout.

It’s important to understand that you can design pages using either Flow or Grid layout modes that are compatible with any browser. In particular, you can design pages by using Grid layout mode that are compatible with browsers that do not support Cascading Style Sheets.

Visual Studio .NET uses different methods to implement Grid layout, depending on the value of the targetSchema property. If targetSchema is set to HTML 4.0 or later, elements are precisely positioned with Cascading Style Sheet style tags. If the targetSchema does not support Cascading Style Sheets, nested HTML tables are used to precisely position elements on the page.

Therefore, as long as you set the targetSchema property appropriately, you should not shy away from using Grid layout mode as a result of browser compatibility worries. Because almost all browsers support table tags, Grid layout is compatible with almost any browser.

Note

When you set the layout mode to Grid layout, a new ms_positioning="GridLayout" attribute is automatically added to the HTML document’s <body> tag. If you switch back to Flow layout, this attribute is automatically removed.

Adding HTML Elements to a Page

After you add an HTML page to your project, you can add HTML elements to the page from the HTML tab on the Toolbox. There are two ways that you can add items from the Toolbox. First, you can double-click an item on the Toolbox, and the item is added at the currently selected spot on the HTML page (the Designer surface). You can also add an item from the Toolbox by dragging and dropping it onto the HTML page.

For example, to add a Label to your HTML page, perform the following steps:

  1. Select the HTML tab on the Toolbox. If the Toolbox is not visible, select the Toolbox from the View menu.

  2. Left-click the Label icon on the Toolbox and drag the Label onto the Designer surface.

After you add the Label, the Designer surface should resemble Figure 2.5.

A Label on a page.

Figure 2.5. A Label on a page.

Note

Adding a Label to an HTML page adds a <div> tag to the page in the background.

After you add a Label to the page, you can modify the text that the Label displays by typing the text directly into the Label. Select the Label on the Designer surface and then click it again. (Be careful to wait a second before you perform the second click.) Now, you can type text into the Label.

Note

If you click a Label too quickly, you’ll open the HTML design window and a client-side event handler will be added to your code. Don’t worry, just click the Design tab at the bottom of the window and you’ll be back where you started.

Notice that you can modify the size of the Label by clicking the Label and pulling on the square handles that appear on the edges of the Label. You can also change the position of the Label by dragging the Label on the page.

Designing an HTML Page in Flow Layout Mode

When Flow layout mode is enabled, you can create an HTML page just as you would create a normal word processing document. You can type text directly onto the Designer surface. When you type a carriage return, a paragraph break is automatically added.

Tip

You can view graphic symbols for paragraph breaks and other elements in Flow layout mode by selecting Details from the View menu.

In Flow layout mode, you can add HTML elements to the page from the Toolbox. However, you cannot precisely position the elements on the page. For example, if you add a Label control, the Label control is added inline with the rest of the text.

Flow layout mode supports a limited number of commands for aligning text. You can use the Align Left, Center, Align Right, and Justify commands.

The alignment commands are available by opening the Format menu and pointing to Justify. You can also access the alignment commands from the Formatting toolbar. If the Formatting toolbar isn’t visible, open the View menu, point to Toolbars, and select Formatting.

Designing an HTML Page in Grid Layout Mode

When Grid layout mode is enabled, you can design an HTML page by providing precise x and y coordinates for all of its elements. In Grid layout mode, you cannot add text directly to an HTML page. Instead, you must add text to elements contained in the page, such as Labels.

In this section, you’ll learn how to take advantage of the features that Visual Studio .NET provides for designing HTML pages in Grid layout mode. You’ll learn how to display a grid and precisely position elements on a page.

Displaying a Grid

If you need to precisely position elements on a page, it’s useful to have a visible grid against which to align elements. You can display a grid while working in Design mode by selecting Show Grid from the Format menu. You also can enable and disable this option by toggling the Show Grid icon on the toolbar.

Note

Displaying a grid adds a new meta tag to your document. When you display a grid, a vs_showGrid meta tag is automatically created.

By default, an 8-pixel by 8-pixel grid is displayed. To set the grid size to a new value, do the following:

  1. Select Options from the Tools menu.

  2. Navigate to the HTML Designer folder.

  3. Display the Display page.

  4. Enter a value for the horizontal and vertical spacing.

For example, Figure 2.6 illustrates how the HTML designer appears when the grid size is changed to a 50-pixel by 50-pixel grid.

Modifying the grid size.

Figure 2.6. Modifying the grid size.

Using Snap to Grid

When Snap to Grid is enabled, all elements added to the HTML designer are automatically moved so that their upper-left corners are aligned against the background grid (even when the grid is not visible). For example, if the grid size is set to 20-pixels by 20-pixels and Snap to Grid is enabled, you’ll never have an element located 25 pixels from the left margin of the page.

To enable Snap to Grid for all new elements added to the page, select Snap to Grid from the Format menu. Alternatively, toggle the Snap to Grid icon on the toolbar. Enabling Snap to Grid does not cause existing page elements to be repositioned; it only applies to new elements.

You also can apply Snap to Grid to a single element on a page without enabling Snap to Grid for the whole page. To snap a single element to the background grid, select the element with your mouse, open the Format menu, point to Align, and select Grid. Snap to Grid also affects the size of an element. When Snap to Grid is enabled and you resize an element on the page, the element is automatically resized to fit on grid lines.

You also can resize an element to fit the background grid when Snap to Grid is disabled. To do this, select an element on the page, open the Format menu, point to Make Same Size, and select Size to Grid.

Aligning Elements in Grid Layout Mode

Grid layout mode supports several commands for precisely aligning multiple elements on a page. You can use these commands to modify the relationship between two or more elements on a page.

To select multiple elements on a page, hold the Shift or Ctrl key while left-clicking the elements with the mouse. Alternatively, you can drag a selection box around the elements that you want to modify with the mouse.

Tip

To unselect one of the selected elements in a group, hold down the Shift key and click that element.

The following is a list of the alignment commands that you can use in Grid layout mode for controlling alignment:

  • Align Lefts—Aligns the left edges of the elements

  • Align Centers—Aligns the centers of the elements changing the horizontal positions of the elements

  • Align Rights—Aligns the right edges of the elements

  • Align Tops—Aligns the top edges of the elements

  • Align Middles—Aligns the centers of the elements changing the vertical positions of the elements

  • Align Bottoms—Aligns the bottom edges of the elements

You can also use the following commands to control the size of elements:

  • Make Same Width—Modifies the elements to have the same width

  • Make Same Height—Modifies the elements to have the same height

  • Make Same Size—Modifies the elements to have the same width and height

The following commands enable you to control the horizontal spacing between elements:

  • Make Horizontal Spacing Equal—When three or more elements are selected, each element is separated horizontally by the same number of spaces.

  • Increase Horizontal Spacing—Increases the horizontal space between elements.

  • Decrease Horizontal Spacing—Removes horizontal spaces between elements.

  • Remove Horizontal Spacing—Removes all horizontal space between elements.

The following commands enable you to alter the vertical spacing between elements:

  • Make Vertical Space Equal—When three or more elements are selected, each element is separated vertically by the same number of spaces.

  • Increase Vertical Spacing—Increases the vertical space between elements.

  • Decrease Vertical Spacing—Decreases the vertical spaces between elements.

  • Remove Vertical Spacing—Removes all vertical spacing between elements.

All of these commands are available from the Format menu. They are also available on the Layout toolbar. (To open the Layout toolbar, open the View menu, point to Toolbars, and select Layout.)

When you select multiple elements, the last element selected is the dominant element. That means that when you select two elements and apply the Align Left command, the second selected element is aligned against the last selected element.

Tip

When aligning elements, you might want to lock one or more elements in place on the design surface. You can do this with the Lock Element command. This command is available by selecting Lock Element from the Format menu. (The command is also available from the Toolbar.)

Layering Elements in Grid Layout Mode

If the document targetSchema property is set to HTML 4.0 or later, you can layer elements on the design surface. In other words, you can place one element directly above or below another (this is also called z-ordering because you are arranging elements on the page’s z-axis).

To layer one element on top of a second element, simply drag the first element over the second element. If you want to change the order of the elements, you can open the Format menu, point to Order, and then select either Bring to Front or Send to Back. Not all browsers support layering. If the targetSchema property is set to HTML 3.0, exclamation marks will appear when you attempt to layer elements (see Figure 2.7).

Invalid layering of elements.

Figure 2.7. Invalid layering of elements.

Mixing Flow and Grid Layout Modes

You can combine the features of Flow and Grid layout mode in a single page by using the Flow Layout Panel and the Grid Layout Panel. Both the Flow Layout and Grid Layout Panels are available under the HTML tab on the Toolbox.

For example, suppose that you are working in Flow layout mode and need to precisely position form fields when constructing an HTML form. In that case, you can add a Grid Layout Panel to the page and add the form elements to the panel (see Figure 2.8).

A Grid Layout Panel in Flow layout mode.

Figure 2.8. A Grid Layout Panel in Flow layout mode.

If you are working in Grid layout mode and discover that you need to add a considerable amount of text to a page, you can add a Flow Layout Panel (see Figure 2.9).

A Flow Layout Panel in Grid layout mode.

Figure 2.9. A Flow Layout Panel in Grid layout mode.

Formatting Text

Visual Studio .NET includes a variety of text formatting commands that you can use regardless of whether you are working in Flow or Grid layout mode. The formatting commands are available from the Format menu and the Formatting toolbar. (If the Formatting toolbar is not visible, open the View menu, point to Toolbars, and select Formatting.)

Visual Studio .NET contains all of the standard formatting commands, such as bold, italic, underline, superscript, and subscript. These commands are available by opening the Format menu and pointing to Font, and are also available on the toolbar.

You can also control text indentation by using the Increase Indent and Decrease Indent commands located from the Format menu or the toolbar.

The Formatting toolbar also includes a Font Name drop-down list for picking a typeface, a Font Size drop-down list for picking a font size, and foreground and background color pickers for choosing text foreground and background colors. (You can’t use background color unless the document targetSchema property is set to a value equal or greater than HTML 4.0.)

Finally, the Formatting toolbar contains a drop-down list for the Block Format. Block Format options include the following:

  • Normal—No formatting applied.

  • Formatted—Text is preformatted with the HTML <pre> tag so that whitespace, such as carriage returns and spaces, is preserved.

  • Address—Text is formatted with the HTML <address> tag to indicate a mailing address.

  • Heading—Text is formatted with <h1>, <h2>, <h3>, <h4>, <h5>, or <h6> tags to create a page heading.

  • Numbered List—Text is formatted with an HTML <ol> tag to create a bulleted list with numbers.

  • Bulleted List—Text is formatted with an HTML <ul> tag to create a bulleted list without numbers.

  • Directory List—Text is formatted with an HTML <dir> tag to create a bulleted list appropriate for a directory listing.

  • Menu List—Text is formatted with an HTML <menu> tag to create a bulleted list appropriate for a menu.

  • Definition Term—Text is formatted with an HTML <dt> tag to create a particular definition in a definition list.

  • Definition—Text is formatted with an HTML <dl> tag to create a definition listing.

  • Paragraph—Text is formatted with an HTML <p> to enclose the text in a paragraph.

You can apply multiple Block Format commands to the same string of text. For example, you can apply Bulleted List formatting to text and change the Font Color to Red. If you want to remove all formatting from a string of text and start fresh, apply Normal formatting to the text.

Setting Background and Margin Properties

You can modify properties of an HTML document, such as its background color and margins, by opening the Property Pages for the document. There are two ways you can open the Property Pages:

  1. Right-click the Designer surface without clicking any elements that you have already added to the surface. Select Properties.

  2. In the Properties window, select DOCUMENT from the drop-down list and click the icon at the top of the Properties window labeled Property Pages.

Either method will open the dialog box displayed in Figure 2.10. The dialog box has three tabs labeled General, Colors and Margins, and Keywords.

Opening DOCUMENT Property Pages.

Figure 2.10. Opening DOCUMENT Property Pages.

The General tab enables you to set properties, such as the page title, page background color, and page background image. If the targetSchema is set to HTML 4.0 or later, this tab can also be used to specify the default client-side scripting language used for the page.

The Colors and Margins tab enables you to specify the default color for text and hypertext links. You can also use the options under this tab to set the top, left, right, and bottom margin sizes.

Finally, the Keywords tab enables you to supply the text used with the keywords meta tag. Some (but not all) search engines use the keywords meta tag when indexing HTML documents.

Adding Images

Adding an image to an HTML page requires the completion of two steps. First, you need to add the image file to your project:

  1. Select Add Existing Item from the Project menu.

  2. In the Add Existing Item dialog box, select All Files (*.*) to display image files.

  3. Browse to the image on your hard drive.

  4. Click Open.

After you add an image to your project, you can add the image to an HTML page by doing the following:

  1. Select Image from the Insert menu.

  2. Click the Browse button to navigate to the image in your project.

  3. Click OK.

The Insert Image dialog box provides you with several options for the layout of the image. You can specify the image alignment, border thickness, and horizontal and vertical spacing around the image. Finally, you can supply alternative text for the image that will appear in browsers that do not support images.

Note

If you insert an image that is not part of your project, the image will not be deployed with your project when you deploy your project to a Web server.

After you add an image to a page, you can automatically add the width and height of the image to the HTML <img> tag by right-clicking the image and selecting Add Height/Width. In general, it’s a good idea to add width and height attributes to an image to render the HTML page containing the image faster.

Adding Hypertext Links

If you want to link one HTML page to another enabling a user to navigate from the first page to the second, you need to create a hyperlink.

Before you can create a hyperlink, you must first select the text or image that you want to use as the label for the hyperlink. Select a string of text or an image with your mouse. Next, select Hyperlink from the Insert menu.

Note

In Grid layout mode, you’ll need to select text that is contained within an HTML element, such as a Label, Flow Layout Panel, or Table element.

Selecting Hyperlink from the Insert menu causes the Hyperlink dialog box to appear (see Figure 2.11). You can use this dialog box to browse to a page to link to the current document.

Opening the Hyperlink Dialog Box.

Figure 2.11. Opening the Hyperlink Dialog Box.

Adding a Bookmark

A bookmark, also known as a named anchor, enables you to name an area of a page. After you create a bookmark, you can create a hyperlink that navigates to the bookmark.

To create a bookmark, select some text on the Designer surface and select Bookmark from the Insert menu. Selecting this menu option opens a dialog box that enables you to enter a name for the bookmark. You can provide any name for a bookmark that you please.

After you create a bookmark, you can create a hyperlink that links to the bookmark by doing the following:

  1. Select a string of text or an image on the HTML page.

  2. Select Hyperlink from the Insert menu.

  3. In the Hyperlink dialog box, choose (other) as the Type of the hyperlink.

  4. In the URL text box, enter the name of a bookmark preceded by a pound sign (#). For example, #mybookmark.

  5. Click OK.

You can also create a hyperlink that links to a bookmark on another page. For example, to create a hyperlink to a bookmark named mybookmark on a page named SomePage.htm, you would supply the following URL for the hyperlink:

/SomePage.htm#mybookmark

Adding Form Elements

You can drag and drop all of the standard HTML form elements—such as text fields, check boxes, and submit buttons—from the HTML Toolbox onto the design surface.

Typically, before you add form elements to an HTML page, you’ll first want to add the HTML <form> tag itself to the page. To add opening and closing HTML <form> tags to a page, select FORM from the Insert menu.

Note

In Grid layout mode, the FORM command is available on the Insert menu only when you select an area in an HTML element, such as a Label or Flow Layout Panel.

You can view the locations of the opening and closing <form> tags by selecting Details from the View menu. You’ll want to be careful to add any form elements between the opening and closing <form> tags.

Adding Text Fields

You can add a text field to a form by selecting a point within the opening and closing <form> tags and double-clicking the Text Field element in the HTML Toolbox. Adding a text field adds a single-line text box.

You can supply the following attributes for a text field:

  • maxlengthThe maximum number of characters that can be entered into the text field

  • nameThe name of the text field

  • sizeThe width of the text field in characters

  • valueThe default value of the text field

Adding Password Fields

Password fields are similar to text fields, with the exception that the text in a password field is automatically hidden. Any text you enter into a password field is echoed with asterisks.

A password field supports the same attributes as a text field:

  • maxlengthThe maximum number of characters that can be entered into the password field

  • nameThe name of the password field

  • sizeThe width of the password field in characters

  • valueThe default value of the password field

Adding Text Areas

A text area is a multi-line text box. To add a text area to a form, select a point within the opening and closing form tags and double-click the Text Area element in the HTML Toolbox.

After you add a text area to a form, you can supply values for the following attributes:

  • colsThe width of the text area in characters.

  • nameThe name of the text area.

  • rowsThe height of the text area in characters.

  • wrapThe wrap mode of the text area. To enable word wrapping, set wrap to either soft or hard. To disable word wrapping, set wrap to off.

Note

You can’t supply a maximum length for a text area.

Adding Radio Buttons

Normally, you add radio buttons in groups. Only one radio button in a group of radio buttons can be checked at a time.

To add a radio button to a form, select an area within the opening and closing <form> tags and double-click the Radio Button element in the HTML Toolbox. You can supply values for the following properties of a radio button:

  • checkedThe default value of the radio button. If True, the radio button appears checked by default.

  • nameThe name of the radio button. Radio buttons with the same name are placed in the same group.

  • valueThe value submitted by the form when the radio button is checked.

Adding Check Boxes

Like radio buttons, you typically add check boxes to a form in groups. Unlike radio buttons, however, multiple check boxes can be checked at one time.

To add a check box to a form, select an area within the opening and closing <form> tags and double-click the Checkbox element in the HTML Toolbox. You can specify the following values for a check box:

  • checkedThe default value of the check box. If True, the check box appears checked by default.

  • nameThe name of the check box. Check boxes with the same name are placed in the same group.

  • valueThe value submitted by the form when the check box is checked.

Adding Drop-Down Lists and List Boxes

To add either a drop-down list or list box to a form, select a point between the opening and closing tags of the form and double-click either the DropDown or Listbox elements in the HTML Toolbox.

After you add a drop-down list or list box, you can add options to the element by right-clicking the element and selecting Properties. Selecting Properties opens a dialog box that enables you to insert a text and value for each option in the drop-down list or list box (see Figure 2.12).

Adding list items to a list box.

Figure 2.12. Adding list items to a list box.

Adding Buttons

There are three types of buttons that you can add to a form from the HTML Toolbox—Submit buttons, Reset buttons, and buttons. To add any of these three types of buttons to a form, select a point within the opening and closing <form> tags and double-click the Button element in the HTML Toolbox.

All three types of buttons support the following properties:

  • nameThe name of the button

  • sizeThe width of the button in characters

  • valueThe text displayed on the button

Adding File Fields

A file field enables you to upload files from a client computer to the Web server. When you add a file field, a text box and Browse button are added to the form.

To add a file field to a form, select a point within the opening and closing <form> tags and double-click the File Field element in the HTML Toolbox.

Adding Hidden Fields

You can use a hidden field to pass additional information to the Web server when the form containing the hidden field is submitted. To add a hidden field to a form, select an area within the opening and closing form tags and double-click the Hidden Field element in the Toolbox.

Hidden fields support the following properties:

  • nameThe name of the hidden field

  • sizeThe size in characters of the hidden field

  • valueThe text contained in the hidden field

Adding Tables

You can add an HTML table to a page by dragging and dropping the Table element from the HTML Toolbox. By default, a table added from the Toolbox contains three columns and three rows.

Alternatively, you can add a table by opening the Table menu, pointing to Insert, and selecting Table. Selecting this menu option provides you with more options for initially configuring the table (see Figure 2.13).

Setting Table options.

Figure 2.13. Setting Table options.

After you add a table, you can add and delete table columns, rows, and cells from the Table menu. For example, to add a new row to a table, select the table, open the Table menu, point to Insert, and select Row Below.

You can change properties of a table, such as the height, width, cell padding, cell spacing, and border colors of the table by opening the table’s Property Pages. You open a table’s Property Pages by right-clicking the table and selecting Properties.

Tip

You can display borders for tables that have invisible borders by selecting Visible Borders from the View menu.

You can also modify the properties of a particular column, row, or cell by selecting the column, row, or cell and selecting Select from the Table menu. After you select the table element, open the Property Pages for the element by right-clicking and selecting Properties.

Adding Framesets

Framesets enable you to display multiple HTML pages in a single window. Visual Studio .NET enables you to easily work with frames by supplying standard frameset templates.

To add a frameset to a project, do the following:

  1. Select Add New Item from the File menu.

  2. In the Add New Item dialog box, select the frameset template.

  3. In the Name text box, specify a name for the frameset (for example, myFrameset.htm).

  4. Click Open and the Select a Frameset Template dialog box appears (see Figure 2.14).

    Selecting a frameset template.

    Figure 2.14. Selecting a frameset template.

  5. Select a frameset template, such as Banner and Content or Horizontal Split, and click OK.

After completing these steps, the frameset will appear in Design View. Before you can use the frameset, you’ll need to assign files to each frame in the frameset.

Assigning Pages to Frames

After you create a frameset, you can assign existing files to the individual frames in the frameset. For example, to assign a file named myPage.htm to a frame, do the following:

  1. Right-click inside a frame on the Designer surface.

  2. Select Set Page for Frame, which opens the Select Page dialog box.

  3. Enter the path to the myPage.htm file in the URL text box and click OK.

After you assign a file to a frame, the file will appear in the frame when you view the frameset in a browser.

Linking to a Page in a Frame

Framesets are commonly used for displaying both a menu and content page in the same window. For example, in a frameset with two frames, you might place a menu of links in the left frame that open pages in the right frame (see Figure 2.15). To open a page in a particular frame, you must include the name of the frame in the link.

Displaying a menu and content page with a frameset.

Figure 2.15. Displaying a menu and content page with a frameset.

To name a frame, perform the follow steps:

  1. Left-click inside the frame.

  2. In the property window, find the name property and supply a name (for example, contents).

After you name a frame, you can create a link to the frame by doing the following:

  1. Select the HTML page that will contain the link.

  2. Add a Label element to the HTML page by double-clicking the Label element on the HTML Toolbox.

  3. Click inside the Label and type the text for the label (for example, Products).

  4. Select the text by double-clicking the text with the left mouse button.

  5. Select Hyperlink from the Insert menu.

  6. Enter the path to the page that you want to open in the frame in the URL text box and click OK.

  7. Click the link and find the target property in the Properties window.

  8. For the target property, enter the name of the frame into which you want the link to open the page.

After you complete these steps, clicking the link will open a page into a new frame.

Modifying Frameset Properties

After you create a frameset, you can modify its properties by selecting options from the Frames menu. For example, selecting Seamless Join Between Frames hides the edges between frames.

You can also use the options under the Frames menu to delete and add frames to the current frameset. You’ll need to use these options to create a frameset that is not included in the standard set of frameset templates.

Working with HTML Pages in HTML View

Visual Studio .NET provides you with a choice when building HTML pages. Some HTML developers prefer to work directly with the HTML source of a page rather than working in the context of a visual designer. If you want to work directly with the HTML content of a page, you need to switch to HTML View by clicking the HTML tab.

HTML is outside the scope of this book. In the following sections, you’ll learn how to take advantage of the features of the HTML editor when writing HTML pages.

Taking Advantage of Automatic Statement Completion

When you start adding a new HTML tag in the HTML editor, a pop-up window appears that contains a list of valid HTML tags by default. If you select an HTML tag from the list, the tag is automatically added to the document.

Furthermore, if you select a tag that requires a closing tag, the closing tag is also added to the editor. For example, adding an opening <b> tag automatically adds a closing </b> tag.

You can also configure the HTML editor to automatically add quotation marks around the values that you assign to attributes in an HTML tag. This option is disabled by default.

You can disable or enable any of these options from the Options dialog box by doing the following

  1. Select Options from the Tools menu.

  2. Select the Text Editor folder.

  3. Navigate to the HTML/XML folder.

  4. Display the HTML Specific page.

From the HTML Specific page, you can modify the following options:

  • To disable automatic HTML statement completion, uncheck the Auto pop-up HTML statement completion check box.

  • To disable the automatic addition of HTML closing tags, uncheck the Close tag check box.

  • To enable the automatic addition of quotes around attribute values, check the Attribute Value Quotes check box.

Enabling and Disabling Automatic Formatting

You can automatically format the source code of an HTML document by opening the Edit menu, pointing to Advanced, and selecting Format Document. You can also apply automatic formatting to a page by clicking the Format the Whole Document button on the HTML Editor toolbar. When you format a document, tags are automatically indented.

For example, if you start with HTML code that looks like the following:

<table><tr>
<td>Hello</td>
</tr>
</table>

After you apply automatic formatting, you get HTML code that looks like the following:

      <table>
           <tr>
               <td>Hello</td>
           </tr>
       </table>

You can control when automatic formatting is applied and particular automatic formatting options from the Format page in the Options dialog box (see Figure 2.16). To open the Format page, do the following:

Modifying options for automatic formatting.

Figure 2.16. Modifying options for automatic formatting.

  1. Select Options from the Tools menu.

  2. Navigate to the Text Editor folder.

  3. Navigate to the HTML/XML folder.

  4. Display the Format page.

You can use the Format page to choose the circumstances under which automatic formatting is applied and the type of formatting that is applied. You can set the following options:

  • Apply Automatic Formatting When Saving Document—Applies automatic formatting when the document is saved

  • Apply Automatic Formatting When switching from Design to HTML/XML View—Applies automatic formatting when switching between Design and HTML View

  • Apply Line Breaks—Add line breaks when applying automatic formatting

  • Insert Attribute Value Quotes—Add quotation marks around HTML tag attribute values

You can also control capitalization for HTML tags and attributes. For example, you can indicate that you want all HTML tags added to a page to be lowercase and all HTML attributes to be uppercase.

Validating HTML Documents

When you add HTML tags to a page, the HTML is validated against the selected targetSchema by default. Any content that can’t be validated appears with a red squiggly line beneath it. If you hover the mouse over the content, a ToolTip appears with an explanation for the validation failure.

For example, the following tags would raise a validation error:

<table><td>

Because you can’t place a <td> tag directly within a <table> tag, the validation error “Per the active schema, the element ‘td’ cannot be nested within ‘table’” appears.

You can turn off automatic validation by completing the following steps:

  1. Select Options from the Tools menu.

  2. Navigate to the Text Editor folder.

  3. Navigate to the HTML/XML folder.

  4. Display the HTML Specific page.

  5. Uncheck the check box labeled Enable HTML validation.

You can also validate an HTML document at any time by clicking the Validate Document button on the HTML Editor toolbar.

Using the Document Outline

Regardless of whether you are building an HTML page in Design or HTML View, you can take advantage of the Document Outline to quickly navigate to particular elements in an HTML page. To display the Document Outline window, open the View menu, point to Other Windows, and select Document Outline.

You can use the Document Outline window to view either an outline of the HTML elements in a page or an outline of the scripts in a page. To switch between the two outlines, click either the HTML Outline or Script Outline button at the top of the Document Outline window.

Tip

You can synchronize the Document Outline with the current document by selecting Synchronize Document Outline from the View menu.

Figure 2.17 illustrates the appearance of the Document Outline window when HTML Outline is selected. You can click any element to navigate directly to the element in the editor window.

Displaying the Document Outline window.

Figure 2.17. Displaying the Document Outline window.

Adding Client-Side Scripts

You can automatically add client-side script event handlers to an HTML page within the Visual Studio .NET environment. However, it is important to understand that different event handlers are available, depending on the value of the targetSchema property.

Another document property, the defaultClientScript property, determines the default client-side script language used with an HTML page. You can assign the value JScript or VBScript to this property.

There are two ways that you can automatically add client-side script event handlers to an HTML page. You can add a client event handler by using the Document Outline window or by using the Event drop-down list.

To add an event handler by using the Document Outline window, perform the following steps:

  1. If the Document Outline window is not already open, open the View menu, point to Other Window, and select Document Outline.

  2. Click the button labeled Script Outline at the top of the Document Outline window.

  3. Expand an HTML element in the Document Outline window to see a list of events associated with the element.

  4. Double-click the name of an event to automatically add a client-side script handler to the editor.

You can also add new client-side script event handlers to a page by taking advantage of the Client Objects & Events and the Events drop-down lists at the top of the HTML editor window. These drop-down lists are visible only when HTML View is enabled (see Figure 2.18).

Adding client-side event handlers.

Figure 2.18. Adding client-side event handlers.

For example, to add an event-handler for the window_onload event, select Window from the Client Objects & Events drop-down list and onload from the Events drop-down list.

Tip

When working with client-side scripts in the HTML editor, you can hide all the HTML content in the page by clicking the Script Only View button at the top-right of the HTML editor window.

Adding Style Sheets

When the document targetSchema is set to the value HTML 4.0 or later, you can use Cascading Style Sheet attributes to format the elements of an HTML page. There are three ways that you can add style attributes to a page. You can add style attributes inline, within a <style> block on a page, or within an external style sheet.

Adding Style Attributes Inline

You can add style attributes to an individual HTML element in Design View. For example, suppose that you want to create a Button element with a red italic font and a blue background. To add these style attributes to a button, do the following:

  1. Select the button in Design View.

  2. Select Build Style from the Format menu (alternatively, right-click the button and select Build Style).

  3. In the Style Builder dialog box (see Figure 2.19), select the Font icon and select the color Red and enable italics.

    The Style Builder dialog box.

    Figure 2.19. The Style Builder dialog box.

  4. In the Style Builder dialog box, select the Background icon and select the color Blue.

You can modify the style attributes for an element at any time in the future by opening the Style Builder dialog box.

Tip

To apply style attributes to text in Design View, add either a DIV or SPAN element by selecting DIV or SPAN from the Insert menu. Next, apply the style attributes to the DIV or SPAN attribute.

Adding a Style Block to a Page

Instead of adding style attributes to individual elements in an HTML page, you can add one <style> block that contains generalized style rules for the page. To add a <style> block to a page, follow these steps:

  1. In Design View, select Document Styles from the Format menu.

  2. In the Document Styles window, click the Add Style Sheet button.

Next, to select the element, class, or element ID to format with a style rule, do the following:

  1. In the Document Styles window, click the Add Style Rule button.

  2. In the Add Style Rule dialog box, select an HTML element, enter a classname, or enter an element ID.

Finally, you can specify the style rule to apply to the element, class, or element ID by performing the following steps:

  1. Select an element, class, or element ID from the appropriate folder in the Document Styles window.

  2. Click the Build Style button.

  3. In the Style Builder dialog box, select the formatting for the element, class, or element ID.

Creating External Style Sheets

You can create a single external style sheet and apply the style sheet to multiple pages within a project. This is useful when you want to create a consistent look for all the pages in your application.

When you create a new ASP.NET Web Application, a single external style sheet file is created by default. You can work with this style sheet file or create a new one.

To create a new external style sheet, do the following:

  1. Select Add New Item from the File menu.

  2. Select the Style Sheet template in the Add New Item dialog box.

  3. Name the style sheet anything you want by entering a name in the Name text box.

  4. Click Open.

After you open an external style sheet file in the editor (see Figure 2.20), you can modify the style rules manually or with the Style Builder dialog box.

Editing an external style sheet.

Figure 2.20. Editing an external style sheet.

To edit a style rule with the Style Builder dialog box, select an element in the CSS Outline window and select Build Option from the Styles menu. You can use the options in the Style Builder dialog box to specify the formatting associated with the element.

After you create an external style sheet, you can apply the style sheet to one or more HTML files by doing the following:

  1. Open an HTML page in Design View.

  2. Select Document Styles from the Format menu.

  3. Click the Add Style Link button in the Document Styles dialog box.

  4. Select the external style sheet that you want to link to the current HTML page from the Select Style Sheet dialog box.

Completing these steps adds a link tag to the HTML page that looks something like the following:

<LINK href="Styles.css" type=text/css rel=stylesheet>

Summary

In this chapter, you learned how to build HTML pages by using Visual Studio .NET. First, you learned how to create HTML pages in Design View. You learned how to design HTML pages by using either Flow layout or Grid layout mode.

Next, you learned how to work directly with HTML code by taking advantage of HTML View. You learned how to enable and disable various automatic formatting options available with the HTML editor.

Finally, you learned how to work with two advanced features of HTML pages. You learned how to add both client-side scripts and Cascading Style Sheets to an HTML page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset