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);
    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);
This method averaged out at 0.000995s. It consistently beat the Regex 
one, even though the Regex one was very simple, the Regexes were 

1 comment:

  1. Below code is used for removing the query string parameters from the collection of parameters-

    Dim isreadonly As PropertyInfo = GetType(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance Or BindingFlags.NonPublic)

    ' make collection editable
    isreadonly.SetValue(HttpContext.Current.Request.QueryString, False, Nothing)

    ' remove

    HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath + "?" & Convert.ToString(HttpContext.Current.Request.QueryString))