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.

Print | posted @ Saturday, October 01, 2005 11:31 PM

Comments on this entry:

Gravatar # re: ASP.NET 2.0 User Controls in App_Code
by Anand at 10/20/2005 7:38 PM

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.
  
Gravatar # re: ASP.NET 2.0 User Controls in App_Code
by Scott Allen at 10/21/2005 1:00 PM

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.
  
Gravatar # re: ASP.NET 2.0 User Controls in App_Code
by Sbyard at 10/27/2005 11:29 AM

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
  
Gravatar # re: ASP.NET 2.0 User Controls in App_Code
by Nick Lang at 2/7/2006 3:00 AM

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!
  
Gravatar # re: ASP.NET 2.0 User Controls in App_Code
by scott at 2/7/2006 3:09 AM

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.
  
Gravatar # re: ASP.NET 2.0 User Controls in App_Code
by Bill Mild at 3/17/2006 2:05 AM

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.
  
Gravatar # re: ASP.NET 2.0 User Controls in App_Code
by scott at 3/17/2006 2:46 PM

Bill, you could always reference the type as ASP.Content.
  
Gravatar # re: ASP.NET 2.0 User Controls in App_Code
by Bill Mild at 3/17/2006 5:52 PM

Yes, that worked. Thanks for the encouraging advice, Scott.
  
Gravatar # re: ASP.NET 2.0 User Controls in App_Code
by Greg Graham at 10/23/2006 9:22 PM

Thanks! I'm late to the thread, but this advice really helps us.
  
Gravatar # re: ASP.NET 2.0 User Controls in App_Code
by Jeff Northrop at 11/16/2006 1:13 PM

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
  

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 6 and 4 and type the answer here: