There are two reasons why it is desirable to do so. The first is for letting
search engines see more of your content rather than the big portion of ViewState many
sites have. The other is perceived rendering time, which means that the content loads
faster because it renders before the ViewState while the total rendering time remains
the same. That will decrease the load time of your website’s content.
Techniques to move the ViewState to the bottom of the WebForm has been published many
times before. What I wanted was adding the functionality to an HttpModule. The technique
to move the ViewState is borrowed from Scott
Hanselman while the HttpModule implementation is my own. As Scott writes, it is
a very low impact technique (0.000995 second) even though it hasn’t been fully tested
for a variety of scenarios.
The goal I’m trying to achieve is to build a reusable component that has 100% plug
‘n play capabilities. That’s where the HttpModule comes in. You can just drop it into
any existing website without changing any code.
I see no reasons why not to move the ViewState to the bottom, which makes me believe
that Microsoft should have done that by default in the first place.
Implementation
Download the ViewstateModule.cs below and put in the App_Code folder of your website.
Then add these lines to the web.config and you’re ready to go.
<httpModules>
<add type="ViewstateModule" name="ViewstateModule" />
</httpModules>
Download
ViewstateModule.zip
(1,06 KB)
The W3C has introduced an API for
their HTML Validator. It is not a SOAP web service, but it does return XML that can
be parsed, so now you have the ability to incorporate validation to any web application,
Windows program or wherever you want. It works just as the normal validator, by specifying
a URL, but you have to add the “output=soap12” parameter to the URL like so: http://validator.w3.org/check?uri=http://www.google.com&output=soap12.
I’ve been playing a little with the possibilities and created a class that encapsulates
the validation process and wraps the results up in a suitable class. The collection
of errors and warnings can be bound directly to a GridView or Repeater without additional
code. This gives you a very easy, but powerful way of dealing with the validator.
Examples of use
Even thought the class is very small and its functionality limited, it still gives
you all the relevant information from the validation. Here’s an example of some of
the possibilities of the class.
using (HtmlValidator val
= new HtmlValidator("http://www.google.com"))
{
if (!val.Validate())
{
grid.DataSource
= val.Errors; // or val.Warnings
grid.DataBind();
lbDoctype.Text
= val.Doctype;
lbCharset.Text
= val.Charset.BodyName;
foreach (HtmlValidator.ValidationError error in val.Errors)
{
DoSomething(error.Message,
error.LineNumber, error.ColumnIndex);
}
}
}
Implementation
Download HtmlValidator.cs below and add it to any C# project. If you want to use it
in an ASP.NET project, just put it in the App_Code folder.
Download
HtmlValidator.zip
(1,43 KB)