Thoughts, Tips and Tricks on what I'm currently do for a living. Currently most of my spare time is spent on contributing to Akka.NET.

Tuesday, January 30, 2007

Asp.Net ViewState

A good article explaining ViewState in Asp.Net. Some of the interesting titbits:
  • When a control is tracking ViewState, items changed in the ViewState will be marked dirty. Only dirty items in the ViewState StateBag will be serialized to the hidden __VIEWSTATE field.
  • Controls starts to track ViewState after the it's OnInit.
  • The page's OnInit is called after all controls' so at this stage all controls are tracking viewstate.
  • Data bound during the page's OnInit and OnLoad will go into ViewState since the control is tracking view state. This means, don't set properties on controls in a page's OnInit and OnLoad if you don't want it to go into viewstate.
  • Properties set on a control during the page's OnPreInit, before controls are tracking view state, will not go into ViewState. This can be used to fill controls with data that's easy and cheap to get on every postback.
  • When dynamically creating controls (for example in CreateChildControls in a custom control) set all properties before adding the control to the control collection, or else you are at risk of that the control is already tracking ViewState.

No comments:

Post a Comment