How To Call A Non-Static Page Method From A UserControl

Scenario: You have a user control that sits in a page. The Control has an ImageButton that needs to call a method on the page containing the control.

Basically we want to wire-up the Controls ImageButton Click event on the Page.

To achieve this expose the ImageButton as a property in the UserControl:

using com.froyo.ehap.ui.Pages;

namespace com.froyo.ehap.ui.Controls
{
    public partial class DisplayStepControl : System.Web.UI.UserControl
    {

        public ImageButton JHACreateIntroButton
        {
            get
            {
               // return the ImageButton you have placed on this control 
               return ImageButton1;
            }
        }


    }
}

Then on your page that contains the method that the button needs to call:

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                    // Configure and wire up the actions in toolbar display step selection
                    this.ctrlDisplayStepControl.JHACreateJobStepButton.Click += new ImageClickEventHandler(ibCreateJobSteps_Click);
                 
            }
      }

// The page method the control needs access to  
     
        /// <summary>
        /// Handles the Click event of the ibCreateJobSteps control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.ImageClickEventArgs"/> instance containing the event data.</param>
        protected void ibCreateJobSteps_Click(object sender, EventArgs e)
        {
            if (SaveJHA())
            {
                // Send user to next step      
                Server.Transfer("/Pages/CreateJHAJobSteps.aspx");
            }

        }

Leave a comment