469,572 Members | 1,281 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

Dynamically loading user controls on to page from App_Code folder

I have a set of pages that inherit from a base class in the App_Code folder.
The class looks something like:

public class MyBaseClass : System.Web.UI.Page

In various stages of the life cycle I need to add user controls to the page
control container. i.e:

protected override void OnInit(EventArgs e)
{
this.Controls.AddAt(0, myUserControl);
base.OnInit(e);
}
However, my user controls are not visible from the base class in App_Code
because of how ASP.NET 2.0 now compiles the projects. I've seen a few
suggestions on actually putting the .ascx file in the App_Code folder, but
this gives me a compile error of:

The file '/MyWebApp/App_Code/WebUserControl.ascx.cs' is in the special
directory 'App_Code', which is not allowed.

What is the solution for dynamically loading and adding user controls to the
page from a base class located in the App_Code folder?

--
Jay Douglas
http://www.jaydouglas.com

Nov 29 '05 #1
5 5894
Hello!

The solution is to move the .ascx files out of the app_code directory.

--
Venlig hilsen
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
Nov 30 '05 #2
On Tue, 29 Nov 2005 11:01:00 -0700, "Jay Douglas"
<ja****************@jaydouglas.com> wrote:

The file '/MyWebApp/App_Code/WebUserControl.ascx.cs' is in the special
directory 'App_Code', which is not allowed.

You can't use a .cs CodeFile if you put the ascx in App_Code - it will
have to be a standalone file with inline code.

What is the solution for dynamically loading and adding user controls to the
page from a base class located in the App_Code folder?


If you don't need to interact with the control in a strongly typed
manner (i.e. you don't need to know the class name), you can still use
LoadControl("file.ascx) and add the Control reference this method
returns to the Controls array.

If you need to interact with the control, i.e:

MyControl c = LoadControl("file.ascx") as MyControl;
c.SomeSpecialProperty = "foo";

then you'll need to define a base class or an interface in App_Code,
and derive the user control from that base class. Inside App_Code
you'll interact with the control through the base class.

Make sense?

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 30 '05 #3
On Wed, 30 Nov 2005 09:49:53 +0100, "Anders Borum"
<an****@sphereworks.dk> wrote:

The solution is to move the .ascx files out of the app_code directory.


That doesn't help if you want to see the type of the control inside
App_Code.

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 30 '05 #4
Just so I can verify your solution:

Create the WebUserControl.ascx file with inline code
<script language="csharp" runat="server">
private string someProperty;
public string SomeProperty
{
set {someProperty = value;}
}
</script>
With the inline .ascx file I need to inherit from an interface that defines
SomeProperty like ISomeProperty.

The System.Web.UI.Page base class in App_Code would use the syntax:
ISomeProperty myControl = LoadControl("WebUserControl.ascx");
myControl.SomeProperty = "foo";
this.Controls.AddAt(0, myControl);
Am I on the right track?

--
Jay Douglas
http://www.jaydouglas.com
"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:tv********************************@4ax.com...
On Tue, 29 Nov 2005 11:01:00 -0700, "Jay Douglas"
<ja****************@jaydouglas.com> wrote:

The file '/MyWebApp/App_Code/WebUserControl.ascx.cs' is in the special
directory 'App_Code', which is not allowed.


You can't use a .cs CodeFile if you put the ascx in App_Code - it will
have to be a standalone file with inline code.

What is the solution for dynamically loading and adding user controls to
the
page from a base class located in the App_Code folder?


If you don't need to interact with the control in a strongly typed
manner (i.e. you don't need to know the class name), you can still use
LoadControl("file.ascx) and add the Control reference this method
returns to the Controls array.

If you need to interact with the control, i.e:

MyControl c = LoadControl("file.ascx") as MyControl;
c.SomeSpecialProperty = "foo";

then you'll need to define a base class or an interface in App_Code,
and derive the user control from that base class. Inside App_Code
you'll interact with the control through the base class.

Make sense?

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 30 '05 #5
On Wed, 30 Nov 2005 14:16:29 -0700, "Jay Douglas"
<ja****************@jaydouglas.com> wrote:
Just so I can verify your solution:

Yes, you are on the right track. If you use an interface in app_code,
you don't nessecarily need to use inline code - you can use a
Codefile, too.

With the inline .ascx file I need to inherit from an interface that defines
SomeProperty like ISomeProperty.
Exactly

The System.Web.UI.Page base class in App_Code would use the syntax:
ISomeProperty myControl = LoadControl("WebUserControl.ascx");
myControl.SomeProperty = "foo";
this.Controls.AddAt(0, myControl);

ISomeProperty myControl =
LoadControl("WebUserControl.ascx") as ISomeProperty;

Just need to coherce the reference to the interface type and it should
all work..
Am I on the right track?


--
Scott
http://www.OdeToCode.com/blogs/scott/

Dec 1 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Donald Xie | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.