One of the most common
problems with web pages is largeness. Sometimes, a lot of data from
database, scripts and other can make pages too slow, here I will try
to suggest some of the more efficient ways for avoid thi problem. In
particular we had a very big page that is composed by several web
controls in a wizard, some ajax controls and it is full of data bound
controls. In the first release we received a lot of errors because
some users of the public module of our system had a very slow
connection. The most common error was the terribile:Invalid
Viewstate.( :-((( ). This error is one the most hated by ASP.NET
developers.
So I decided to resolve
the problem and reduce drastically the amount of code in that page.
Remove viewstate from
the controls that don't need.
Put ViewState in
Session.
You can put viewstate in
session if your web page inherits from this base class. This class
inherits
from
System.Web.UI.Page and overrides some
routines used during page lifecicle, in particular
LoadPageStateFromPersistenceMedium()
and SavePageStateToPersistenceMedium() for load and save viewstate.
In normale pages viewstate is put into code as coded string. Here
viewstate is loaded in session into server.
Imports
Microsoft.VisualBasic
Namespace
EasyCV
Public
Class
ViewStateSessionPage : Inherits
System.Web.UI.Page
'''
<summary>
'''
'''
</summary>
'''
<remarks></remarks>
Protected
Overrides
Sub
InitializeCulture()
MyBase.InitializeCulture()
Dim
cultureCode As
String
=SessionUtils.GetCurrentLangCode()
Dim
cinfo As
Globalization.CultureInfo =
Globalization.CultureInfo.CreateSpecificCulture(cultureCode)
System.Threading.Thread.CurrentThread.CurrentUICulture=cinfo
System.Threading.Thread.CurrentThread.CurrentCulture
= cinfo
End
Sub
'Load viewstate
Protected
Overrides
Function
LoadPageStateFromPersistenceMedium()
As
Object
Return
Session("_ViewState")
End
Function
'Save viewstate in Session
Protected
Overrides
Sub
SavePageStateToPersistenceMedium(ByVal
viewState As
Object)
Session("_ViewState")
= viewState
End
Sub
End
Class
End
Namespace
This
is a good solution if your server have a lot of ram or your database
server is powerful, because a good option is saving sessions into
database server but this solution require a lot of conversations
between application and database server.
Use Cache everywhere
is possible.
Using caching in ASP.NET
is a great way for avoid too slow pages, in particular when you have
a lot of databound controls.
Cache in ASP.NET is a
statis/shared object, really simple to use. Here you can find a
little example about Cache:
Dim
lang As
String
= SessionUtils.GetCurrentLangCode()
Dim
dt As
System.Collections.Generic.IList(Of
EasyCV.Entities.NameValuePair) = CType(Cache(lang+EasyCV.WebLogic.Caching.CachingConstants.DatasetDrivingLicenses),
System.Collections.Generic.List(Of
EasyCV.Entities.NameValuePair))
If
dt Is
Nothing
Then
'inizializzazione
Dim
dal As
ICvTypological =
EasyCV.WebLogic.DataAccessLayer.DALManager.GetDal(Of
ICvTypological)()
dt=dal.GetByTypeId(SessionUtils.GetCurrentClientID,
TypologicalConstants.CvDrivingLicense, lang)
''This
code add dataset to cache for 2 hours with normal priority
Cache.Add(lang
+ EasyCV.WebLogic.Caching.CachingConstants.DatasetDrivingLicenses,
dt, Nothing,
DateTime.Now.AddMinutes(120), Cache.NoSlidingExpiration,
CacheItemPriority.Normal, Nothing)
End
If
Me.chkPatente.DataSource
= dt
Here
I put a dataset into session and it will be there for 2 hours, so I
will not ask everytime to db for this data. In
my situation I must set dataset for every language, so I must control
the entry of Cache.
These are some of the ways
for speed up your web forms, but there are other ways (ex. Add
compression module to your application).
There isn't one solution
for every situation but there are solutions and you must choose the
right solution for your application. Everything depends by some
factors as architecture, avalaibility,scalability,etc.