User Control calling another function in another user control

by rBellantoni 8/24/2009 11:31:00 AM

Recently I faced an issue where I had two user controls. Inside one of the controls I needed to call a load function from the other control that are both in the same aspx page. So to do this I wired up a new handler in the page load event of the main aspx page. The page that contains both user controls like so:

 this.UserControl.OnAddNewButtonClicked += new Controls_UserControl.AddNewButtonClicked(AddNewButtonClicked);

Then I created the function for this to attach to:

 public void AddNewButtonClicked(object sender, EventArgs e)

 

}

Inside the control that we are doing the calling from we create the delegate and the event:

public partial class Controls_UserControl : System.Web.UI.UserControl

{

     public delegate void AddNewButtonClicked(object sender, EventArgs e);

     public event AddNewButtonClicked OnAddNewButtonClicked;

}

Now inside of the event that we would like to fire the function from the other usercontrol we reference the delegate:

public void btnAddNew_Click(object sender, EventArgs e)

{

     try

     {

          // Save the form

         SaveForm();

         pnlMessage.Visible =
true;         if (OnAddNewButtonClicked != null)

         { 

              OnAddNewButtonClicked(sender, e);

         }

     }

     catch (Exception ex)

     {

          HandleError(ex);

     }

}

Now inside of the function we created earlier in the main aspx page, we can call whatever usercontrol.function we would like to fire:

public void AddNewButtonClicked(object sender, EventArgs e)

{

     LoadControls();

}

Tags:

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  October 2018  >>
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Recent comments

Tags

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2018

Sign in