Best practices for minimising ViewState
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcontrolscs' list.


Michael
Does anyone out there have any tips for how control developers can
generically keep control state whilst keeping the use of ViewState to an
absolute minimum ?

Using ViewState to record a control's settings seems to quickly become very
costly in terms of the page load times/HTML byte sizes. The documentation
seems to suggest that if you have properties or other state data that can be
altered at run time the ViewState should be used to hold that data. This
seems to be a little inefficient as property values set at design time can
end up in the page's ViewState.

Ideally I'd like to only store information in the ViewState when it has been
set at runtime. I can't seem to find a way of easily differentiating
between property changes as a result of design time assignments and other
property changes such as from a postback.

One possible approach is to mark all the properties of the control with the
DesignOnly(true) attribute, and then supply methods (which utilise the
ViewState) for use at runtime, and then in the LoadViewState restore state
data only if it exists:-

object viewStateData = ViewState[viewStateKey];
if (viewStateData != null)
myDataMember = (ADataType)viewStateData;

It will work but having two points access for every setting of the control
seems a little costly in terms of code maintainability.

Another approach is by default to disable the use of ViewState and then
supply flags to enable ViewState individually for each property:-

public string MyProperty { ..... }
public bool MyPropertyEnableViewState { ..... }

This approach seems like a bit more of a kludge.

What do you think of either of these approaches ?
Can anyone out there think of a better approach ?

Michael Lang
MS .NET Developer
Ph: +61 0417-498-620
www.michaellang.com.au
Reply to this message...
 
    
Nikhil Kothari
WW91IGRvIG5vdCBoYXZlIHRvIGltcGxlbWVudCBhbnkgb2YgdGhpcy4NClZpZXdTdGF0ZSBpcyBh
bHJlYWR5IHNtYXJ0IGFib3V0IHJvdW5kLXRyaXBwaW5nIG9ubHkgdGhvc2UgdmFsdWVzIHRoYXQg
aGF2ZSBiZWVuIGNoYW5nZWQgZnJvbSB0aGVpciBpbml0aWFsIHN0YXRlLg0KIA0KVGhlIHN0dWZm
IHRoYXQgZ2V0cyBzZXQgZGVjbGFyYXRpdmVseSBvbiBhIHRhZyBpcyBwYXJ0IG9mIHRoZSBpbml0
aWFsIHN0YXRlLCBhbmQgaXQgZG9lc24ndCByb3VuZC10cmlwIGFzIHBhcnQgb2YgdGhlIGRhdGEg
aW4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UFSQ0ggQXJjaGl2ZXMgPCAvQkxPQ0tRVU9URSA+IA0KDQo=

Reply to this message...
 
    
Michael
Thanks Nikhil...

After some experimentation I've realised you're quite right.
I was sure I had seen design time data ending up in the viewstate at some
stage and I thought this was it's intended behaviour....

Perhaps I was mistaken or it was a problem in the beta's. In any case
you've set me a little straighter on how viewstate works within controls (I
get the feeling I'm not quite there yet as you'll see in another post
following this one).

Thanks once again... you've saved me from writting some rather silly code.

-----Original Message-----
From: Nikhil Kothari [mailto:Click here to reveal e-mail address]
Sent: Sunday, 21 April 2002 3:54 PM
To: aspngcontrolscs
Subject: [aspngcontrolscs] RE: Best practices for minimising ViewState

You do not have to implement any of this.
ViewState is already smart about round-tripping only those values that have
been changed from their initial state.

The stuff that gets set declaratively on a tag is part of the initial state,
and it doesn't round-trip as part of the data in the hidden field.

Nikhil Kothari
ASP.NET Development

    -----Original Message-----
    From: Michael [mailto:Click here to reveal e-mail address]
    Sent: Sat 4/20/2002 8:04 PM
    To: aspngcontrolscs
    Cc:
    Subject: [aspngcontrolscs] Best practices for minimising ViewState



    Does anyone out there have any tips for how control developers can
generically keep control state whilst keeping the use of ViewState to an
absolute minimum ?
    
    Using ViewState to record a control's settings seems to quickly
become very costly in terms of the page load times/HTML byte sizes. The
documentation seems to suggest that if you have properties or other state
data that can be altered at run time the ViewState should be used to hold
that data. This seems to be a little inefficient as property values set at
design time can end up in the page's ViewState.
    
    Ideally I'd like to only store information in the ViewState when it
has been set at runtime. I can't seem to find a way of easily
differentiating between property changes as a result of design time
assignments and other property changes such as from a postback.
    
    One possible approach is to mark all the properties of the control
with the DesignOnly(true) attribute, and then supply methods (which utilise
the ViewState) for use at runtime, and then in the LoadViewState restore
state data only if it exists:-
    
    object viewStateData = ViewState[viewStateKey];
    if (viewStateData != null)
     myDataMember = (ADataType)viewStateData;
    

    It will work but having two points access for every setting of the
control seems a little costly in terms of code maintainability.
    
    Another approach is by default to disable the use of ViewState and
then supply flags to enable ViewState individually for each property:-
    
    public string MyProperty { ..... }
    public bool MyPropertyEnableViewState { ..... }
    
    This approach seems like a bit more of a kludge.
    
    What do you think of either of these approaches ?
    Can anyone out there think of a better approach ?
    
    Michael Lang
    MS .NET Developer
    Ph: +61 0417-498-620
    www.michaellang.com.au <http://www.michaellang.com.au/>
    
    | [aspngcontrolscs] member Click here to reveal e-mail address = YOUR ID |
http://www.asplists.com/asplists/aspngcontrolscs.asp = JOIN/QUIT |
http://www.asplists.com/search = SEARCH Archives < /BLOCKQUOTE >

j??zf?i    rVj9?rr
Reply to this message...
 
 




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com