Custom controls are compiled controls that function like ASP.NET's own server controls. Like user controls, custom controls can enhance the reusability of code repeated within a project or over multiple projects. There are important differences, however. In general, user controls are like "mini" web pages in that they contain part of an ASP.NET page. Additionally, they are compiled when first requested, but their user interface can easily be changed as required. With custom controls, on the other hand, the user interface is generated by the code and cannot easily be changed except through properties and methods that you implement, about which we'll speak more in a minute.
In a broader sense, a custom control is any control you create with these common themes: it is typically derived from the Control or WebControl class in the System.Web.UI namespace or an existing ASP.NET server control. It generally provides its own user interface, and it may provide its own backend functionality through the methods, properties, and events that you implement for it.
Custom controls range from the simple to the complex. A simple custom control might, for example, write some HTML, perhaps modifying its HTML-style attributes as it does so. A more complex custom control would offer HTML-style attributes of its own through properties you implement. A custom text box control could, for example, offer one attribute for controlling the color of its label and another to control the width of the control itself. To make it more complex and more useful, the control would have to handle postback events like the server controls provided with ASP.NET. Still more complex would be a custom control that, like the DataGrid control, includes templates and supports data binding. Because custom controls can be created from scratch or inherited from existing controls, the possibilities are endless.
All of the hypothetical custom controls we've described are, with the exception of the templated control, illustrated in this chapter's recipes. (If you're interested in learning more about templated controls, see the example in the ASP.NET QuickStart Tutorials that are part of the .NET Framework QuickStarts that ship with Visual Studio or are available via http://www.gotdotnet.com.)
This chapter introduces you to some of the techniques used to build custom controls. In sticking to the basics, we implicitly recognize that custom controls are "custom" and, therefore, highly individual. However, these basics ought to take you a long way in crafting your own custom controls.