SEO ASP.NET - Move Keywords Up By Moving ViewState Down

9. September 2009 13:13 by Dan in SEO  //  Tags: , ,   //   Comments (0)

One aspect of on page SEO is to get your keywords to the top of the page.  Now if you're not using nice clean ASP.NET MVC, which by the way doesn't have a nasty viewstate, but the traditional aspx code behind model then as you probably know you the more controls that use viewstate the larger the hidden input field containing the actual viewstate data will be. 

The viewstate also increases page size which is another separate issue but what I wanted to do was look at moving my keywords towards the top of the page whilst keeping viewstate.

There are a few ways of getting around this problem like keeping viewstate on the server (which I might look at later) but the option I went for is to just move the hidden viewstate to the bottom of the form.

I can't take the credit for this fix as I found the solution here so thought I'd just redistribute.

I had to change the code slightly but here's my base page using the render method to change location of the viewstate:

   1: public class BasePage : Page
   2: {
   3:     protected override void Render(HtmlTextWriter writer)
   4:     {
   5:         var stringWriter = new System.IO.StringWriter(); 
   6:         var htmlWriter = new HtmlTextWriter(stringWriter); 
   7:         base.Render(htmlWriter); 
   9:         string html = stringWriter.ToString(); 
  10:         int StartPoint = html.IndexOf("<input type=\"hidden\" name=\"__VIEWSTATE\""); 
  11:         if (StartPoint >= 0) { 
  12:         int EndPoint = html.IndexOf("/>", StartPoint) + 2; 
  13:         string viewstateInput = html.Substring(StartPoint, EndPoint - StartPoint); 
  14:         html = html.Remove(StartPoint, EndPoint - StartPoint); 
  15:         int FormEndStart = html.IndexOf("</form>") - 1; 
  17:             if (FormEndStart >= 0) { 
  18:             html = html.Insert(FormEndStart, viewstateInput); 
  19:         } 
  20:         } 
  21:         writer.Write(html); 
  22:     }
  24: }

Pingbacks and trackbacks (4)+

Add comment

  Country flag

  • Comment
  • Preview