views:

175

answers:

5

Hy,

I'm interested in some general optimisation methods in an asp.net ajax project for Viewstate

(ex. to reduce the size of viewstate , or just speeding up the viewstate somehow, or others this should be a general discussion :-) ).

So what kind of optimisation do you use for ViewState? What possibilities are offered on Asp.net or Ajax framework?

A: 

The best optimisation: store less stuff in it.

But you need to be specific. Are you experiencing a problem? If yes, with what controls? Your own? Others? Please expand.

Noon Silk
I'm just interested how other people handle thier own viewstate.As for myself in many cases i use control whitout viewstate when not needed.but still the size of viewstate still big.I'm thinking of increasing the encryption level of viewstate if it's possible
Sad0w1nL1ght
"Encryption Level" of ViewState? What do you mean? The ViewState isn't encrypted, it's encoded. My approach: I don't store that much in the viewstate.
Noon Silk
You are right i assumed that the encoding is in some way also a little bit compressing the viewstate.Right now i'm not sure.
Sad0w1nL1ght
+2  A: 

Disabling View State of controls you dont need explicitly, for example there are many controls on your page, which may not require viewstate, like menus, some hyperlinks, some display statistics labels etc, turn off them a bit.

Also another thing you can do is, reduce the control ID length, this will improve the page size to a much better level.

Your control IDs are like following

PageContainer1_MyLoginContainerUserControl1_MyLoginForm1_MyUsername1

PageContainer1_MyCustomerDataGrid1_item0_MyCustomerNameLabel

I know it sounds little bad, but if you notice the grid/list items put so many big control IDs like this, we noticed that by reducing certain characters in ID also helps improving page size. In list/gridview etc it improves this by 30% to 40%, also nested user controls are bad as well, when you really dont think you need control's ID anymore, just put it 3 letter big max.

Akash Kava
+1  A: 

I can be criticized for this: but in certain cases you can store viewstate in session by just override tho methods of Page.

To balance my karma I also suggest you to abandone both viewstate and session in future projects ;), for instance by trying Asp.NET MVC.

m.bagattini
i agree with it! :)
Sad0w1nL1ght
+1  A: 

@silky is right, disable it where ever you can. We try to keep it disabled on as many controls as possible.

Also, once you're using as little as possible, it might be worth looking at putting something like this in you pages (or better a base page class)

Protected Overrides ReadOnly Property PageStatePersister() As PageStatePersister
    Get
        Return New SessionPageStatePersister(Me)
    End Get
End Property

But that depends on how many users you have and how much memory you have and it assumes that you are using sessions.

Oh and in C# it's:

protected override PageStatePersister PageStatePersister
{
    get
    {
        return new SessionPageStatePersister(this);
    }
}
ilivewithian
Session states are very bad, too much memory usage on server will eventually kill the web application if there are many simultaneous users.Other better Approach is to create a new PageStatePersister class and storing values in Temp files, let the temp files accumalete and delete after some times.
Akash Kava
RAM is dirty cheap, even on a fast disk access times are slow, I'd rather spend a lump of money on more RAM than be tied to a slow disk.Of course I'd rather not be using any viewstate at all, but sometimes it gets used.
ilivewithian
+1  A: 

If you've never read TRULY Understanding ViewState, do that first.

As for AJAX helping with ViewState, I'm just speculating here, but if you have the entire page in an UpdatePanel you might be able to disable ViewState for the entire page

Greg