共用方式為


Markup Validation in Visual Web Developer

When you edit markup in Source view of the Visual Studio Web designer, the editor continually checks that the markup you are creating is valid. Markup validation works like the spelling checker in a word processing program. The editor examines the markup and adds wavy red lines under the elements or attributes that are not valid.

The editor determines what is valid based on the currently selected validation schema. For example, if you have set the current browser to Internet Explorer 6, the editor compares the markup in your page against a schema that defines what Internet Explorer 6 considers valid HTML. Internet Explorer 6 does not require tag attributes to have quotation marks around them. Therefore, the editor does not mark the text attribute in the following code example.

<asp:button runat="server" id="button1" text=Button1 />

In contrast, if you change the current browser schema to XHTML 1.0 Transitional, the editor marks the value of the text attribute, because XHTML requires that all attributes be within quotation marks.

A validation error does not prevent the page from running. It simply provides you with feedback that the markup in the page does not conform to the current browser schema.

Selecting a Browser Schema

Visual Studio includes schemas for commonly used browsers (such as Netscape Navigator and Internet Explorer) and for common standards (such as XHTML Transitional 1.0). You can select a schema from the drop-down list in the HTML Source Editing toolbar. Alternatively, you can select a validation schema in the Options dialog box. For details, see How to: Select Validation Schemas for HTML Editing in Visual Web Developer.

Viewing Validation Errors

The editor shows you validation errors in the following ways:

  • Underlined elements   Elements or attributes that are invalid are underlined with wavy red lines.

  • ToolTips   If you hold the mouse pointer over an underlined element, a ToolTip displays details about the error.

  • Error List   If you have the Error List window open, you can see a list of all the validation errors. Double-clicking an error in the Error List window takes you directly to the error in the editor.

What Validation Checks

Markup validation checks for the following:

  • Allowed tags   Some browser schemas support tags that are not allowed in others. For example, the <blink> tag is supported only in Netscape browsers, so this tag is marked as invalid in other schemas.

  • Allowed attributes   Validation checks that the attributes in an element are allowed for that element.

  • Required attributes   Validation checks that any required attributes are included. For example, in XHTML, the <script> tag must have a type attribute.

  • Allowed attribute values   If an attribute supports only certain enumerated values, such as true or false, validation marks any values that do not conform to the allowed values.

  • Allowed CSS values   Validation checks that the cascading style sheet (CSS) values for the style attribute are allowed.

  • Allowed child elements   Validation checks that child elements are allowed for a given parent element.

  • Proper use of quotation marks around attribute values   Depending on the schema, quotation marks around attribute values might be required. For schemas where quotation marks are not required, validation checks that if a value is enclosed in quotation marks, they are balanced. For schemas where quotation marks are optional, validation checks them according to the validation options you set. See "Customizing Validation" later in this topic.

  • Casing rules   Depending on the schema, element and attributes names might need to be in lowercase letters. If the schema allows uppercase or lowercase letters in names, validation checks them according to the validation options you set. See "Customizing Validation" later in this topic.

  • Unique element IDs   Validation ensures that element IDs are unique throughout the page.

  • Well-formed elements   Validation confirms that all opening tags have corresponding closing tags, if the schema requires it.

  • Element opening and closing order   Validation checks that you do not have crossed opening and closing elements, such as <b><i>text</b></i>.

  • Outdated tags or attributes   A schema such as XHTML 1.0 Transitional will mark tags such as <font> with an error that tells you that the tag is no longer recommended.

  • Valid targets for relative hyperlinks and graphics   Validation confirms the validity of any targets that are relative to the current site, but it does not check the validity of fully qualified URLs.

Validation and Generation

Validation is a distinct operation from generation. When you work in the designer, Visual Studio adds, or generates, markup to your page. The markup created by the designer is not determined by the current browser schema. Instead, the Web page designer in Visual Studio always generates XHTML 1.1–compatible markup. For details, see XHTML in Visual Web Developer.

Validating Page Output

Validation checks the markup of the page in the designer. It cannot guarantee that when the page runs in the browser, the output of the page will validate against a specific schema. Individual ASP.NET server controls and the page itself render markup, and sometimes script, into the page when the page is processed. That output is not accessible to validation in the designer.

The markup and scripts that are created at run time will be valid for most browsers. ASP.NET pages and server controls render markup that is XHTML 1.1–compatible. Most browsers now target standards rather than supporting browser-specific tags, specifically XHTML. Because XHTML output is compatible with most browsers, the markup rendered at run time by the page and controls will be valid.

For more information, see ASP.NET and XHTML.

Validation and IntelliSense

Specifying a target browser controls not just validation, but the list of tags and attributes that are available in IntelliSense. For example, the Internet Explorer 3.02 schema does not offer style attributes because Internet Explorer 3.02 does not support them. Similarly, the XHTML 1.0 Transitional schema does not offer the <font> tag because <font> tags are deprecated in XHTML.

Customizing Validation

Depending on the browser schema that you are using, you might be able to specify additional validation options. For example, if you are using the Internet Explorer 6 schema, you can select whether validation marks element and attribute names that are not in lowercase letters. For details, see How to: Set Validation Options for HTML Editing in Visual Web Developer.

Disabling Validation

If you do not want to use validation, you can turn it off. For details, see How to: Set Validation Options for HTML Editing in Visual Web Developer.

Validating Multiple Namespaces

In a Web page, it is possible to include blocks of markup that are declared with their own namespace. For example, an ASP.NET Web page might include a block of embedded XML that includes a declaration for the namespace in which the XML elements are defined. In that case, validation is based on a union of the current page schema and the namespace for the embedded block.

See Also

Tasks

How to: Select Validation Schemas for HTML Editing in Visual Web Developer

Concepts

ASP.NET and XHTML