Partilhar via


Walkthrough: Editing HTML Tables in Visual Web Developer

Table editing is an important part of many Web pages because tables are used to create the page layout. This walkthrough illustrates the table-editing features of the HTML editor in Microsoft Visual Studio. You will use tables to create the layout for a simple entry form on the page.

Note

The table-editing features described in this walkthrough pertain to HTML tables, not to the Table Web server control (the <asp:Table> control and its child controls).

Tasks illustrated in this walkthrough include:

  • Adding a table.

  • Selecting the table, rows, and columns.

  • Resizing elements.

  • Adding and removing table elements.

  • Setting cell characteristics such as background color and alignment.

Prerequisites

In order to complete this walkthrough, you will need:

  • Microsoft Visual Studio 2008 or Visual Web Developer 2008 Express Edition.

Creating the Web Site

If you have already created a Web site in Visual Studio (for example, by following the steps in Walkthrough: Creating a Basic Web Page in Visual Web Developer), you can use that Web site and skip to the next section, "Creating the Page Layout with a Table." Otherwise, create a new Web site and page by following these steps.

To create a file system Web site

  1. Open Visual Web Developer.

  2. On the File menu, click New Web Site.

  3. Under Visual Studio installed templates, click ASP.NET Web Site.

  4. In the Location box, enter the name of the folder where you want to keep the pages of your Web site.

    For example, type the folder name C:\WebSites.

  5. In the Language list, click the programming language you prefer to work in.

  6. Click OK.

    Visual Web Developer creates the folder and a new page named Default.aspx.

Creating the Page Layout with a Table

You will begin by adding a table that defines the overall layout of the page.

To add a table for the page layout

  1. If you do not already have a page open in the designer, add a page to the Web site.

  2. Switch to Design view.

  3. In the Table menu, click Insert Table.

    The Insert Table dialog box appears.

    Note

    You can create tables either by using the Insert Table command, or by dragging a Table element from the Toolbox.

  4. Click OK.

    A two-column table is added to the page that takes up the entire width of the page.

    Note

    The borders around the cells in the table are displayed by default at design time to help you edit. Unless you explicitly set the table or cells to have a border, no border will appear when the table is rendered in the browser.

Creating a Form Layout with a Table

You can also use HTML tables to lay out the controls in a form. You will now create a second table inside the first one. You will later use the second table to create the layout for some controls in a form.

To add a table for the form layout

  1. From the HTML group of the Toolbox,drag a Table element into the right-hand (main) cell.

    When you drag a table from the Toolbox, Visual Web Developer creates an empty table of three columns and three rows. You need only two columns; in a moment, you will delete one of the columns.

  2. Right-click in one of the cells in the left-most column. From the Delete sub-menu, click Columns to remove the column of the selected cell.

    The column is deleted from the table.

    Note

    If you see a Delete option that does not have a submenu, it indicates that the focus is not in a cell. Right-click inside a cell in the left-hand column.

  3. Click in the bottom right-hand cell and then press the TAB key to add a fourth row to the table.

    You now have a table of two columns and four rows, which can be the container for your form controls.

Setting Characteristics of the Page Layout Table

Now that you have a form layout table inside the page table, you can finish laying out the page by editing the page layout table.

To set characteristics of the page layout table

  1. In the page layout table, click in the top cell.

    The thicker border around the cell indicates that it is selected and in content-edit mode.

  2. In the Properties window, select the Style box and then click the ellipsis (…) button.

    The Style Builder dialog box appears.

  3. In the Category box, click Block, then select Centered in the text-align drop down.

  4. On the Category box, click Font, in the Color drop down, select a color you like.

    The settings you have made set the style for the cell at the top of the layout table.

  5. Type Contoso Web Site as a heading.

  6. Repeat steps 1 through 6 for the leftmost cell in the table, with these changes:

    • Change the text to This site is maintained by Contoso, Incorporated.

    • In the Category boxof the Modify Style dialog box, in the vertical-align drop-down list, click bottom.

    In a production Web page, the side cell would probably be occupied by a menu of links or other content. However, for this walkthrough it does not matter what content the cell contains.

Building the Form

You can now build the form.

To add text and controls to the form layout table

  1. In the form layout table (the table inside the page layout table), click in the top left cell and type Name:.

  2. Click in the left cell of the second row and type Birth year:.

  3. From the Standard group of the Toolbox, drag a TextBox control into the top right cell.

  4. Set the TextBox control's ID property to textName.

  5. Drag another TextBox into the second row's right cell and set its ID property to textBirthYear.

  6. Drag a Button control into the third cell on the right and set its Text property to Submit.

  7. Drag a Label control into the bottom right cell, set its ID property to labelDisplay, and clear its Text property.

  8. Place the mouse cursor over the left-hand column until you see a selection symbol at the top of the column, and then click the symbol.

    The left column is selected.

  9. In the Properties window, click the Style cell, then click the ellipsis (…) in the cell to the right.

    The Style Builder dialog box appears.

  10. On the Category box, click Block, then select right in the text-align drop down.

    The text captions in the form are right-aligned.

  11. Click OK to close the style builder.

  12. Select the left column again, and then drag its right edge to shrink the column until it is just wide enough to fit the caption text.

Programming the Form Controls

You can now program the form controls. The form displays the age that the user will be this year.

To program the form controls

  1. Double-click the Button control in the form.

    The editor creates a Click event handler.

  2. Add the following highlighted code.

    Protected Sub Button1_Click(ByVal sender As Object, _
        ByVal e As System.EventArgs) _
        Handles Button1.Click 
        Dim age As Integer    age = DateTime.Now.Year - CInt(textBirthYear.Text)    labelDisplay.Text = Server.HtmlEncode(textName.Text) & _        ", this year you are " & Server.HtmlEncode(age.ToString())
    End Sub
    
    protected void Button1_Click(Object sender, EventArgs e)
    {
        int age = DateTime.Now.Year - Int32.Parse(textBirthYear.Text);    labelDisplay.Text = Server.HtmlEncode(textName.Text) +     ", this year you are " + Server.HtmlEncode(age.ToString());
    }
    

Testing the Page

You can now test the page.

To test the page

  1. Press CTRL+F5 to run the page.

    When the page is displayed in the browser, note the layout that you have created. Because you did not explicitly specify table borders, there are no lines on the form.

  2. In the Name box, type your name.

  3. In the Birth year box, type the year you were born, and then click the Submit button.

    The age calculation is displayed in the form in the location you created with the form table.

Next Steps

In this walkthrough, you have exercised some of the capabilities of the visual table editor in Design view. You added tables in two ways, resized a table, added a row, deleted a column, set cell styles, and added text and controls as cell contents. Suggestions for more exploration include:

See Also

Tasks

Walkthrough: Creating and Using ASP.NET Master Pages in Visual Web Developer

Concepts

HTML Editor Tag Navigation in Visual Web Developer

Formatting Elements in the HTML Editor in Visual Web Developer