PreRender event not firing on ImageButton

Well there is a bug in ImageButton OnPreRender that simply don’t call base.OnPreRender so your event handler will never run. If you want to workaround this problem you can create your own ImageButton. Your new web custom control should overload OnPreRender and PreRender event.

 

The following code demonstrates ImageButton control that will raise PreRender event:

 

public class MyImageButton : ImageButton 

      {

            public new event EventHandler PreRender;

            public MyImageButton() : base()

            {

                 

            }

            protected override void OnPreRender(EventArgs e)

            {

                  if (PreRender != null)

                      PreRender(this,e);

                  base.OnPreRender(e);

            }

 

           

      }

}

 

1 Comment

  • Don't forget to check if the event is null. The event will have a null reference if nothing has been delegated to the event. So change your OnPreRender method to:



    protected override void OnPreRender(EventArgs e) {

    if (PreRender != null)

    PreRender(this, e);



    base.OnPreRender(e);

    }

Comments have been disabled for this content.