Home All Groups Group Topic Archive Search About

Strong typing for user controls

Author
13 Jan 2006 5:09 PM
Ryan
I'm a lot confused when using user controls in ASP.NET 2.0. How do I cast a
varaible to the type of a user control... for example:
Me.PlaceHolder1.Controls.Add(Page.LoadControl("~/App_Controls/ctlApplicationHome.ascx"))

' now, this doesn't work... why?
Dim apl As App_Controls_ctlApplicationHome = Me.PlaceHolder1.Controls(0);

Author
13 Jan 2006 5:15 PM
Ryan
Actually, let me clarify... the class "App_Controls_ctlApplicationHome" does
not exist... where is it!?!

Show quoteHide quote
"Ryan" wrote:

> I'm a lot confused when using user controls in ASP.NET 2.0. How do I cast a
> varaible to the type of a user control... for example:
>
> Me.PlaceHolder1.Controls.Add(Page.LoadControl("~/App_Controls/ctlApplicationHome.ascx"))
>
> ' now, this doesn't work... why?
> Dim apl As App_Controls_ctlApplicationHome = Me.PlaceHolder1.Controls(0);
>
>
Are all your drivers up to date? click for free checkup

Author
13 Jan 2006 5:15 PM
tdavisjr
Ryan wrote:
> I'm a lot confused when using user controls in ASP.NET 2.0. How do I cast a
> varaible to the type of a user control... for example:
>
> Me.PlaceHolder1.Controls.Add(Page.LoadControl("~/App_Controls/ctlApplicationHome.ascx"))
>
> ' now, this doesn't work... why?
> Dim apl As App_Controls_ctlApplicationHome = Me.PlaceHolder1.Controls(0);

Dim apl As App_Controls_ctlApplicationHome =
CType(Me.PlaceHolder1.Controls(0), App_Controls_ctlApplicationHome)

That should do it.

Just curious, what .NET language are you using?  It looks like VB but I
notice you have a semi-colon at the very end of your last line of code.
Show quoteHide quote
:)
Author
13 Jan 2006 5:34 PM
Ryan
Actually, here's a revised description of the issue:

I have two user controls, each of which sits in a directory called
"App_Controls" in an ASP.NET 2.0 site. When I try dynamically loading Control
#2 to a placeholder in Control #1, I cannot get a strongly typed reference to
that control, because the Type "App_Controls_ctlApplicationHome" is not
defined.

Does that make sense?

(to answer your question, I'm a C# developer working on a VB.NET project,
hence the schizophrenic coding)

Show quoteHide quote
"tdavisjr" wrote:

>
> Ryan wrote:
> > I'm a lot confused when using user controls in ASP.NET 2.0. How do I cast a
> > varaible to the type of a user control... for example:
> >
> > Me.PlaceHolder1.Controls.Add(Page.LoadControl("~/App_Controls/ctlApplicationHome.ascx"))
> >
> > ' now, this doesn't work... why?
> > Dim apl As App_Controls_ctlApplicationHome = Me.PlaceHolder1.Controls(0);
>
> Dim apl As App_Controls_ctlApplicationHome =
> CType(Me.PlaceHolder1.Controls(0), App_Controls_ctlApplicationHome)
>
> That should do it.
>
> Just curious, what .NET language are you using?  It looks like VB but I
> notice you have a semi-colon at the very end of your last line of code.
> :)
>
>
Author
14 Jan 2006 1:49 PM
Scott Allen
On Fri, 13 Jan 2006 09:34:02 -0800, "Ryan"
<R***@discussions.microsoft.com> wrote:

>
>I have two user controls, each of which sits in a directory called
>"App_Controls" in an ASP.NET 2.0 site. When I try dynamically loading Control
>#2 to a placeholder in Control #1, I cannot get a strongly typed reference to
>that control, because the Type "App_Controls_ctlApplicationHome" is not
>defined.
>

You need an @ Reference in your aspx page to the user control.

<%@ Reference VirtualPath="~/Products.ascx"  %>

That will make sure the user control type is visible in your code
file.

Example:
http://odetocode.com/Blogs/scott/archive/2005/06/30/1889.aspx

Author
16 Jan 2006 2:00 PM
Ryan
Ah, of course! Thanks!

Show quoteHide quote
"Scott Allen" wrote:

> On Fri, 13 Jan 2006 09:34:02 -0800, "Ryan"
> <R***@discussions.microsoft.com> wrote:
>
> >
> >I have two user controls, each of which sits in a directory called
> >"App_Controls" in an ASP.NET 2.0 site. When I try dynamically loading Control
> >#2 to a placeholder in Control #1, I cannot get a strongly typed reference to
> >that control, because the Type "App_Controls_ctlApplicationHome" is not
> >defined.
> >
>
> You need an @ Reference in your aspx page to the user control.
>
> <%@ Reference VirtualPath="~/Products.ascx"  %>
>
> That will make sure the user control type is visible in your code
> file.
>
> Example:
> http://odetocode.com/Blogs/scott/archive/2005/06/30/1889.aspx
>
> --
> Scott
> http://www.OdeToCode.com/blogs/scott/
>

Bookmark and Share