views:

549

answers:

6

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!

A: 

You will probably want to look into Server Controls instead. AFAIK you can't compile User Controls into a single DLL.

Marek Karbarz
+7  A: 

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

Bob
I have followed the steps, but am having problems integrating the assembly into my site. I set the "unupdateable" option, and I have reference the .dll file in my new project. I can't figure out how to actually declare it on my page.
Mike C.
Is is jut me, or does this method STILL require you to deploy the markup .ascx files ? "1.Copy the appropriate .aspx/.ascx files from the built [...]"
Radu094
You still need the physical file there, they will be empty though.
Bob
+2  A: 

Update: I was right on my hint on using the pre-compiled user control as you would an user control.

For a step by step on how, see Turning an .ascx User Control into a Redistributable Custom Control

There is no need for any .ascx files in the project where you'll use it, so this matches exactly what you were looking for.


User Controls aren't meant to be reused across sites, use custom server controls instead.

You can compose the server controls using existing controls, and set it up all so you can style through css.

If you are still going for the user control approach, try what Bob mentioned in the answer. I haven't done it with user controls, but I would expect to be able to use the output of that approach like you would use a custom server control.

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls"%>
...
<aspSample:WelcomeLabel Text="Hello" NameForAnonymousUser="Guest" 
   ID="WelcomeLabel1" runat="server" BackColor="Wheat" ForeColor="SaddleBrown" />

Ps. if that doesn't work, the link that he provided tells you to copy the ascx files anyway, so that wouldn't be what you were looking for i.e.try the above & if not stick to custom server controls.

eglasius
Of course you can use a user control across multiple pages
CRice
@boon oops, meant to say across sites - which have been clear given the question of the OP ;)
eglasius
A: 

If we are talking about web-forms, The answer is no. The markup (.ascx file contents) is not compiled into the dll. The ASP.NET engine expects to find this file in the file system, along with the web application. So it is not possible to create a DLL that you can simply redistribute or share.

If you want to create controls that can be shared across web-applications, you can create custom controls instead, but then you have to code up the HTML in .net code (preferebly using the appropriate classes for the task instead of just creating html in text), which can get pretty ugly if the control is big.

Personally, I think it is pretty annoying that it is this way.

I think however, that if you are using ASP.NET MVC, there are some possibilities (depending on your view engine), but I don't have much experience with MVC.

Pete
The answer is not no, take a look at my answer.
Bob
@bob, your answer does not provider a class library (as in one dll) with embedded user controls, but a bunch of dlls, and respective ascx files. You still need to place those files in the destination web application to be able to use the controls. So the answer to the question, as it was phrased, is no. Your answer may however be a good alternative.
Pete
The file contents do get compiled into the dll. You need the ascx files as placeholders. Having multiple DLLs just depends on the structure of your project. There is no reason why it can't be combined into one.
Bob
"preferebly using the appropriate classes for the task instead of just creating html in text" -- can you give me a quick explanation so I start off correctly? Thanks!
Mike C.
A: 

It sounds like custom server controls are what you are looking for.

If your controls are a combination of other server controls (e.g. a textbox and a button for a date control), then you could look at CompositeDataBoundControl and CompositeControl - these are handy ways of working with this sort of thing.

Beware, however, that it is very easy to get confused by the way the page cycle/viewstate re-hydration works. I've lost quite a bit of time over this, due to subtle misunderstandings of how it should work.

http://aspnetresources.com/blog/composite_databound_control.aspx

Paddy
A: 

You can serve User Controls from a dll. You will just need to create a custom VirtualPathProvider to load the controls. This is essentialy what SharePoint does.

Here is an article from Microsoft that explains the process:

http://support.microsoft.com/kb/910441

You can also do a search for 'VirtualPathProvider'.

This example loads the code from a database, but you can modify it to load the code from the assembly as an embedded resource. We are doing this at my company to share a Master page across many sites and appdomains.

One caveat, however. This will not work in a Medium trust environment. So if you are planning on deploying this to a shared hosting environment, make sure to do a test on your host to see if it will work first.

Jason