11 Dec 2007

ViewState Mayhem

I made a portal app that had little draggable, dynamically loaded ASP.NET webcontrols that you could move around a dashboard. All was fine and dandy until I needed to handle control events in these draggable controls. See, when I moved a control, the viewstate got all munged up and controls started restoring each other's data. WHY WAS THIS SO?

Turns out that by default, ViewState is maintained NOT by an index of ClientIDs but by offset indexes to the parent control. So if you dynamically change the order of controls loaded in a container between first view and postback, ViewState will go boom.

The elusive, and not very satisfactory solution, is the mysterious ViewStateModeById custom attribute. When adorning a class, it ensures the object will have its ViewState keyed by ID instead of by index. Huzzah. Only prob is you need to wrap everything you want to maintain this way in a thin wrapper in order to implement the custom attribute. Oh joy.

Anyway for this and other sexy CustomAttributes you may have missed, check dis:

No comments:

Post a Comment

Comments are very welcome unless you're a spammer, in which case you should probably kill yourself.

If I helped you out today, you can buy me a beer below. Cheers!