c# - Binding to events in dynamically loaded user controls -


i constructing test project demonstrate (to me) how dynamically load user controls. works except wiring of control notify parent has happened. problem line tries connect event in user control event handler in parent page. "notifyparentevent" event not visible , compiler barfs on (the event not defined in system.web.ui.control). have tried using usercontrol instead of control, no avail.

control control = page.loadcontrol(savedcontrolvirtualpath); if (control != null) {     control.id = control.gettype().name; >>>>>    control.notifyparentevent += new eventhandler(usercontrolnotificationhandler);     controlplaceholder.controls.add(control); } 

code behind user control:

public partial class usercontrols_webusercontrol1 : system.web.ui.usercontrol {     public event commandeventhandler notifyparentevent;      private void notifyparent(string message)     {         if (notifyparentevent != null)         {             commandeventargs e = new commandeventargs("control1 action", message);             notifyparentevent(this, e);         }     } } 

parent page:

public partial class _default : system.web.ui.page {     private string savedcontrolvirtualpath     {         {return (viewstate["savedcontrolpath"] == null || (string)viewstate["savedcontrolpath"] == string.empty)                  ? null : (string)viewstate["savedcontrolpath"]; }         set { viewstate["savedcontrolpath"] = value; }     }      private void reloadcontrol()     {         controlplaceholder.controls.clear();         if (savedcontrolvirtualpath != null)         {             control control = page.loadcontrol(savedcontrolvirtualpath);             if (control != null)             {                 // gives control unique id. important ensure                 // page working properly. here use control.gettype().name                 // id.                 control.id = control.gettype().name;                 control.notifyparentevent += new eventhandler(usercontrolnotificationhandler); <== line won't compile                 controlplaceholder.controls.add(control);             }         }     }      private void usercontrolnotificationhandler(object sender, commandeventargs e)     {         //  ???     }      protected void page_init(object sender, eventargs e)     {         lbllastevent.text += "page_init<br />";         loadusercontrols();     }      protected void page_load(object sender, eventargs e)     {         lbllastevent.text += string.format("{0} on main page<br />", (this.ispostback) ? "postback" : "page_load");         reloadcontrol();     }      protected void rblcontrolselector_changed(object sender, eventargs e)     {         lbllastevent.text += "rblcontrolselector_changed<br />";         loadusercontrols();     }      private void loadusercontrols()     {         label lbl = new label();         controlplaceholder.controls.clear();         switch (rblcontrolselector.selectedvalue)         {             case "0":                 lbllastevent.text = "unload/clear<br />";                 savedcontrolvirtualpath = "";                 break;              case "1":                 lbllastevent.text += "adding control #1<br />";                 savedcontrolvirtualpath = "~/usercontrols/webusercontrol1.ascx";                 break;              case "2":                 lbllastevent.text += "adding control #2<br />";                 savedcontrolvirtualpath = "~/usercontrols/webusercontrol2.ascx";                 break;              case "3":                 lbllastevent.text += "adding control #3<br />";                 savedcontrolvirtualpath = "~/usercontrols/webusercontrol3.ascx";                 break;          }         if (!string.isnullorempty(savedcontrolvirtualpath))         {             reloadcontrol();         }     } } 

option 1

you need cast control type implements notifyparentevent event in order attach event handler. basically, in reloadcontrol method, replace line of code:

control.notifyparentevent += new eventhandler(usercontrolnotificationhandler); 

with following:

if(control usercontrols_webusercontrol1)  {     (control usercontrols_webusercontrol1).notifyparentevent += new eventhandler(usercontrolnotificationhandler); } 

option 2

a more generic approach create interface , check if dynamic control implements interface.

create 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 dynamic control implements interface:

if(control inotifyparent)  {     (control inotifyparent).notifyparentevent += new eventhandler(usercontrolnotificationhandler); } 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -