I l @ ve RuBoard |
As usual, we'll start with the obligatory Hello World example. In this case we're going to create a custom control that does nothing more than output the text "Hello World". We will see shortly how we can do this by inheriting from existing controls, such as the Label , but for now, we will simply inherit from the base class Control . The source code for our control is listed in Listing 12.1. Listing 12.1 A C# Hello World Control, HelloWorld.csnamespace ASPNETByExample { public class HelloWorld : System.Web.UI.Control { protected override void Render(System.Web.UI.HtmlTextWriter htwOutput) { htwOutput.Write("Hello World"); } } } Examining this code line-by-line , we see first that we have declared a namespace for our control, called ASPNETByExample . Within this namespace, we declare our class, HelloWorld , which inherits from the System.Web.UI.Control class. This allows us to use all the methods and properties that the Control class makes available to us, one of which is the Render() method. Since we want to control the output of our control ourselves , we override the Render() method with our own declaration, which we define with the same parameters as the Control.Render() method. In this case, the method takes a single parameter, a reference to an HtmlTextWriter object, called htwOutput . This object is used to output HTML to the Response stream, which in turn is sent to the user 's browser. The method that we're most concerned with is the Write() method, which we have used here to output the text "Hello World".
Having written our control, we must now compile it into an assembly, copy the assembly to the /bin folder of our Web application, reference the assembly on one of our ASP.NET pages, and finally place an element on the page where we want our control rendered. That's a seemingly daunting amount of work, especially compared to the amount of effort needed to access a user control to do the same thing. Let's take a look at what's involved, first with the command line and then with Visual Studio.NET. Command Line Building and DeploymentTo build our control, we will use the csc command ( vbc if you are using the .vb file from the Web site). These programs can be found in the %SystemRoot%\Microsoft.NET\Framework\v1.0.2914 directory (note that for the release of .NET, the v1.0.2914 will probably change). The %SystemRoot% variable is just the drive letter that your operating system is installed on, and will usually be C. So, assuming that you've found the csc.exe (vbc.exe) file, you can compile the HelloWorld.cs file into an assembly called HelloWorld.dll with the following command: csc /t:library /out:HelloWorld.dll HelloWorld.cs This results in the file, HelloWorld.dll, being created in the same directory as our HelloWorld.cs source file. To use this assembly in an ASP.NET application, we need to copy it to the Web application's folder on the hard drive, and place it in a folder called bin within the Web's folder. Visual Studio.NET Building and DeploymentTo build the control using Visual Studio.NET, we need to perform the following steps. First, we must create a new project for this control.
---------------------- Done ---------------------- Build: 1 succeeded, 0 failed, 0 skipped Your assembly will be in your project's bin folder, and by default should be named HelloWorld.dll. To use this assembly in your ASP.NET application, you will need to copy it to the Web's bin folder. Referencing the Control from ASP.NETNow that you've built your control and copied it to your Web application's bin folder, you're ready to access it from an ASP.NET page. To do this, we need to add a directive to the top of the page similar to how we set up a page to use a user control. For this control, we will use the following page directive: <%@ Register TagPrefix="Hello" Namespace="ASPNETByExample" Assembly="HelloWorld" %> This directive tells the ASP.NET compiler that, whenever it sees a control with a tag prefix of "Hello", it should look for the corresponding control in the ASPNETByExample namespace found in the HelloWorld assembly. Note that you do not need to have a separate Register tag for every control you want to use on a page, unless they are all in different assemblies and namespaces. You could put a whole suite of control in a single namespace and assembly, and have access to all these controls using just one Register directive. The complete source code for our ASP.NET page is shown in Listing 12.2 below. Listing 12.2 HelloControl.aspx<%@ Register TagPrefix="Hello" Namespace="ASPNETByExample" Assembly="HelloWorld" %> <html> <body> <Hello:HelloWorld runat="server" /> </body> </html> That's it. Note that, as with all ASP.NET controls, you need to be sure that you add the Runat="server" tag to the control's declaration. When you view this page, you'll see a blank Web page with just the words "Hello World". You can see this example live at this book's supporting Web site, found at: http://aspauthors.com/aspnetbyexample/ch12/ |
I l @ ve RuBoard |