Creating ASP.Net User Control Libraries in Visual Studio 2010

If you try to find information on how to create a Web UserControl library for your ASP.Net web (application) project, you will most likely come across these articles by Scott Gu or Scott Allen, which either

  • do not work in Visual Studio 2010 (since VS2010 does not compile user controls into the ASP namespace), or
  • use Web Site Projects (which do not use project files, and therefore do not feature post-build events to automatically create assembly DLLs, but require manual publishing)

Another variant to solve the problem of shared user controls is to copy the ascx and dependent files from a master web control project to application projects (which I described previously) in the pre- or post-build events.

However, this approach does not solve the basic problem of web user controls: that the Visual Studio designer cannot use the compiled form of the user control, but rather parses the ascx to retrieve information on how to render it in design view.

Finally, I found this blog by Tim Gittos providing an approach that works in VS2010, namely to include the ascx as Embedded Resource and load the user control description contained in the ascx during an early stage in the ASP.Net page life cycle.

Instead of the OnLoad() event, I chose the FrameworkInitialize() virtual method to load the ascx, which seems to be the method called earliest in the life cycle with a valid value for the Page member:

protected override void FrameworkInitialize()
{
  base.FrameworkInitialize();

  string content = String.Empty;
  Stream stream = Assembly.GetExecutingAssembly()
    .GetManifestResourceStream(GetType(), GetType().Name + ".ascx");
  using (StreamReader reader = new StreamReader(stream))
  {
    content = reader.ReadToEnd();
  }
  Control userControl = Page.ParseControl(content);
  this.Controls.Add(userControl);
}
About these ads

4 Responses to Creating ASP.Net User Control Libraries in Visual Studio 2010

  1. [...] sketching the problem of creating User Control Libraries in VS 2010 and various ways to solve it (some of them no longer supported or cumbersome to use), here is a [...]

  2. [...] Creating ASP.Net User Control Libraries in Visual Studio 2010 [...]

  3. Mike says:

    Thanks a lot for this .. Any idea of why this is so danged difficult to achieve?

  4. Ankit Singh says:

    Very informative post. It’s really helpful for me and helped me lot to complete my task. Thanks for sharing with us. I had found another nice post over the internet which was also explained very well about Populate Grid Control From XML Document Easily, for more details of this post check out this link…

    http://www.mindstick.com/Articles/535bd817-c3c1-46dd-be9c-f14e42c7db78/?Creating%20User%20Define%20Control%20in%20ASP%20.Net

    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 66 other followers

%d bloggers like this: