开发者

How to handle dynamic user control events in aspx page

I have an aspx webpage in which an user 开发者_开发问答control is added dynamically as follows:

UserControl testUsrControl = LoadControl("TestUsrControl") as UserControl;
testUsrControl.ID ="test";

Then I tried adding an event handler of user control inside aspx like below:

testUsrControl.Drpdatafield_SelectIndexChanged += new EventHandler(this.Drpdatafield_SelectIndexChanged);

But this line is giving error at **testUsrControl.Drpdatafield_SelectIndexChanged **. The error is "Drpdatafield_SelectIndexChanged" doesn't exist in UserControl.

How can get the testUsrControl's events inside aspx page dynamically.

Thanks, Rupa


You need to cast the control to the correct type (say MyUserControlType) and then verify that it's ok

MyUserControlType testUsrControl = LoadControl("TestUsrControl") as MyUserControlType;
if(testUsrControl != null {
          testUsrControl.Drpdatafield_SelectIndexChanged += new EventHandler(this.Drpdatafield_SelectIndexChanged)
}

You get the type from ClassName in the usercontrol markup file <% @ Control Language="C#" ClassName="MyUserControlType" %>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜