views:

28

answers:

1

Hi, 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

+2  A: 

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" %>

Preet Sangha
Here, is th MyUserControlType name of the user control??If so, I am getting the name of the user control name inside the aspx page even after registering the user contro in aspx page.Could you please elobrorate this?
Rupa
I got the type from user control mark up file, but to relate user control and the aspx page?
Rupa