ASP.NET 2.0 User Controls in App_Code

Here is a tip I picked up at the MVP summit from ASP.NET team member Simon Calvert.

You can place a user control file in App_Code, as long as the .ascx uses inline code. The user control will compile into the App_Code assembly. Instead of using LoadControl with a virtual path parameter, you can use the overloaded version accepting a Type parameter.

As an example, here is the contents of MyUserControl.ascx. Place the file in the App_Code directory.

<%@ Control Language="C#" ClassName="MyUserControl" %>

 

<script runat="server">

 

  protected override void OnLoad(EventArgs e)

  {

    base.OnLoad(e);

    Label.Text = "Bonjour!";

  }   

 

</script>

<asp:Label runat="server" ID="Label" />

Here is the code for a web form that dynamically loads the control.

using System;

using ASP;

 

public partial class _Default : System.Web.UI.Page

{

    protected override void OnInit(EventArgs e)

    {

        base.OnInit(e);

 

        MyUserControl c;

        c = LoadControl(typeof(MyUserControl), null)

            as MyUserControl;

 

        if (c != null)

        {

            Controls.Add(c);

        }

    }

}

Note: No @ Reference directive was required in the .aspx file.

Why is this interesting?

I was conversing with Shawn Wildermuth, and Shawn is unhappy with the solutions needed to make types visible in the ASP.NET 2.0 compilation model, solutions like using @ Reference and stubs. Shawn is not the only one. The above solution is easy if you don’t mind using the App_Code directory. The user control type will be visible to both the App_Code assembly and all web form assemblies.

posted on Saturday, October 01, 2005 7:31 PM by scott

Comments

Sunday, October 02, 2005 8:53 AM by LA.Net

# Utilizar User Controls na pasta app_code

Thursday, October 20, 2005 12:38 PM by Anand

# re: ASP.NET 2.0 User Controls in App_Code

I tried this out and it didn't seem to be working atleast in VB (using Visual Web Developer 2005 Express Edition). Am I missing something?

Heres what I did. I added a user control (Quotes.ascx), lets say in the App_Code section.

In the aspx page, I am trying to reference the control as follows.
Dim q As Quotes

But it keeps saying Quotes is undefined. Note as per your writing, I didn't include a "register" in the aspx page.
Friday, October 21, 2005 6:00 AM by Scott Allen

# re: ASP.NET 2.0 User Controls in App_Code

Hi Anand:

I have not tried express but I wonder if this was a capability added to the release candidate that was not in beta 2. Are you trying beta 2? I used the RC.
Thursday, October 27, 2005 4:29 AM by Sbyard

# re: ASP.NET 2.0 User Controls in App_Code

It doesn't work in Beta2.

Still, November soon, so we'll all have the access to the release build!

It'll be worth checking this out though as another option for WUC's
Monday, February 06, 2006 7:00 PM by Nick Lang

# re: ASP.NET 2.0 User Controls in App_Code

I have actaully found that this is not true. I simply put the <%@ Control Inherits="Web.Layouts.Classname" %> on my ascx file (which is not in App_Code). I do not include the Src attribute. Then I put a .cs file which contains Web.Layouts.Classname somewhere in App_Code and it works. So you can separate the two. Please email me at bangbangnicklang@eml.cc if you have comments or find this to not be true.
Thanks!
Monday, February 06, 2006 7:09 PM by scott

# re: ASP.NET 2.0 User Controls in App_Code

Nick:

You can certainly inherit from a class in App_Code or a class library.
The only way to put the .ascx itself in App_Code is to use inline code.
Thursday, March 16, 2006 7:05 PM by Bill Mild

# re: ASP.NET 2.0 User Controls in App_Code

One problem. I have a class named Content. And, when you place an ascx file in App_Code folder, it becomes part of ASP namespace. Once it is in ASP nameaspace, the System.Web.UI.WebControls.Content object conflicts with my class. So, in an ItemDataBound event of my ascx file, I have Content content = (Content)e.Item.DataItem, but unfortunately, the application thinks that Content is from WebControls, instead of my class.

Either I go back to <%Reference Control or I put Content in a namespace, I guess.
Friday, March 17, 2006 7:46 AM by scott

# re: ASP.NET 2.0 User Controls in App_Code

Bill, you could always reference the type as ASP.Content.
Friday, March 17, 2006 10:52 AM by Bill Mild

# re: ASP.NET 2.0 User Controls in App_Code

Yes, that worked. Thanks for the encouraging advice, Scott.
Monday, October 23, 2006 2:22 PM by Greg Graham

# re: ASP.NET 2.0 User Controls in App_Code

Thanks! I'm late to the thread, but this advice really helps us.
Thursday, November 16, 2006 5:13 AM by Jeff Northrop

# re: ASP.NET 2.0 User Controls in App_Code

Thanks for the code! However, I can't seem to get it to compile. I'm getting the following error.

The type or namespace name 'MyUserControl' could not be found (are you missing a using directive or an assembly reference?)

VS recognizes the control in intellisence and I can run the page by creating a virtual directory to my website folder, but it won't compile for me.

Any help would be greatly appreciated.
Jeff