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

F

Filippo

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:

======================================
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.
 
R

Robert Koritnik

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);
 

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

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top