LCJ
LCJ

Reputation: 22652

Subscribing event of Dynamically Loaded User Control

I have two usercontrols. One is loaded statically another dynamically.

In the aspx page I can write as follows for static user control.

 ProgramAddSchedule.onNewProgram += new AddProgram.OnNewProgramClick(onNewProgram_btnHandler);

But dynamically loaded control’s event I am not able to get

 Control myUserControl = (Control)Page.LoadControl("~/View/EditScheduleProgram.ascx");

How do I get the event of the dynamically loaded control?

Upvotes: 2

Views: 197

Answers (2)

Grant Thomas
Grant Thomas

Reputation: 45083

Assuming the name of the type follows the name of the file, you can strongly-type it to that type, such that,

var myUserControl = Page.LoadControl("~/View/EditScheduleProgram.ascx") as EditScheduleProgram;

Then you have access to any custom (or specific) events exposed by that type but not by Control, so,

myUserControl.MyEvent += MyEventHandler;

Upvotes: 2

Stecya
Stecya

Reputation: 23266

Cast to your Control Type

 YourControlType myUserControl =(YourControlType)Page.LoadControl("~/View/EditScheduleProgram.ascx");
 myUserControl.onNewProgram += new AddProgram.OnNewProgramClick(onNewProgram_btnHandler);

Upvotes: 2

Related Questions