Chapter 13. Custom Controls

In ASP.NET, Microsoft distinguishes between user controls , which are segments of ASP.NET pages that can be reused in other pages, and custom controls, which are compiled controls that act, from the client's perspective, much like web (ASP) controls. Custom controls can be created in one of three ways:

  • By deriving a new custom control from an existing control (e.g., deriving your own specialized text box from asp:textbox). This is known as a derived custom control.

  • By combining two or more existing controls into a new control. This is known as a composite custom control.

  • By deriving from the base control class, thus creating a new custom control from scratch. This is known as a full custom control.

User Controls

User controls allow you to save a part of an existing ASP.NET page and reuse it in many other ASP.NET pages. A user control is almost identical to a normal .aspx page, with two differences: the user control has the .ascx extension rather than .aspx, and it cannot have <HTML>, <Body>, or <Form> tags.

The simplest user control is one that displays HTML only. A classic example of a simple user control is an HTML page that displays a copyright notice. To demonstrate this, create a new web application named UserControls. Once the application is open, right-click on the project and choose Add New Item. Highlight Web User Control and name your new control Copyright.ascx, as shown in Figure 13-1.

Example 13-1 shows the complete listing for Copyright.ascx ...

Get Programming Visual Basic 2005 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.