ASP.NET 2.0 User Controls in App_Code

Saturday, October 1, 2005

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.


Comments
Anand Thursday, October 20, 2005
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.
Scott Allen Friday, October 21, 2005
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.
Sbyard Thursday, October 27, 2005
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
Nick Lang Tuesday, February 7, 2006
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!
scott Tuesday, February 7, 2006
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.
Bill Mild Friday, March 17, 2006
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.
scott Friday, March 17, 2006
Bill, you could always reference the type as ASP.Content.
Bill Mild Friday, March 17, 2006
Yes, that worked. Thanks for the encouraging advice, Scott.
Greg Graham Monday, October 23, 2006
Thanks! I'm late to the thread, but this advice really helps us.
Jeff Northrop Thursday, November 16, 2006
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
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!