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()

  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);

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…


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: