Articles in The 'moving-Viewstate' Tag


August 25 2008

ASP.NET SEO Quick Tip – Moving SEO Unfriendly Code To the Bottom of the Page

by Lee Zoumas

A common SEO technique is to make sure the content of your web page is placed as close to the top of your HTML as possible. This will help ensure that your relevant content achieves higher priority by search engine spiders. ASP.NET provides a great framework for developing feature rich web applications, especially with the addition of view state. View state gives web forms the ability to persist changes across postbacks. Other web scripting languages are not able to accomplish this easily, however, this benefit may have some negative SEO implications.

The view state of a page is placed by default in a hidden form field named ___VIEWSTATE at the top of the html source code. The contents of the __VIEWSTATE form field contain serialized information, which can get very large (tens of kilobytes), about various controls on the web page. When a web page does not have a lot of controls using view state, the hidden form field will look something like this…

20080815-top

… which is probably fine at the top of the page. But, often times a web page may have numerous controls, no matter how much it is optimized to minimize view state, which produce a view state value that looks something like this…

20080815-bottom

… actually it could go on and on. This particular view state sample (this is just a small portion) was 10 pages long! Needless to say, you don’t want that to appear before your precious web page content.

There is an easy way to move the __VIEWSTATE form field to the bottom of the html source code. By pasting the following VB.NET code, “as is”, into your web form, the view state will be moved to the bottom of the html source code right above the closing </form> tag…

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter
Dim htmlWriter As HtmlTextWriter = New HtmlTextWriter(stringWriter)
MyBase.Render(htmlWriter)
Dim html As String = stringWriter.ToString()
Dim StartPoint As Integer = html.IndexOf("
If StartPoint >= 0 Then
Dim EndPoint As Integer = html.IndexOf("/>", StartPoint) + 2
Dim viewstateInput As String = html.Substring(StartPoint, EndPoint - StartPoint)
html = html.Remove(StartPoint, EndPoint - StartPoint)
Dim FormEndStart As Integer = html.IndexOf("") - 1
If FormEndStart >= 0 Then
html = html.Insert(FormEndStart, viewstateInput)
End If
End If
writer.Write(html)
End Sub

Now when you browse your web page, the __Viewstate hidden form field and its ridiculously long value, will be at the bottom of the page, and your precious content will be closer to the top, just how the search engines like it.

© 2023 MoreVisibility. All rights reserved.