dotsandcoms
asked on
Keep ASP.NET ViewState out of ASPX Page for Performance Improvement
Hi folks,
I have developed a website using asp.net 2.0, C#, ms sql server. Page is taking time to load because of view state. How can i keep the page view state out of it.
Please provide the way to achieve it.
many thanks
I have developed a website using asp.net 2.0, C#, ms sql server. Page is taking time to load because of view state. How can i keep the page view state out of it.
Please provide the way to achieve it.
many thanks
ASKER
hi rohanpandya
do we have the same code in C#?
do we have the same code in C#?
Here are some brief guidelines to a "Light" page:
1. Use raw text and HTML controls (HTML Tab of the control Toolbox) for things that don't require server side programming.2. For ASP.NET controls that don't need it, set the property EnableViewState = false.
3. Avoid large datasets on the page. Instead use pagination where only a single page of data is displayed at a time.
4. Use AJAX update panels to avoid expensive POST Backs to the server.
5. Avoid trying to put too much into a single page. Some pages are best split-up into multiple pages that are more focused.
6. Avoid lots of large graphics.
7. Use server side caching strategies for significantly large data sets rather than view state or cookie based caching.
(I'm sure there are more, but this was a quick list off the top of my head and have served me well.)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
good
I am trying to use this in Framework 4.0 using Visual Studio 2010. I am getting a couple of errors. Can you provide any help?
Example.jpg
Example.jpg
ASP.NET ViewState is a great mechanism that simplifies the life of ASP.NET developers. But, as everybody knows, the .NET Framework saves the ViewState data as a hidden field on your ASPX page. If your page has only a few controls, this is not a problem. But, if your page has some Panels and/or some DataGrids, with the technique demonstrated on this article, you could reduce dramatically the load time of the page.
We will start our analysis thinking where the Framework needs to work with the data saved on the ViewState hidden field. The answer is: only on the server side. The system doesn't need to work with the ViewState data on the client side, so, if we start to save this data on the server instead of carrying all this from the server to the client and from the client to the server again, we will save a lot of time while loading our documents. You probably won't note the difference while loading the page on the same computer that the application is installed, but, test it using a dial-up connection and you will see what happens. You cold also see the the size of the source code generated with and without this technique.
This example consist of a class that inherits the System.Web.UI.Page, and overrides the methods SavePageStateToPersistence
If you already have a project and want to start using this technique, all that you need to do is add a reference to the class that accompanies this example and inherit your "code-behind files" from it instead of the System.Web.UI.Page, and add the Config information to your Web.Config.
To develop this technique, I read a lot of articles over the internet, but, one really "opened my mind" of how this implementation could be done and the benefits of it. See more information on the Points of Interest Section.
I included a demo project that you could download and use to test the implementation. Note that the demo project saves the ViewState to the session, which I think is the best place to store it.
Using the code
As I said, the implementation is very easy. So, let's start with my Class code, and after discussing it, let's see the configuration of the Web.Config file.
Imports System.Configuration.Confi
Imports System.Diagnostics
Public Class VSPage
Inherits System.Web.UI.Page
Now, let's see the method responsible for saving the ViewState data, called SavePageStateToPersistence
Protected Overrides Sub _
SavePageStateToPersistence
Dim VSKey As String
'String that will hold the Unique Key
'used to reference this ViewState data
Debug.WriteLine(MyBase.Ses
'Create the key based on the SessionID, on the Request.RawUrl
'and on the Ticks representated by the exact time
'while the page is being saved
VSKey = "VIEWSTATE_" & MyBase.Session.SessionID & "_" & _
Request.RawUrl & "_" & Date.Now.Ticks.ToString
'Check if the ServerSideViewState is Activated
If UCase(AppSettings("ServerS
'Check were we will save the ViewState Data
If UCase(AppSettings("ViewSta
'Store the ViewState on Cache
Cache.Add(VSKey, viewState, Nothing, _
Date.Now.AddMinutes(Sessio
Cache.NoSlidingExpiration,
Web.Caching.CacheItemPrior
'The ViewStateData will be Saved on the SESSION
Else
Dim VsDataTable As DataTable
Dim DbRow As DataRow
'Check if the ViewState DataTable are on the Session
If IsNothing(Session("__VSDat
'No, it's not. Create it...
Dim PkColumn(1), DbColumn As DataColumn
VsDataTable = New DataTable("VState")
'Create the DataTable
'Column 1 - Name: VSKey - PrimaryKey
DbColumn = New DataColumn("VSKey", GetType(String))
VsDataTable.Columns.Add(Db
PkColumn(0) = DbColumn
VsDataTable.PrimaryKey = PkColumn
'Column 2 - Name: ViewStateData
DbColumn = New DataColumn("VSData", GetType(Object))
VsDataTable.Columns.Add(Db
'Column 3 - Name: DateTime
DbColumn = New DataColumn("DateTime", GetType(Date))
VsDataTable.Columns.Add(Db
Else
'The ViewState DataTable is already on the UserSession
VsDataTable = Session("__VSDataTable")
End If
'Check if we already have a ViewState saved with the same key.
'If yes, update it instead of creating a new row.
'(This is very dificult to happen)
DbRow = VsDataTable.Rows.Find(VSKe
If Not IsNothing(DbRow) Then
'Row found!!! Update instead of creating a new one...
DbRow("VsData") = viewState
Else
'Create a new row...
DbRow = VsDataTable.NewRow
DbRow("VSKey") = VSKey
DbRow("VsData") = viewState
DbRow("DateTime") = Date.Now
VsDataTable.Rows.Add(DbRow
End If
'Check if our DataTable is OverSized...
If Convert.ToInt16(AppSetting
< VsDataTable.Rows.Count Then
Debug.WriteLine("Deleting ViewState Created On " _
& DbRow(2) & ",ID " & DbRow(0))
VsDataTable.Rows(0).Delete
End If
'Store the DataTable on the Session.
Session("__VSDataTable") = VsDataTable
End If
'Register a HiddenField on the Page,
'that contains ONLY the UniqueKey generated.
'With this, we'll be able to find with ViewState
'is from this page, by retrieving these value.
RegisterHiddenField("__VIE
Else
'Call the normal process.
MyBase.SavePageStateToPers
End If
End SubAnd now, let's see the method responsible to load the ViewStateData, LoadPageStateFromPersisten
Protected Overrides Function LoadPageStateFromPersisten
'Verifica se o ServerSideViewState est¿ ativado
If UCase(AppSettings("ServerS
Dim VSKey As String 'ViewState UniqueKey
VSKey = Request.Form("__VIEWSTATE_
'Request the Key from the page and validade it.
If Not VSKey.StartsWith("VIEWSTAT
Throw New Exception("Invalid VIEWSTATE Key: " & VSKey)
End If
'Verify which <SPAN id=BABID_Results7>modality
If UCase(AppSettings("ViewSta
Return Cache(VSKey)
Else
Dim VsDataTable As DataTable
Dim DbRow As DataRow
VsDataTable = Session("__VSDataTable")
DbRow = VsDataTable.Rows.Find(VSKe
If IsNothing(DbRow) Then
Throw New Exception("VIEWStateKey not Found. " & _
"Consider increasing the ViewStateTableSize" & _
" parameter on Web.Config file.")
End If
Return DbRow("VsData")
End If
Else
'Return the ViewState using the Norma Method
Return MyBase.LoadPageStateFromPe
End If
End Function
End ClassAnd now, this is what we need to insert on our config file Web.Config:
<!-- application specific settings -->
<appSettings>
<!--ServerSideViewState: Defines if ViewState
will be saved on the Server: True|False-->
<add key="ServerSideViewState" value="True"/>
<!--ViewStateStore: Defines where we'll save the ViewState: Cache|Session-->
<add key="ViewStateStore" value="Session" />
<!--ViewStateCacheFSSize: Define the maximum Number
of viewStates will be saved when ViewStateStore = Session -->
<add key="ViewStateTableSize" value="150" />
</appSettings>