Showing posts with label Move view state to bottom. Show all posts
Showing posts with label Move view state to bottom. Show all posts

Monday, August 22, 2011

ASP.NET: Moving ViewState to bottom of page

If you wanted to move the ViewState to the bottom of the page in order to get Google to pay more attention to 
my page and less to the wad of Base64'ed ViewState. 
 
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("<input name="\"__VIEWSTATE\"");</pre" type="\"hidden\"" />
    if (StartPoint >= 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);
}
 
This method averaged out at 0.000995s. It consistently beat the Regex 
one, even though the Regex one was very simple, the Regexes were 
precompiled.