Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET UserControl class library

Is it possible to create class libraries that contain UserControls so I can reuse them? If so, how? Is the markup compiled with the .dll? Thanks for any help!

like image 860
Mike Cole Avatar asked Nov 19 '09 19:11

Mike Cole


1 Answers

You can compile both UserControls and Pages into class libraries because ultimately, this is what happens after your web site is just in time compiled. The process is a little involved because really UserControls and Pages aren't meant to be used across applications.

From MSDN:

The UserControl gives you the ability to create controls that can be used in multiple places within an application or organization

http://msdn.microsoft.com/en-us/library/system.windows.forms.usercontrol.aspx

The prefered method for controls that are going to be used across applications would be to create custom web server controls.

If you really want to stick with UserControls though, the basic process to obtain this functionality is as follows:

  1. Create a new web application project.
  2. Develop the reuseable UserControls.
  3. Publish the site as an un-updatable* site. (uncheck Allow this site to be updatable)
  4. Copy the compiled library from the bin directory and the ascx files as needed from the published site into the new site.

    • The un-updatable option is what brings the markup into the assembly. This is an important step.

Yes, as point number four states, you do need to copy the ascx files. The markup will be contained in the class library and the ascx will actually be empty. There is no way to avoid this (unless you use custom web server controls) because UserControls are added to Pages through their file names.

All of this is documented in more detail over at MSDN,

http://msdn.microsoft.com/en-us/library/aa479564.aspx

like image 169
Bob Avatar answered Oct 13 '22 02:10

Bob