2009-06-29

Ensure XHTML Strict Compliance by Removing Name Attribute from Form tag in ASP.NET

Many of us web developers are trying to be "good citizens" and conform to the higher standards of XHTML in our apps.

By default, ASP.NET will add the name="FormName" attribute to the rendered XHTML.

To force ASP.NET to omit the name attribute, simply add the following tag to the system.web section of your web.config file for your app:

<xhtmlConformance mode="Strict" />


Be warned, however; unfortunately it doesn't seem to make ASP.NET render HyperLinks properly. I have an asp:HyperLink that happens to have an ampersand (&) in the Text and ToolTip properties. The ToolTip properly renders a title="" attribute with the ampersand encoded as &amp; ... but the text between the open and close <a> tags doesn't have the ampersand properly encoded.

The only advice I can give you there is to:
1. Use the word "and" instead of an ampersand
2. Make static "a" tags instead of using the ASP.NET HyperLink object, if you can. If someone knows a smoother/quicker/more elegant solution, please leave a comment with it!