Dynamically loading user controls on to page from App_Code folder

J

Jay Douglas

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

Scott Allen

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?
 
J

Jay Douglas

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

Scott Allen

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..
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top