views:

996

answers:

6

I'm looking at building some web user controls with an eye toward re-use, but I can't seem to add a Web User Control in my class library in VS2008. Is there a way to work around this problem, or is there a better approach to creating reusable controls?

A: 

Hi,

There is a project template called "ASP.NET Server Control" that I assume you can use...

--larsw

larsw
+2  A: 

Follow the following steps (from this post by Phil Haacked):

  1. Close VS.NET 2005.

  2. Open the directory C**:\Program Files\Microsoft Visual Studio 8\Web\WebNewFileItems\CSharp** (assuming a default installation of VS.NET).

  3. Open the CSharpItems.vsdir file in Notepad. Select the text and copy it to the clipboard.

  4. Now open up the file C:\Program Files\Microsoft Visual Studio 8\VC#\CSharpProjectItems\CSharpItems.vsdir and paste the contents of the clipboard underneath the existing text.

  5. Now copy the contents of C:\Program Files\Microsoft Visual Studio 8\Web\WebNewFileItems\CSharp (excluding CSharpItems.vsdir) into the folder C:\Program Files\Microsoft Visual Studio 8\VC#\CSharpProjectItems.

Now “Web User Control” should be an option when you select Add | New Item.

Reference: http://haacked.com/archive/2006/02/07/addingwebusercontroltoaclasslibraryinvs.net2005.aspx

Andreas Grech
+4  A: 

You can create either Web User Controls or Web Custom Controls that encapsulate the functionality you need. The main difference between the two controls lies in ease of creation vs. ease of use at design time.

You should maybe consider creating a Web Custom Control library. There is a walkthrough for creating a web custom control using the Web Control Library template.

According to the MSDN article "Recommendations for Web User Controls vs. Web Custom Controls" these are the differences between the two types of controls:

Web user controls are easy to make, but they can be less convenient to use in advanced scenarios. You develop Web user controls almost exactly the same way that you develop Web Forms pages. Like Web Forms, user controls can be created in the visual designer, they can be written with code separated from the HTML, and they can handle execution events.

However, because Web user controls are compiled dynamically at run time they cannot be added to the Toolbox, and they are represented by a simple placeholder glyph when added to a page. This makes Web user controls harder to use if you are accustomed to full Visual Studio .NET design-time support, including the Properties window and Design view previews.

Also, the only way to share the user control between applications is to put a separate copy in each application, which takes more maintenance if you make changes to the control.

Web custom controls are compiled code, which makes them easier to use but more difficult to create; Web custom controls must be authored in code. Once you have created the control, however, you can add it to the Toolbox and display it in a visual designer with full Properties window support and all the other design-time features of ASP.NET server controls.

In addition, you can install a single copy of the Web custom control in the global assembly cache and share it between applications, which makes maintenance easier. For more information see global assembly cache.

splattne
A: 

You can do anything in a class library.

  1. Add reference to System.Web
  2. Create your new Control class that inherits from WebControl or HtmlControl or whatever.

That's it. You now have a reusable control for ASP.NET.

You could do some special things like add attributes to your class and properties, but they are really not needed.

[DefaultProperty("Text")]
[Category("...")]
[DefaultValue("")]
A: 

You could using virtual path providers but you you should consider whether it really is worth your wile. Consider this codeproject article on the subject.

Cristian Libardo
A: 

As platte's link mentions, if you're going for reuse then Web User Controls aren't very good. The ascx file has to be manually copied to every project you want to use them in, or you have to hack your way around that.

It's better to use System.Web.UI.WebControls.WebControl which is what you get when you add an "ASP.NET Server Control". These are designed for reuse. If one of the existing framework controls fits the bill for the most part and you just need to extend the functionality of it, then add an "ASP.NET Server Control" and change it to inherit from Panel or Menu or whatever.

If you're still determined to get reusable Web User Controls to work, then this article by The Gu should set you on the right path.

sliderhouserules