Creating a custom server control in code Using a custom server control in Visual Studio .NET

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

To create a custom server control without the benefit of Visual Studio .NET, derive a class from the WebControl class and override the Render method. For good measure, add the ToolboxData custom attribute to the class, as exemplified by the Visual Studio .NET-generated code in Ex am ple 6- 20 . An example is shown in Ex am ple 6- 21 . Example 6-21. A web control designed without Visual Studio .NET Imports System.ComponentModel Imports System.Web.UI Namespace OReilly.VBNET ToolboxData{0}:HelloWebControl runat=server{0}:HelloWebControl _ Public Class HelloWebControl Inherits System.Web.UI.WebControls.WebControl Protected Overrides Sub Render _ ByVal output As System.Web.UI.HtmlTextWriter _ output.Writeihello, worldi End Sub End Class End Namespace The control in Ex am ple 6- 21 renders the hard-coded string ihello, worldi within the page on which it is placed. Placing custom controls on web forms is described shortly. To compile the code in Ex am ple 6- 21 , save it to a file named HelloWebControl.vb, and execute the following command from the command line: vbc.exe HelloWebControl.vb r:System.dll,System.Web.dll t:library This creates the file HelloWebControl.dll, which can then be referenced from a web project, as explained next.

6.11.2.3 Using a custom server control in Visual Studio .NET

To add a custom server control to the Visual Studio .NET Toolbox, perform the following steps: 326 1. Right-click on the Toolbox in Visual Studio .NET and select Customize Toolbox. The Customize Toolbox dialog box appears, as shown in Figur e 6- 17 . If the Toolbox isnt visible, select View Toolbox from the Visual Studio .NET main menu. Figure 6-17. The Customize Toolbox dialog box 2. Click on the .NET Framework Components tab. 3. Click on the Browse button, browse to your custom server controls compiled .dll, and click the Open button. The controls in the .dll are added to the list view, as shown in Figur e 6- 18 . In this case there is only one control in the .dll. Note that the checkbox next to the control is not yet checked. Figure 6-18. The control has been added to the Customize Toolbox dialog box 327 4. Click the checkbox next to the control, then click OK. After the control has been added to the Toolbox, it can be dropped onto a web form and manipulated in the same way as the built-in server controls. Ex am ple 6- 22 displays the HTML view of a new web page with an instance of the control from Ex am ple 6- 21 placed on it. The lines related to the control are shown in bold, but the entire HTML view is shown for context. Example 6-22. The HTML view generated by Visual Studio .NET when a custom control is added to a web form Page Language=vb AutoEventWireup=false Codebehind=WebForm1.aspx.vb Inherits=WebApplication1.WebForm1 Register TagPrefix=cc1 Namespace=OReilly.VBNET Assembly=HelloWebControl DOCTYPE HTML PUBLIC -W3CDTD HTML 4.0 TransitionalEN HTML HEAD titletitle meta name=GENERATOR content=Microsoft Visual Studio .NET 7.0 meta name=CODE_LANGUAGE content=Visual Basic 7.0 meta name=vs_defaultClientScript content=JavaScript meta name=vs_targetSchema content=http:schemas.microsoft.comintellisenseie5 HEAD body MS_POSITIONING=GridLayout form id=Form1 method=post runat=server cc1:HelloWebControl id=HelloWebControl1 style=Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px runat=servercc1:HelloWebControl form body HTML The code-behind file that goes with Ex am ple 6- 22 has the following line added to it: Protected WithEvents HelloWebControl1 As OReilly.VBNET.HelloWebControl

6.11.2.4 Using a custom server control manually