Forcing ASP.NET to generate valid XHTML code

Ever seen error like these when validating your ASP.NET web site using validator.w3.org?

there is no attribute "name".
<form name="aspnetForm" method="post" action="../shop/StartPage.aspx?id=122" i

document type does not allow element "input" here; missing one of "p", "h1",
"h2", "h3", "h4", "h5", "h6", "div", "pre", "address", "fieldset", "ins", "del" start-tag.
...dkZGQDrhr9OheQO96crtnJaz+8HiO6ew==" />

This will happen if you have the following doctype, indicating that the page content should be XHTML Strict:

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

However, using that doctype is not enough for ASP.NET to generate valid XHTML Strict code. To make it do that, make sure that the following is set in the system.web section of Web.config (the default value is “Legacy”):

<xhtmlConformance mode="Strict"/>

Without that setting, ASP.NET will insert invalid attributes on the form tag and place the hidden input fields in invalid contexts. AFAIK, there’s no dialog where you can change this setting, you have to know it’s there. And now you do! 🙂

UPDATE:
The above solution only works in ASP.NET 2.0. Under 1.0 there is no known solution (at least not to me).

12 thoughts on “Forcing ASP.NET to generate valid XHTML code”

  1. wow! i found lots of hacks/workarounds and this was all i needed! i cant believe it was so simple.

    thank you so much Emil – you just ended my 1 hour of research into trying to fix this problem.

  2. Hi,

    Its worth mentioning that this web.config setting is extremely important when coming to use the ASP.NET Ajax 1.0 extensions.

    If the setting is set to legacy, then the UpdatePanel (which is central to ASP.NET Ajax) will not work! Transitional/Strict must be chosen.

    Rich

  3. Hi friend,
    I feel that only u can help me.I’m facing the same problem .I’m always geting form name=”test”….and coz fo this it is not correct XHTML 1.0 .I saw your solution

    but when i pasted the same in my web config i got an error that

    *******************
    —————————
    Microsoft Development Environment
    —————————
    Error while trying to run project: Unable to start debugging on the web server. Server side-error occurred on sending debug HTTP request.

    Make sure the server is operating correctly. Verify there are no syntax errors in web.config by doing a Debug.Start Without Debugging. You may also want to refer to the ASP.NET and ATL Server debugging topic in the online documentation.

    —————————
    OK Help
    —————————
    ******************************

    I’m surprise y this error is coming after pasting your code …before it was runing nicly. Please help me and sent me any solution on my email id. if possiblem.

    Thank you very much

    Kunal

  4. What shall i do for asp.net 1.0 coz above solution is really good but only for asp.net 2.0 ….for asp.net1.0 if we past

    then you will get error from web config file….can any body help me to solve the same problem with asp.net 1.0.

    Regards,
    Kunal

  5. Hi Kunal,

    You’re right that this only works in ASP.Net 2.0 (I now mention this in the post). I’m not aware of any solution that works for ASP.Net 1, I’m afraid. Your best bet is probably to port your site to 2.0 (which of course is a good idea for other reasons as well) but maybe that’s inconvenient for you?

    /Emil

  6. Thanks. thats such a useful piece of knowledge you are sharing and it works !! .. I was quite lost about how to achieve XHTML 1.1 Strict in ASP.net. Its working like it should now. Cheers! 😀

  7. Thanks, spent a while trying to slove this issue. Your post has been very helpful

  8. This is a good tip, but what if you want to take it up a notch and use xhtml doctype 1.1 like this:

    In that case you help by adding a content type to your page declaration like this:

    You’ll then need to enclose your javascript in cdata, but this can help you get to the highest level of validation.

    regards-

  9. Well, my tags just got stripped out!

    I was trying to say where you declare your .NET page, set the attribute ContentType=”application/xhtml+xml”.

    As for what the xhtml 1.1 doctype is you’ll have to google it because I can’t add it here :(.

  10. Great, thanks for the tip!

    And sorry for the filtering… If you want you can insert HTML code inside (left square bracket)html(right square bracket)…(left square bracket)/html(right square bracket)

    For example:

    <html><head>...

    /Emil

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.