5.1. Combining HTML Controls in a Single Custom Control
Problem
You want to create a custom control that combines two or more HTML controls.
Solution
Use the .NET language of your choice to:
Create a class that inherits from the
Controlclass in theSystem.Web.UInamespace.Override the
Rendermethod to have it output the HTML controls you wish to include.(Optional) Use the
HtmlTextWriterclass to enhance your chances of writing well-formed HTML.
To use the custom control in an ASP.NET page:
Register the assembly containing the control.
Insert the tag for the custom control anywhere in the page.
Figure 5-1 shows the output of a custom control that combines a label and text box. Example 5-1 and Example 5-2 show the VB and C# class files for the custom control. Example 5-3 shows how to use the custom control in an ASP.NET page.

Figure 5-1. Basic custom control output
Discussion
To create a custom control that combines the functionality of two or
more HTML controls, you first create a class that inherits from the
Control class in System.Web.UI.
The Control class is the base class that all
ASP.NET server controls inherit from. It provides all of the basic
properties, methods, and events necessary to build custom controls.
The only method of Control that is required to
output HTML is
the Render method.
Render is responsible for writing the HTML that will be rendered by the browser. To enhance ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access