Simple Custom Control

Let's start with a simple custom control. This control will display a line of text like a label but will center the text and make it boldface. I named this custom control CenteredLabel. You must make a couple of design decisions when you decide to create a custom control. The first, and probably most important, is the decision of what class to use as your base class. Your new custom control will inherit the base behaviors and attributes of whatever base class you use. In this example, the choice is simple: System.Web.UI.WebControls.Label.

Note MSDN documentation lists multiple pages of properties, methods, and events for System.Web.UI.WebControls.Label. All but one of these properties, methods, and events are inherited from the WebControl or Control classes. The single exception is the Text property. Inheriting from Label means that all of its properties, methods, and events are available in our new class. The next decision is what additional properties, methods, and events need to be exposed. In this example, there's no need to add any properties, methods, or events. The only property we'll use is the Text property inherited from Label. The CenteredLabel source (written in Visual Basic .NET) is shown in Listing 6-5.

0 0

Post a comment