I have a user control with an asp.net server side button control in it. I use this user control on multiple pages. I am raising a custom event on the button click event of the usercontrol. All the parent pages using this usercontrol should get notified of this custom event that I raise from the usercontrol. Is there an easy way for me to get notified of this custom event in the parent pages other than subscribing to this event in all the parent pages?
I tried subscribing to this usercontrol event in an abstract base class that overrides the OnLoad() event of the parent pages and have all the parent pages inherit from this abstract base class. Usercontrol code behind is:
public partial class CustomPaging : System.Web.UI.UserControl
{
public delegate void NavigationButtonHandler(int currentPage);
public event NavigationButtonHandler NavigationButtonClicked;
public int CurrentPage { get; set; }
protected void btnPrev_ServerClick(object sender, EventArgs e)
{
if (NavigationButtonClicked != null)
{
NavigationButtonClicked(CurrentPage);
}
}
}
And the abstract base class is:
public abstract class CustomPagingBase
{
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
((CustomPaging)this.FindControl("ucPaging")).NavigationButtonClicked += new CustomPaging.NavigationButtonHandler(CustomPagingBase_NavigationButtonClicked);
}
void CustomPagingBase_NavigationButtonClicked(int currentPage)
{
LoadData(currentPage);
}
protected abstract void LoadData(int currentPage);
}
But the piece this.FindControl("ucPaging") returns null. Please note that I have a usercontrol with an id of ucPaging that I set declaratively in the parentpage’s markup
FindControl does not search recursively by default.
So unless your
ucPagingcontrol was added directly to the controls collection that implements your abstract class you would get a null.You can use this function to find it