Strong typing for user controls

G

Guest

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

Guest

Actually, let me clarify... the class "App_Controls_ctlApplicationHome" does
not exist... where is it!?!
 
T

tdavisjr

Ryan said:
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.
:)
 
G

Guest

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

Scott Allen

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
 

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

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top