Help casting a dynamic user control to the type i need

Discussion in 'ASP .Net' started by Tarun Mistry, Feb 25, 2006.

  1. Tarun Mistry

    Tarun Mistry Guest

    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
    Tarun Mistry, Feb 25, 2006
    #1
    1. Advertising

  2. Tarun Mistry

    Tarun Mistry Guest

    It appears the following code works:

    MyPlaceHolder.Controls.Add(LoadControl("AddTestReferralTestData.ascx"));
    AddTestReferralTestData refControl =
    (AddTestReferralTestData)MyPlaceHolder.Controls;
    Tarun Mistry, Feb 25, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. heyo
    Replies:
    3
    Views:
    900
    Dan Pop
    Apr 1, 2004
  2. pete
    Replies:
    4
    Views:
    793
    Dan Pop
    Apr 2, 2004
  3. Replies:
    2
    Views:
    424
  4. William
    Replies:
    5
    Views:
    1,011
  5. William
    Replies:
    3
    Views:
    646
    Andreas Leitgeb
    Mar 4, 2011
Loading...

Share This Page