Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add a web user control to a class library?

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?

like image 625
JoshRivers Avatar asked Dec 29 '08 20:12

JoshRivers


People also ask

How do I add user control to my library?

Right click the project in the Solution Explorer and select Add > User Control…. In the dialog that appears, name the user-control file and click Add. Inside the project, add controls and functionality to the UserControl ( here I added 2 radiobuttons(Bold, Italic) and.

What is Web user control?

User controls are containers into which you can put markup and Web server controls. You can then treat the user control as a unit and define properties and methods for it. Custom controls. A custom control is a class that you write that derives from Control or WebControl.


2 Answers

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.

like image 121
splattne Avatar answered Oct 22 '22 19:10

splattne


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

like image 33
Andreas Grech Avatar answered Oct 22 '22 21:10

Andreas Grech