共用方式為


XHTML in Visual Web Developer

XHTML is a World Wide Web Consortium (W3C) standard that defines HTML as an XML document. If you want to produce Web pages that render using XHTML-compatible markup, Visual Web Developer can help you create conformant pages in a variety of ways.

For more information about XHTML and the XHTML standards, see the HTML/XHTML topic page on the W3C Web site.

Creating XHTML-Conformant Pages in Visual Web Developer

By default, the Web page designer in Visual Web Developer always generates markup that is compatible with the XHTML 1.0 Transitional specification. When you create a new ASP.NET Web page, the new page contains the basic elements that are required for XHTML, including a DOCTYPE declaration, such as the one shown in the following example.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

The document will also contain an html element that includes a reference to the XHTML namespace, as shown in the following example.

<html xmlns="http://www.w3.org/1999/xhtml">

Generating Tags

When you use the Web page designer to create markup in the page, the HTML elements and attributes that are generated by the Web page designer are XHTML compatible. For example, tag and attribute names are created using lowercase letters, and all attribute values are enclosed in quotation marks. Elements are properly closed, and the Web page designer recognizes and implements the rules for required attributes and those attributes disallowed in the XHTML standard.

The Web page designer generates markup when you do the following:

  • Drag a control onto the page from the Toolbox.

  • Set character or paragraph formatting options in Design view. Formatting options are set using styles. Formatting options for ASP.NET server controls are set by using properties that render styles at run time.

  • Use the Properties windows to set a control or element property.

  • Set a property by using a dialog box, such as inserting a table in the Insert Table dialog box.

Validating XHTML Compliance

In Source view, the Web page designer validates that the markup in the page conforms to the schema for a specified browser or standard. For XHTML compliance, you can use the XHTML 1.0 Transitional, XHTML 1.0 Frameset, or XHTML 1.1 schema. For details, see Markup Validation in Visual Web Developer.

Note

Validation will mark a form element that contains the runat="server" attribute as missing a required action attribute. This is expected behavior. The action attribute is generated at run time automatically, populating the name of the current page as the attribute's value.

XHTML Output at Run Time

When an ASP.NET page is requested in the browser, ASP.NET processes the page, which causes the page and its controls to render markup, styles, and script. The page and its controls will render XHTML 1.0 Transitional markup unless the page is requested by a browser that uses a non-XHTML protocol, or if you have configured your Web site to render XHTML 1.1 or legacy markup.

During page processing, ASP.NET performs other processing tasks that are required to make the page conform to XHTML standards, including these tasks:

  • Adding an action attribute to the form element.

  • Rendering an HTTP header that includes information about the current character set, encoding, and so on.

ASP.NET will not perform the following tasks during page processing:

  • It will not add a DOCTYPE declaration if one is missing, unless the page is being rendered for a browser that supports the XHTML Mobile Profile (XHTML MP) protocol.

  • It does not make any changes to your markup. Any errors in static markup are passed as-is to the browser.

  • It will not add the namespace attribute to the html element unless the browser uses the XHTML MP protocol.

For more information, see ASP.NET and XHTML.

Working with Existing Pages

You can open and edit any Web page in Visual Web Developer. If the page does not already contain XHTML-compatible markup, validation will flag all of the elements in the page that do not conform to XHTML standards. Visual Studio does not add any missing elements, and it makes only minor corrections to elements, such as adding a closing slash (/) to elements that should be self-closed. For example, the Format Selection and Format Document menu commands can fix casing and quotation-mark problems, but they will not change the structure of the document.

If you want to make a page conformant to XHTML standards, you should do all of the following:

See Also

Concepts

ASP.NET and XHTML

Markup Validation in Visual Web Developer