Telerik blogs

As you may know, all components from the r.a.d.controls suite render XHTML 1.1 compliant output. We wanted to promote it in a way that all users can click a button in our online examples and see for themselves that everything validates perfectly. W3C provides an online validation service (http://validator.w3.org) so it should not be that hard. Unfortunately ASP.NET 1.x complies with *NO* HTML standard ever approved by W3C. ASP.NET 2.0 was first XHTML 1.1 compliant, but then in the official release Microsoft fell back to XHTML 1.0 transitional which is less restrictive.
I made a couple of Google searches and found other people who had the same problem. There were different approaches and we picked the one with custom page and regular expression replaces. As a result all live examples inherit from a page derived from System.Web.UI.Page. In the Render method the output is cleaned by a set of regular expressions to comply with the XHTML 1.1 specification. Here are all the problems solved by XhtmlPage:

  1. ViewState rendering
    According to the XHTML 1.1 spec input elements should be enclosed in a tag with block display. In ASP.NET 1.x the ViewState is rendered as hidden inputs directly after the form tag. A regular expression captured all those hidden fields and enclosed them in a <div>.
  2. Name attribute of the form tag
    Forms should not have name attribute. In ASP.NET they are rendered with a name attribute and later referenced by it in the __doPostBack JavaScript routine. This required two replaces – first the name attribute must be removed, then __doPostBack should be altered to get the form DHTML object via document.getElementById.
  3. Script tags
    The type attribute is mandatory according to the spec. ASP.NET 1.x renders language="Javascript" instead. This one was relatively easy – global replace of language="Javascript" with type="text/javascript".
  4. For some bizarre reason ASP.NET renders language="Javascript" in the root tag of all controls that postback to the server. Every single control – checkboxes, dropdowns, is cleaned from that unnecessary attribute.

At the end of this endeavor I really became a regular expression ninja and nothing seemed impossible. However I should give credit to www.regexlib.com and their tester page: http://www.regexlib.com/RETester.aspx - it was really helpful and saved me lots of time to find the right regular expression hack.

However XhtmlPage does not magically cure all bad ASP.NET practices. Developers have to make sure the rest of the html is compliant – lowercase tag names, no unclosed tags. Here I will mention the rather annoying feature of Visual Studio 2002/2003 to capitalize some of the tags when switching between design and source view. It slowed us down a bit. Another thing to mention is that client-side validation controls cannot be made valid using that technique. They deeply rely on custom attributes which fail to validate. However server side validation can be used instead.

If you want to make XHTML compliant pages but cannot switch immediately to ASP.NET 2.0, XhtmlPage is an option. The class is available from the Telerik.QuickStart.dll assembly which can be found in the bin folder of any r.a.d.control. We plan to release the source code soon.


rahnev
About the Author

Stefan Rahnev

Stefan Rahnev (@StDiR) is Product Manager for Telerik Kendo UI living in Sofia, Bulgaria. He has been working for the company since 2005, when he started out as a regular support officer. His next steps at Telerik took him through the positions of Technical Support Director, co-team leader in one of the ASP.NET AJAX teams and unit manager for UI for ASP.NET AJAX and Kendo UI. Stefan’s main interests are web development, agile processes planning and management, client services and psychology.

Comments

Comments are disabled in preview mode.