Found on Juye's Blog, a custom attribute that is used to automatically persist fields from the codebehind class to the ViewState or the Session.
It is unclear to me that it makes the code really much shorter or clearer, or that it is efficient in any way, but it still is a cool trick. ;-)
In the .aspx:
In the .cs:
In place of (the boring):
A custom control, the Persister, needs to be added in the page (see aspx sample above). It gets called via the Control.LoadViewState and Control.OnInit hooks, which gives it a chance to inspect all fields from the page (using the Page.GetType().GetFields() reflection call) to find those marked with the custom Persist attribute.
It supports persisting to the ViewState with [Persist(PersistMode.ViewState)] and to the Session with [Persist(PersistMode.Session)].
Here is the zip containing the code, with a sample.Posted by Julien on May 09, 2003. Permalink