Custom Controls

In addition to creating user controls, which are essentially reusable portions of web pages, you can create your own compiled custom controls. As noted earlier, there are three ways to create custom controls: derive from an existing control, create a control that combines existing controls, or create a new control from scratch.

Composite controls are most similar to user controls. The key difference is that composite controls are compiled into a dll and used as you would any server control.

To get started, you’ll create a Web Control Library in which you’ll create the various custom controls for this chapter. Open VS2005 and choose New Project. In the New Project dialog box, select Visual C# Projects, then Windows, and create a Web Control Library called CustomControls, as shown in Figure 14-6.

Warning

For inexplicable reasons, the Web Control Library is listed under Windows and not directly under Visual C#, nor under New Web Site.

Visual Studio has created a complete custom control named WebCustomControl1. Build the application.

Warning

If you try to run this application, you will get a message telling you that a project with an output type of Class Library cannot be started directly.

Before examining this control in detail, open a second instance of VS2005 and create a web site named CustomControlWebPages. This will act as your consuming application or your client. Both of these phrases mean the same thing: CustomControlWebPages will use the custom controls you’ll put ...

Get Programming ASP.NET, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.