Help casting a dynamic user control to the type i need

T

Tarun Mistry

Hi all, I am adding dynamic user controls with the simple code as follows:

MyPlaceHolder.Controls.Add(LoadControl("MyUserControl.ascx"));

I then want to run a member on my user control, something like:

MyPlaceHolder.Controls.TheMemberFunction();

However I cannot do this as it obviously has not cast the User Control to
its own type and therefore cannot reference the member function.

How can i cast my User Control into the "MyUserControl" before adding it
into the controls array for the place holder (or after for that matter?).
Any attemps sees error along the lines of "A control cannot be case to
UserControl".

Thank you for any help.
Kind Regards
Tarun
 
T

Tarun Mistry

It appears the following code works:

MyPlaceHolder.Controls.Add(LoadControl("AddTestReferralTestData.ascx"));
AddTestReferralTestData refControl =
(AddTestReferralTestData)MyPlaceHolder.Controls;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top