Creating a Simple Control


Let's start with a simple control: HelloWorld . The HelloWorld control displays the text "Hello World!" in a page. The code for this control is included in Listing 28.1.

NOTE

The CD that accompanies this book includes two new ASP.NET controls that you can use in your applications: the SuperDataGrid control and the SuperContentRotator control. The source code for these controls is included on the CD in both C# and VB.NET versions.

The SuperDataGrid control enables you to automatically sort , page, and edit database data. The SuperContentRotator control enables you to randomly display 1 or more items from a data source such as a database table or XML file. Both of these controls can be found in the SuperexpertControls folder on the CD.


Listing 28.1 HelloWorld.vb
 Imports System Imports System.Web Imports System.Web.UI Namespace myControls Public Class HelloWorld: Inherits Control   Protected Overrides Sub Render( objTextWriter As HtmlTextWriter )     objTextWriter.Write( "Hello World!" )   End Sub End Class End Namespace 

The C# version of this code can be found on the CD-ROM.

Listing 28.1 contains the source code for a Visual Basic class named HelloWorld . What makes the class a control is the fact that it inherits from the Control class.

You inherit the Control class by using the Inherits statement. In particular, you inherit from the System.Web.UI Control class. This means that you can take advantage of all the properties, methods , and events of the base Control class in this control.

In Listing 28.1, you override the Render method of the Control class. When an ASP.NET page renders content, the page calls the Render method for each control on the page. So, any content that you output by using the Render method is rendered when the page itself is rendered.

The Render method has one parameter: an instance of the HtmlTextWriter class. By calling the Write method of the HtmlTextWriter class, you can display content. In this example, the Write method displays the text "Hello World!" .

At the top of Listing 28.1, you import three namespaces: System , System.Web , and System.Web.UI . In general, it's a good idea to always import at least these three namespaces when building a control. These namespaces contain the basic classes used by controls, such as the base data types and the base Control and WebControl classes.

Finally, notice that you create a namespace named myControls for this control. You can create a namespace with any name that you wish. You need to refer to the namespace when you instantiate the control in an ASP.NET page.

Before you can use the HelloWorld control contained in Listing 28.1, you first need to compile it by executing the following statement from the command line:

 
 vbc /t:library /r:System.dll,System.Web.dll HelloWorld.vb 

NOTE

If you install Visual Studio .NET, then you cannot execute the vbc compiler from the default command prompt (the proper path environmental variable is not set). Instead, you need to execute the compiler by opening the Visual Studio .NET Command Prompt located at Start, Programs, Microsoft Visual Studio .NET, Visual Studio .NET Tools.


This statement compiles the control by using the Visual Basic command-line compiler. Two assemblies are referenced by the control when it's compiled: System.dll and System.Web.dll . If everything works correctly, the statement should produce a new file named HelloWorld.dll . This file is the compiled control.

Before you can use the compiled HelloWorld control, you need to copy the HelloWorld.dll to the /bin directory of your ASP.NET application. It needs to be located in this special directory so that ASP.NET pages can find it. If the /bin directory does not exist, you can create it as a subdirectory of the root directory of your ASP.NET application.

The page in Listing 28.2 illustrates how you can use the HelloWorld control in an ASP.NET page.

Listing 28.2 DisplayHelloWorld.aspx
 <%@ Register TagPrefix="myControls" Namespace="myControls" Assembly="HelloWorld"%> <html> <head><title>DisplayHelloWorld.aspx</title></head> <body> <myControls:HelloWorld   Runat="Server" /> </body> </html> 

The C# version of this code can be found on the CD-ROM.

The first line in Listing 28.2 contains a Register page directive used to associate the tag prefix myControls with the namespace myControls . By default, an ASP.NET page recognizes the ASP TagPrefix for the System.Web.UI.WebControls namespace. When you add the Register directive, the page also recognizes the myControls tag prefix.

The HelloWorld control is instantiated in the body of the ASP.NET page. When the page in Listing 28.2 is rendered, the HelloWorld control's Render method is called, and the text "Hello World!" is displayed (see Figure 28.1).

Figure 28.1. Displaying the HelloWorld control.

graphics/28fig01.jpg



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net