Loading a ASCX Control on the page and gertting a reference to it

Discussion in 'ASP .Net Web Controls' started by Filippo, Jun 12, 2004.

  1. Filippo

    Filippo Guest

    Hi there,
    I am loading at RunTime a MENU.ascx control on the page and since I
    need to update the URL paths, depending on the page that is making the
    call, I need to get a reference to the MENU and reset the URL.


    CODE:
    ======================================
    // Get a refence of the control
    MyMenu aMenu = (MyMenu) LoadControl("menu.ascx");

    // update the URLs
    UpdateURLs(aMenu);

    // loads the control on the page
    Page.Controls.Add(aMenu);
    ======================================


    ISSUE:
    When it runs the first line, the one that Loads the Control
    an EXCEPTION occurrs:

    >> Specified cast is not valid.


    ======================================
    The Control exists and loads ok if I directly load it into the page,
    but I still need to get a reference of it in order to reset the URLS
    of each node.


    Any idea why?
    F.
     
    Filippo, Jun 12, 2004
    #1
    1. Advertising

  2. LoadControl creates a PartialCachingControl object which is not actually
    your control yet. First you have to put this control inside some container
    and then reference to your control like this:

    PartialCachingControl dynaControl = LoadControl("menu.ascx");
    // have some container on your page
    Page.Controls.Add(dynaControl);
    MyMenu aMenu = (MyMenu)dynaControl.CachedControl;
    UpdateURLs(aMenu);

    --
    RobertK
    { Clever? No just smart. }

    "Filippo" <> wrote in message
    news:...
    > Hi there,
    > I am loading at RunTime a MENU.ascx control on the page and since I
    > need to update the URL paths, depending on the page that is making the
    > call, I need to get a reference to the MENU and reset the URL.
    >
    >
    > CODE:
    > ======================================
    > // Get a refence of the control
    > MyMenu aMenu = (MyMenu) LoadControl("menu.ascx");
    >
    > // update the URLs
    > UpdateURLs(aMenu);
    >
    > // loads the control on the page
    > Page.Controls.Add(aMenu);
    > ======================================
    >
    >
    > ISSUE:
    > When it runs the first line, the one that Loads the Control
    > an EXCEPTION occurrs:
    >
    > >> Specified cast is not valid.

    >
    > ======================================
    > The Control exists and loads ok if I directly load it into the page,
    > but I still need to get a reference of it in order to reset the URLS
    > of each node.
    >
    >
    > Any idea why?
    > F.
     
    Robert Koritnik, Jun 14, 2004
    #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. Quentin
    Replies:
    1
    Views:
    521
    Quentin
    Jul 29, 2003
  2. Jason Shohet
    Replies:
    1
    Views:
    516
    Kevin Spencer
    Nov 10, 2003
  3. Holger (David) Wagner
    Replies:
    2
    Views:
    1,981
    Holger (David) Wagner
    Jul 3, 2004
  4. Shawn Meyer

    Dynamically loading ascx page and having events fire

    Shawn Meyer, Jan 31, 2004, in forum: ASP .Net Building Controls
    Replies:
    10
    Views:
    337
    Alessandro Zifiglio
    Feb 9, 2004
  5. Joe
    Replies:
    1
    Views:
    413
    Jeffrey Tan[MSFT]
    Feb 16, 2004
Loading...

Share This Page