Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Loading a ASCX Control on the page and gertting a reference to it

Reply
Thread Tools

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

 
 
Filippo
Guest
Posts: n/a
 
      06-11-2004
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.
 
Reply With Quote
 
 
 
 
Robert Koritnik
Guest
Posts: n/a
 
      06-14-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple *.ascx files with a single *.ascx.cs in VS.NET 2003 Holger (David) Wagner ASP .Net 2 07-03-2004 09:23 AM
datasource of child .ascx not visible to parent .ascx Joe ASP .Net Web Controls 1 02-16-2004 07:27 AM
Dynamically loading ascx page and having events fire Shawn Meyer ASP .Net Building Controls 10 02-09-2004 07:55 PM
can a dg be added to an ascx? ascx call a ws? Jason Shohet ASP .Net 1 11-10-2003 07:08 PM
[ASCX] Add an ascx in a webcontrol... Quentin ASP .Net 1 07-29-2003 07:37 PM



Advertisments