interviews.dotnetthread.com

↑ Grab this Headline Animator

Wednesday, April 8, 2009

Moving VIEWSTATE and other hidden fields to the bottom of the page in ASP.NET 3.5 SP1 and 2.0



As we know that state of all ASP.NET controls is maintained __VIEWSTATE hidden field in encrypted format.
By default this _VIEWSTATE HTML hidden field will be rendered at the top inside body tag (just below form tag). If we have lot of control data to be stored in ViewState then length of the viewstate will be more hence page will take time to load and also reduces rank in search engine as all the junk will be loaded at the top of actual content.
So to make web page more search engine friendly and to make page render faster we may wish to move this __VIEWSTATE to the bottom of the page.

In this manner we can make our web page more search engine friendly by moving ViewState to the bottom of the page and not to gunk up search engines as the real content will be at the top of the HTMl page.


To achieve this in ASP.NET 2.0 we need to tweek the way the page is render by overriding the page Rrender() method and move the _VIEWSTATE from its default location at the top of the page to the bottom of the page. This results in better search engine spidering.


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) //does __VIEWSTATE exist?
{

int EndPoint = html.IndexOf("/>", StartPoint) + 2;
string ViewStateInput = html.Substring(StartPoint, EndPoint - StartPoint);
html = html.Remove(StartPoint, EndPoint - StartPoint);
int FormEndStart = html.IndexOf("</form>") - 1;
if (FormEndStart >= 0)
{
html = html.Insert(FormEndStart, ViewStateInput);
}
}
writer.Write(html);
}


Instead of overriding Render() of all pages its better to do this in the Base class or in the master page.

However there is a problem when user clicks on a button before the page is loaded then ViewState will not be available to the server. It will not be able to process the ViewState.

In ASP.NET 3.5 SP1

In order to render __EVENTTARGET, __EVENTVALIDATION, __EVENTARGUMENT etc at the bottom of the page we need to set renderAllHiddenFieldsAtTopOfForm to false in web.config which true is by default.


<pages renderAllHiddenFieldsAtTopOfForm="false" />


Note: This will not move __VIEWSTATE to the bottom of the page.

Submit this story to DotNetKicks

3 comments:

Lee Dumond said...

renderAllHiddenFieldsAtTopOfForm works as ASP.NET 2.0 as well since SP1.

Anonymous said...

is there a way to alter this code to write div tags both open and close before and after the viewstate so it's xtml strict complaint?

Anil said...

Yes you can do as _VIEWSTATE is another hidden control.

Post a Comment

Post your comments/questions/feedback for this Article.

 

Latest Articles