473,320 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 6065
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am have facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
7
by: Samuel | last post by:
Hi, I am building a page that makes use of user control as a templating technique. The following is that I have in mind and it is actually working: Root/ -- login.aspx -- login.aspx.vb --...
7
by: Alan Silver | last post by:
Hello, I am just looking at VWD and seeing what needs doing to take an existing site I've written by hand and importing it into VWD. I've already discovered that I need to rename my code-behind...
2
by: A.Wussow | last post by:
Hi Everybody, i want to load dynamically content from some user controls (with forms, or some data-controls) using atlas. So i use an UpdatePanel for loading the user control into a placeholder....
4
by: Eric | last post by:
I got a particular problem in visual studio 2005 There's a user control on page and I want to meka a cast like this MyPage mp=(MyPage)this.Page; Error is : cannot cast from...
11
by: Web Search Store | last post by:
Hello, I set up a web page with 2 user controls. In classic asp, the first one did all the declarations, and the second one used the values, and could reset it. In ASP.Net so far I can't...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.