简体   繁体   English

动态加载用户控件的订阅事件

[英]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. 在aspx页面中,我可以为静态用户控件编写如下内容。

 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? 如何获取动态加载控件的事件?

Cast to your Control Type 转换为您的控件类型

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

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, 然后,您可以访问该类型而不是Control公开的任何自定义(或特定)事件,因此,

myUserControl.MyEvent += MyEventHandler;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM