facebook  linkedin  Twitter  skype  Rss googlePlus

DNN and ViewState

Sep 17 2004
2759

I'm not a big fan of viewstate, great idea from getting away from sessions, but it really is a pain. MS is doing some enhancements to viewstate in the next version of ASP.NET, but being a DotNetNuke user I wanted to clean my DotNetNuke implementation from the viewstate. I know there are several methods of dealing with viewstate from loading into a file, database or session. For me, memory overhead of storing in a session wasn't as big of an issue so I opted for that.

Moving viewstate into a session is pretty easy to do in DotNetNuke. Since all pages inherit from the DotNetNuke.Basepage class, all you need to do is modify this one class in order to remove the viewstate from your HTML and put it into the session or server's memory. Below is the modification I did in order to do that, another thing you'll notice here is I took out the method that intercepts the request and places the viewstate into the bottom of the HTML form instead of the top. This method was added for spiders, since relevant content is indexed at the top of the HTML page, and considered less relevant when content is towards the end. Since some of the viewstate crypts were hovering around 50k, it was hurting DotNetNuke sites in their rankings. I really don't like the viewstate move method since it processes post-request, basically pulling out the viewstate from the HTML string and inserting it into the bottom of the page. There is plenty of overhead to do this on every request, and since I'm removing the viewstate crypt from the page it isn't necessary to have the method in there. This way I move the overhead onto the hardware which we can always upgrade if needed, and lighten the load on my bandwidth, and make the pages load much faster.

This is a core mod, so if you're going to use it you need to readd it if you upgrade your DotNetNuke install. The basepage.vb file is located in the components directory of the DotNetNuke application.

Imports System
Imports System.Diagnostics
Namespace DotNetNuke
Public Class BasePage
Inherits System.Web.UI.Page
Private _formatter As New LosFormatter
Private Sub Page_Error(ByVal Source As Object, ByVal e As System.EventArgs) Handles MyBase.Error
   Dim exc As Exception = Server.GetLastError
   Dim strURL As String = ApplicationURL()
   If Not Request.QueryString("error") Is Nothing Then
      strURL += "&error=terminate"
   Else
      strURL += "&error=" & Server.UrlEncode(exc.Message)
      If Not IsAdminControl() Then
         strURL += "&content=0"
      End If
   End If
   ProcessPageLoadException(exc, strURL)
End Sub
'save viewstate to a session. in the next two methods we take the viewstate and load/unload into a session variable.
Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object)
   Dim Key As String = Request.Url.ToString & "__VIEWSTATE"
   Dim memStream As New System.IO.MemoryStream
   _formatter.Serialize(memStream, viewState)
   memStream.Flush()
   Session(Key) = memStream
End Sub
Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object
   Dim Key As String = Request.Url.ToString & "__VIEWSTATE"
   If Not Session(Key) Is Nothing Then
      Dim memStream As System.IO.MemoryStream = CType(Session(Key), System.IO.MemoryStream)
      memStream.Seek(0, IO.SeekOrigin.Begin)
      Return _formatter.Deserialize(memStream)
   End If
End Function
End Class 'BasePage
End Namespace

About the Author, Patrick Santry

Patrick Santry, has two decades of experience in enabling businesses to take advantage of the digital landscape. A well rounded experience in technology, and business is what sets me apart from the rest of the pack. When it comes to an overall digital strategy my experience is impressive.

BS in Computer Information Systems. Four time recipient of the Microsoft MVP Award, and author of several books and magazine articles on digital technologies.


blog comments powered by Disqus

Latest Podcast

B2B e-Commerce, Who's Your End User?

 

Subscribe to Pat's Digital Podcast on iTunes! Latest Podcast

 

Thank you for visiting our site, before you leave, please visit some of the areas or information you may have missed.