Forum Moderators: open

Message Too Old, No Replies

View state and ajax

Moving viewstate and ajax down on page

         

eljacko

12:32 pm on Mar 14, 2008 (gmt 0)

10+ Year Member



I have been told that if I am to improve the speed of my webpage and optimise for better crawl I must move the view state down but when I do this I am having problems with ajax.

Is there a solution to this?

httpwebwitch

12:46 pm on Mar 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My "glib" reply is that ViewState is garbage... Wherever possible do not use ViewState! I once worked in an all-.NET shop where the temptation to use ViewState was always there, but we managed to build fully-functional apps without it. There's always a way (PHP coders know this), though the drag-n-drop sexiness of Visual Studio's built-in controls makes it hard to resist.

Anyways, yes a huge block of Viewstate adds bulk to the page, makes downloading slower, and may (I believe *does*) have SEO implications. I have no specific advice regarding making things work when moving the viewstate down the page, but in general if you can accomplish your app without using Viewstate, or reducing the amount of viewstate, you're better off.

- Set "enableViewState = false" on any controls that don't need it.
- If your page doesn't PostBack, you can turn ViewState off on the page level
- Disable ViewState for those controls that are going to get populated every time a page loads

MorningZ

2:48 pm on Mar 14, 2008 (gmt 0)

10+ Year Member



Maybe this will help

1) Create your own "Page" class, like:

Namespace MyStuff
Public Class PageTemplate
Inherits System.Web.UI.Page

End Class
End Namespace

inside that class, you can over ride the render method (which is at the point that the .NET dll is generating the HTML to send to the client)

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()

End Sub

At this point, the variable "html" holds all the html that will be sent to the client browser, so a little manipulation:

Dim StartPoint As Integer = html.IndexOf("<input type=""hidden"" name=""__VIEWSTATE""")
If StartPoint >= 0 Then 'does __viewstate exist?
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("</form>")
If FormEndStart >= 0 Then
html = html.Insert(FormEndStart, viewstateInput + vbNewLine)
End If
End If
writer.Write(html)

That will put the ViewState field just before the closing form tag.... full code:

Namespace MyStuff
Public Class PageTemplate
Inherits System.Web.UI.Page

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("<input type=""hidden"" name=""__VIEWSTATE""")
If StartPoint >= 0 Then 'does __viewstate exist?
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("</form>")
If FormEndStart >= 0 Then
html = html.Insert(FormEndStart, viewstateInput + vbNewLine)
End If
End If
writer.Write(html)

End Sub
End Class
End Namespace

On pages you want to use this, change it's code behind to inherit from "MyStuff.PageTemplate" instead of "System.Web.UI.Page" and the ViewState will be moved :-)

eljacko

11:42 am on Mar 17, 2008 (gmt 0)

10+ Year Member



Thanks I shall give that a try and let you know.