Knowledge Base

Why Is ViewState reported as invalid?

ASP.NET records the state of each HTML control on a page through a technology called ViewState which is enabled by default. Viewing the source of an ASP.NET page will reveal an entry similar to the following:

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="..." />

The HTML 4.01 specification section 6.2 SGML basic types requires 'id' and 'name' tokens to begin with a letter. As the generic attribute 'ID' is declared as SGML type 'ID' it cannot begin with an underscore.

As of November 2010 Sitemorse reports the following error for HTML pages and no error for XHTML pages:

file/html/badvalue - "__VIEWSTATE" is not a valid value for attribute "id" to tag "input".

Previously to this date Sitemorse enforced the rule for both HTML and XHTML. Although the collection of legal values for token names in XHTML is much larger than that permitted in HTML we enforced Appendix C, our reasoning was as follows:

XHTML is a blend of XML and HTML - to support current browsers, including IE7 XHTML is served from the web server with a text/html mime type. The XHTML 1.0 Specification requires XHTML content served with a text/html mime type to conform to certain "Compatibility Guidelines outlined in Section C.

Furthermore Section C.3 Fragment Identifiers states:

When defining fragment identifiers to be backward-compatible, only strings matching the pattern [A-Za-z][A-Za-z0-9:_.-]* should be used.

Thus our conclusion was that in order to comply with the compatibility specifications the 'id' attribute could not begin with an underscore.

However, given the widespread use of the '.NET' platform and direct discussions with the W3C we have decided that 'id' attribute values starting with one or more underscores has no negative impact on modern browsers and is therefore acceptable for pages declared as XHTML.

Fixing ViewState in ASP.NET

One of our clients submitted a workaround to force valid ID values for HTML and therefore fully compliant for XHTML pages. The code can be applied either to a Master Page or in individual pages.

Source code for the VB ViewState fix

The Sitemorse Knowledge Base is the repository of knowledge about the Sitemorse system.

Every time a new feature is added, a question asked or anything our technical team believe might be appropriate is recorded here.

Categories