views:

1171

answers:

4

We have a bunch of user controls we would like to pull out of a web application and into a separate assembly/library, and I thought it would be as simple as creating a class library and pulling the ascx and ascx.cs files into the project, and compiling a DLL to be reused among our applications.

This was not the case, however.

Our ultimate goal is to have a single distributable DLL (similar to how Telerik distributes their controls) that we can throw into any web application. The steps here: http://msdn.microsoft.com/en-us/library/aa479318.aspx were very simple to follow, however this results in many files named controlname.ascx.guid.dll, which is not the desired result. I couldn't even get these to work anyways, since we have additional classes that need to be compiled into the assembly.

Has anyone successfully created a web user control library in .NET (we're using 3.5 here)? I can't seem to find a nice step-by-step guide.

+4  A: 

If you want to share controls among project, my experience has shown that the best way is to build custom asp.net server controls instead of usercontrols. User controls are good for sharing within the same project, but not over multiple ones.

For this purpose I suggest you to build a set of custom server controls inside a class library and use that on all of your projects.

This book does quite a good job at explaining the basics of creating server controls

Edit:
I'm currently developing a .net web server control library. I actually didn't follow any step-by-step guide. I mostly considered using the book I mentioned above and the MSDN library + Reflector, which is a great tool for inspecting existing MS server controls and learning from them.

Juri
Yea, we were considering that.
Keith
So you consider writing custom server controls?
Juri
Yes, but the problem lies in the fact that the user controls are currently using asp.net controls throughout. I would prefer not having to write out the HTML to emulate a ListView that we have within a user control, for example.
Keith
Of course you're using asp.net controls. That's perfectly fine. You can mimic the ListView internally by either using it directly or by using a Repeater control. Creating server controls doesn't necessarily mean to write the whole Hml rendering yourself. When I combine other asp.ne controls together I rarely override the Render(...) myself.
Juri
I have currently just accss over my iPhone. I'll provide an example today evening.
Juri
+4  A: 

I realize this is an old topic, but if anyone is looking for a solution for creating reusable user control libraries, it turns out it's fairly simple. Here are two good step-by-step guides along with source code:

From MSDN: Turning an .ascx User Control into a Redistributable Custom Control

From Code Project: www.codeproject.com/KB/aspnet/WebLibraryMaker.aspx (sorry to make you cut and paste, but I only get one hyperlink as this is my first post :p)

The second link provides a solution to the multiple dlls created by the first link.

Erasmus777
A: 

I found the tutorial Creating and Using User Control Libraries but it seems like a bit of a hack as it relies on a post-build command line event to copy the user controls from one project to another.

Daniel Ballinger
+1  A: 

Somewhat late, I admit.

To create a re-usable library of user controls; create a new Web Application Project, delete all the scaffolding, add a (number of) user control(s). Create a Web Deployment Project from the Web Application Project, in the WDP properties choose the option to Merge all control output and assign a name for the library and ensure that Allow this website to be updatable is NOT checked.

Build the WDP and use Reflector to examine the generated library; you'll see that it contains an ASP namespace and the types you carefully crafted have been renamed i.e. usercontrol_ascx. In your target website(s) add references to BOTH the output dlls from your WDP, add a system.web/pages/controls node to web.config using the namespace ASP and the name of the assembly that you defined in the WDP.

Now when you use the library in a page (for example) you must use the alias that you defined in web.config and the typename as seen in Reflector i.e.

<ucl:usercontrol_ascx ... />

I found it useful to add a dependancy for the website(s) on the WDP so that the WDP is built before the websites; now I can change the user controls in the WAP without having to remember to build the WAP before building the website(s).

I hope that someone finds this useful as it cost me a few grey hairs getting to this stage and still have VS do its 'automagically' thing.

Scott Rickman