Creating a custom server control using Visual Studio .NET

324

6.11.2 Server Controls

ASP.NET provides the ability to develop custom server controls also known as web controls. A server control is a class that inherits from the WebControl class in the System.Web.UI.WebControls namespace and overrides the Render method. This section describes how to create a server control in the Visual Studio .NET IDE, as well as manually.

6.11.2.1 Creating a custom server control using Visual Studio .NET

To create a custom server control in Visual Studio .NET: 1. Select File New Project. The New Project dialog box appears, as previously shown in Figur e 6- 1 . 2. Select Visual Basic Projects in the Project Types pane on the left side of the dialog box. 3. Select Web Control Library in the Templates pane on the right side of the dialog box. 4. Enter a name in the Name text box. 5. Enter a location in the Location text box. 6. Click OK. Visual Studio .NET creates a project with some boilerplate code that implements a server control. The code generated by Visual Studio .NET is shown in Ex am ple 6 - 20 . Example 6-20. Code generated by Visual Studio .NET for a new web- control project Imports System.ComponentModel Imports System.Web.UI DefaultPropertyText, _ ToolboxData{0}:WebCustomControl1 runat=server{0}:WebCustomControl1 _ Public Class WebCustomControl1 Inherits System.Web.UI.WebControls.WebControl Dim _text As String BindableTrue, CategoryAppearance, DefaultValue _ Property [Text] As String Get Return _text End Get SetByVal Value As String _text = Value End Set End Property Protected Overrides Sub Render _ ByVal output As System.Web.UI.HtmlTextWriter _ output.Write[Text] End Sub End Class Note the following about the code in Ex am ple 6- 20 : • It defines a class that inherits from the WebControl class defined in the System.Web.UI.WebControls namespace. 325 • The class has two custom attributes associated with it: DefaultProperty defined in the System.ComponentModel namespace and ToolboxData defined in the System.Web.UI namespace. These attributes are not required, but they provide useful information to the Visual Studio .NET Web Forms Designer when the control is referenced in a web-form project. • The class has a property called Text. This is not a requirement for web controls; it is just a design pattern used by Visual Studio .NET. • The Text property has three custom attributes: Bindable , Category , and DefaultValue all defined in the System.ComponentModel namespace. Again, these custom attributes are not required, but they provide information to the Visual Studio .NET Web Forms Designer when the control is referenced in a web-form project. • The class defines an override for the Render method originally declared in the WebControl class. The ASP.NET framework calls this method to ask the control to render itself into HTML. It is up to the control developer you to render output that appropriately represents the control. The boilerplate code simply outputs the value of the Text property.

6.11.2.2 Creating a custom server control in code