SYSK 112: How to Invoke a Method on the Parent Web Form From a Control

The other day I was asked the question above, and since others might benefit from seeing the answer, and I’m sharing it with SYSK readers below.

 

Question:

I've a need to call a method on a WebForm from within the UserControl that lives on that form.

 

Here's an example...

public class MyForm:System.Web.UI.Page

{

    public void DoWork()

    {

    }

}

 

public class MyControl:System.Web.UI.UserControl

{

    private void makeParentDoWork()

    {

        if (this.Page is MyForm)

        {

            MyForm form = (MyForm)this.Page;

            form.DoWork();

        }

    }

}

 

This was perfectly doable in .Net 1.1. In 2.0 however, I'm not able cast control's Page property to MyForm type in order to call the method. I can add "@References" directive to the control to be able to perform the cast, but this ends up creating a circular reference as MyForm.aspx contains a "@Register" directive for the user control.

 

Answer:

There are a couple of ways to accomplish the desired effect.

 

1. Declare and raise an event in the control; then subscribe to that event in the form and invoke the DoWork method from the form itself.  In my opinion, this is the preferred way as it’s less coupled.

2.  If you absolutely must invoke DoWork from the control itself, do the following:

 

a).  Add a new class to the website (make sure you put it under App_Code so it's available to the rest of the site):

    public interface IDoWork

    {

         void DoWork();

    }

 

b).  Implement the interface on the form: 

     public class MyForm:System.Web.UI.Page, IDoWork

 

c). Replace your code in makeParentDoWork()with:

IDoWork myWorkPage = this.Page as IDoWork;

if (myWorkPage != null)

    myWorkPage.DoWork();

    

Special thanks to Brian Hitney who recommended to bubble the notification via event firing and to David Corbin for suggesting the usage of ‘as’ instead of my originally stated ‘traditional’ type cast.