Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Splitting a WebForm's First Page_Load from PostBack Page_Load

Sometimes you might want to split one of the Page (or UserControl) events up.

A simple scenario is dividing the first load event (IsPostBack==false) from the load event during a postback. The code I've posted fires two additional events after/during page load. It still allows the Load event to be used, but there is some code commented out that throws an exception if you use Load. The same thing can be done with PreRender to fire some sort of PostBackEventsComplete.

The DefaultEventAttribute will cause VS.Net to emit the FirstLoad event handler instead of the Load event.

 

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

 

 

namespace AndrewTesting.ModifyPageEventStructure

{

      [DefaultEvent("FirstLoad")]

      public class PageEx :Page

      {

 

            private static readonly object FirstLoadEventKey = new object();

            private static readonly object PostBackLoadEventKey = new object();

 

            public PageEx():base()

            {

            }

            override protected void OnInit(EventArgs e)

            {

                  InitializeComponent();

                  base.OnInit(e);

                 

            }

            private void InitializeComponent()

            {   

                  this.Load += new System.EventHandler(this.Page_Load);

            }    

            private void Page_Load(object sender, System.EventArgs e)

            {

                  if(IsPostBack)

                  {

                        OnPostBackLoad(e);

                  }

                  else

                  {

                        OnFirstLoad(e);

                  }

            }

 

            virtual public void OnFirstLoad(EventArgs e)

            {

                  FireEvent(FirstLoadEventKey,e);

            }

            virtual public void OnPostBackLoad(EventArgs e)

            {

                  FireEvent(PostBackLoadEventKey,e);

            }

 

            private void FireEvent(object key,EventArgs e)

            {

                  FireEvent(key,this,e);

            }          

            private void FireEvent(object key,object sender, EventArgs e)

            {

                  EventHandler eventHandlerDelegate = (EventHandler)Events[key];

                  if (eventHandlerDelegate != null)

                  {

                        eventHandlerDelegate(sender, e);

                  } 

            }

//          new public event EventHandler Load

//          {

//                add

//                {

//                      throw new Exception("Load event not allowed");

//                }

//                remove

//                {

//                      throw new Exception("Load event not allowed");

//                }

//          }

            public event EventHandler FirstLoad

            {

                  add

                  {

                        Events.AddHandler(FirstLoadEventKey, value);

                  }

                  remove

                  {

                        Events.RemoveHandler(FirstLoadEventKey, value);

                  }

            }

 

            public event EventHandler PostBackLoad

            {

                  add

                  {

                        Events.AddHandler(PostBackLoadEventKey, value);

                  }

                  remove

                  {

                        Events.RemoveHandler(PostBackLoadEventKey, value);

                  }

            }    

      }

}

 

 

p.s. It isn't that I use or need this code; I am studying and I just need to write code from time to time.

2 Comments

  • it's not a good idea to fire events from an eventhandler. instead you should override the OnLoad method of the base class.



    also, your commented out code doesn't work well either, consider



    Page page = new PageEx();

    page.Load += new EventHandler(Page_Load);



    can't think of an alternative solution though, other than just simply eat the event altogether by not calling base.OnLoad()

  • The override is a good suggestion.



    I'm not sure I would want the real Load event to go away.

Comments have been disabled for this content.