ASP.NET pages with large hidden viewstate at the top of the page is not good for search engine optimization purposes. One of my colleagues dug up the below code snippet to help with this problem. Is anyone attacking this problem in a more elegant manner?
/// <summary>
/// This method overrides the Render() method for the page and moves the viewstate
/// from its default location at the top of the page to the bottom of the page for SEO.
/// </summary>
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 type=\"hidden\" name=\"__VIEWSTATE\"");
if (startPoint >= 0){
int endPoint = html.IndexOf("/>", startPoint) + 2;
string viewstateInput = html.Substring(startPoint, endPoint - startPoint);
html = html.Remove(etartPoint, endPoint - startPoint);
int formEndStart = html.IndexOf("</form>");
if (formEndStart >= 0){
html = html.Insert(formEndStart, "\n" + viewstateInput);
}
}
writer.Write(html);
}