ASP.NET Page Persistence & Extended Attributes
by David Hovel

Example 1:

For each such member
{
  If the member is a field (data variable)
  {
     Get a collection of the extended attributes on the field.
     If one of them is a page persistence attribute
     {
       If session persistence, use the page's Session
           collection, else
       If application persistence, use the global
          Application object, else
       Use web.config file information through the
          ConfigurationSettings collection (read only).
     }
  }
}


Listing One
  public enum PagePersistEnum {
        ePersistSession,
        ePersistApplication,
        ePersistWebConfig };
  [AttributeUsage(AttributeTargets.Field)]
  public class PagePersistAttribute : System.Attribute
  {
        public PagePersistAttribute ( PagePersistEnum ePersist )
        {
              this.ePersist = ePersist;
        }
        public PagePersistEnum PersistLevel
        {
              get { return ePersist; }
        }
        protected PagePersistEnum ePersist;
  }

Listing Two
class Demo
{
  public Demo() {}
  [PagePersist(PagePersistEnum.ePersistSession)]
  protected string sTest = "this is the test string";
}


Listing Three
public static void PersistPage(HttpApplication app, bool bRestore)
{
  //  Access the page object that will handle this request.
  System.Web.UI.Page page = app.Context.Handler as System.Web.UI.Page;
  if ( page == null )
        return;     // We only handle page classes            
  //  Create a persister helper for operating on this page
  PagePersister persist = new PagePersister(page);
  
  string sMethod = app.Context.Request.HttpMethod.Trim().ToUpper();
  bool bInitialRequest = app.Session.IsNewSession || sMethod != "POST";
  if ( bRestore )
  {
      if ( ! bInitialRequest )
          persist.Restore();
  }
  else
     persist.Save();
}



Listing Four
public class PersistPage : System.Web.UI.Page
{
  private void Page_Load(object sender, System.EventArgs e)
  {
        if ( ! IsPostBack )
        {
              // Initialize all variables to desired original state
              string[] sa = {"First","Second","Third","Fourth"};
              arrayString = sa;
        }
  }
  [PagePersist(PagePersistEnum.ePersistSession)]
   protected string[] arrayString;


Listing Five
protected void Application_Start(Object sender, EventArgs e)
{
   //  Restore static variables of the Global class and PersistPage class.
  PagePersister.RestoreStatic(Application,typeof(Global));
  //  Restore static variables of all subclasses of System.Web.UI.Page class.
  PagePersister.RestorePageStatics(Application);
}

Listing Six
public static void RestorePageStatics(HttpApplicationState app)
{
  Assembly asmb = Assembly.GetExecutingAssembly();
  Type[] types = asmb.GetExportedTypes();
  Type tPage = typeof(System.Web.UI.Page);
  foreach ( Type t in types )
  {
     if ( tPage == t.BaseType )
        RestoreStatic(app, t);
  }
}


Listing Seven
protected void Session_End(Object sender, EventArgs e)
{
  PagePersister.SessionEnd(Session);
}






3


