ProblemYou want to create a custom control that combines two or more HTML controls. SolutionUse the .NET language of your choice to:
To use the custom control in an ASP.NET page:
Figure 6-1 shows the output of a custom control that combines a label and text box. Examples 6-1 and 6-2 show the VB and C# class files for the custom control. Example 6-3 shows how to use the custom control in an ASP.NET page. Figure 6-1. Basic custom control outputDiscussionTo create a custom control that combines the functionality of two or more HTML controls, you first create a class that inherits from the WebControl class in System.Web.UI.WebControls. The only method of WebControl required to output HTML is the Render method. Render is responsible for writing the HTML that will be rendered by the browser. To enhance your ability to write well-formed HTML, you can use other methods of the HtmlTextWriter class along with the HtmlTextWriterAttribute and HtmlTextWriterTag enumerations. We'll talk more about this in a minute, but for now we'll stick with writing our own unvarnished HTML. The custom control we have implemented in our example contains a label and an input control. The label and input control are output in the Render method with the following code: writer.Write("Enter Age: ") writer.Write("<input type='text' size='3' />") writer.Write("Enter Age: "); writer.Write("<input type='text' size='3' />"); To use the custom control, the assembly containing the control must be registered in the target .aspx file. The TagPrefix attribute defines an alias to use for the namespace in the page. The Namespace attribute must be set to the fully qualified namespace of the control. Here is how you register the assembly in our example: <%@ Register TagPrefix="ASPCookbook" Namespace="ASPNetCookbook.VBExamples"; %>
The custom control can be placed anywhere on the page by inserting a tag. The control to insert is identified by naming the tag with the TagPrefix followed by the class name. The tag must include the id and runat="server" attributes for the control to be rendered on the page. This is the tag used in our example: <ASPCookbook:CH06QuickAndDirtyCustomControlVB1 runat="server" /> In our example, raw HTML is written to the web page in the Render method. For simple HTML, this works well. As the complexity of the HTML you write increases, however, the likelihood that you'll introduce errors increases. Fortunately, HtmlTextWriter includes methods that simplify the generation of complex HTML. These methods can help you with the nuances of adding HTML attributes (and values) to an HTMLTextWriter output stream, writing beginning and ending tags, flushing buffers so all buffered data is written to the text stream, etc. To create the input box in our example, you could use the HtmlTextWriter like this: Protected Overrides Sub Render(ByVal writer As HtmlTextWriter) 'output label writer.Write("Enter Age: ") 'output input control writer.AddAttribute(HtmlTextWriterAttribute.Type, _ "text") writer.AddAttribute(HtmlTextWriterAttribute.Size, _ "3") writer.RenderBeginTag(HtmlTextWriterTag.Input) writer.RenderEndTag() End Sub 'Render protected override void Render(HtmlTextWriter writer) { //output label writer.Write("Enter Age: "); //output input control writer.AddAttribute(HtmlTextWriterAttribute.Type, "text"); writer.AddAttribute(HtmlTextWriterAttribute.Size, "3"); writer.RenderBeginTag(HtmlTextWriterTag.Input); writer.RenderEndTag(); } // Render One advantage of implementing the Render method in this way is that you can use the RenderBeginTag and RenderEndTag methods to output HTML and sidestep having to insert the <, /, and > characters yourself. In addition, using the HtmlTextWriterTag and HtmlTextWriterAttribute enumerations ensures all tags and attributes are correctly spelled. Another advantage is that you can avoid the hassle of ensuring the single and double quotes are handled correctly. Notice in the first example that the values for the attributes of the input tag were output with single quotes, since double quotes mark the beginning and end of strings. Outputting double quotes around the values would have required more complex code with string concatenations. Using the AddAttribute method avoids this problem completely.
A useful enhancement to this approach would be to add HTML-style attributes to the control to make the control more adaptable and reusable throughout your applications. See Recipe 6.2 for how to do this.
See AlsoRecipe 6.2; for additional details on Control, Render, HtmlTextWriterTag, HtmlText-WriterAttribute, and especially HtmlTextWriter, search the MSDN Library Example 6-1. Quick-and-dirty custom control (.vb)
Example 6-2. Quick-and-dirty custom control (.cs)
Example 6-3. Using the quick-and-dirty custom control
|