Option 1
You need to pass you a control that implements the NotifyParentEvent event to attach an event handler. Basically, in your ReloadControl method ReloadControl replace this line of code:
control.NotifyParentEvent += new EventHandler(UserControlNotificationHandler);
with the following:
if(control is UserControls_WebUserControl1) { (control as UserControls_WebUserControl1).NotifyParentEvent += new EventHandler(UserControlNotificationHandler); }
Option 2
A more general approach was to create an interface and check if this dynamic control implements this interface.
Create an interface:
interface INotifyParent { event CommandEventHandler NotifyParentEvent; }
Implement Interface:
public partial class UserControls_WebUserControl1 : System.Web.UI.UserControl, INotifyParent { public event CommandEventHandler NotifyParentEvent; private void NotifyParent(string message) { if (NotifyParentEvent != null) { CommandEventArgs e = new CommandEventArgs("Control1 Action", message); NotifyParentEvent(this, e); } } }
Check if the dynamic control implements the interface:
if(control is INotifyParent) { (control as INotifyParent).NotifyParentEvent += new EventHandler(UserControlNotificationHandler); }
source share