Thursday, March 22, 2012

Strong typing for user controls

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);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.
:)
Actually, let me clarify... the class "App_Controls_ctlApplicationHome" does
not exist... where is it!?!

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

"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.
> :)
>
On Fri, 13 Jan 2006 09:34:02 -0800, "Ryan"
<Ryan@.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/ar...06/30/1889.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/
Ah, of course! Thanks!

"Scott Allen" wrote:

> On Fri, 13 Jan 2006 09:34:02 -0800, "Ryan"
> <Ryan@.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/ar...06/30/1889.aspx
> --
> Scott
> http://www.OdeToCode.com/blogs/scott/

0 comments:

Post a Comment