Potty Little Details

Just another WordPress.com weblog

Moving ViewState to the Bottom of the Page

leave a comment »

If your sites stay in the first few pages of Google’s search results, it is said that you have “Google juice.” Many developers worry that because ViewState’s hidden form field appears very early in the page and almost always before any meaningful content, web bots and spiders such as Google won’t bother looking past a giant glob of ViewState. To get around this problem you may want to move ViewState to the bottom of a rendered page. You likely wouldn’t want to take the performance hit on every page for this hack, but certainly it’s reasonable on occasion. It also gives you a great opportunity to override Render and seriously mess with the resulting HTML to include any other hacks or HTML modifications that you were previously unable to do using standard techniques.

Let’s try this technique first. Override your page’s Render method and call up to the base class’s Render and insist that the page render in its entirety. The downside here, of course, is that by hacking into the render you’re bypassing the buffered writing to the response output and dealing with strings. It’s almost as if you’re saying to the page, “Render yourself … stop! Wait. OK, continue, I’m done messing around.”

Moving ViewState to the bottom of the page—technique 1

C#

protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
base.Render(htmlWriter);
string html = stringWriter.ToString();
int StartPoint = html.IndexOf(“= 0)
{
int EndPoint = html.IndexOf(“/>”, StartPoint) + 2;
string viewstateInput = html.Substring(StartPoint, EndPoint – StartPoint);
html = html.Remove(StartPoint, EndPoint – StartPoint);
int FormEndStart = html.IndexOf(“”) – 1;
if (FormEndStart >= 0)
{
html = html.Insert(FormEndStart, viewstateInput);
}
}
writer.Write(html);
}

Moving ViewState to the bottom of the page—technique 2
static readonly Regex viewStateRegex = new Regex(@”()”,RegexOptions.Multiline|RegexOptions.Compiled);

static readonly Regex endFormRegex = new
Regex(@””,RegexOptions.Multiline|RegexOptions.Compiled);

protected override void Render(HtmlTextWriter writer)
{
//Defensive coding checks removed for speed and simplicity.
// If these don’t work out,
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
base.Render(htmlWriter);

string html = stringWriter.ToString();
Match viewStateMatch = viewStateRegex.Match(html);
string viewStateString = viewStateMatch.Captures[0].Value;
html = html.Remove(viewStateMatch.Index,viewStateMatch.Length);

Match endFormMatch = endFormRegex.Match(html,viewStateMatch.Index);
html = html.Insert(endFormMatch.Index,viewStateString);
writer.Write(html);
}

Advertisements

Written by oneil

September 9, 2008 at 4:31 pm

Posted in ASP DOT NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: