6.0 Introduction


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.

Which Is Better: Control or WebControl?

The Control class in the System.Web.UI namespace is the base class for all server controls. It provides the properties, methods, and events shared by all web controls. The WebControl class in the System.Web.UI.WebControls namespace derives from the Control class and adds style properties such as Font, Forecolor, and Backcolor. In addition, it provides skin and theme features.

Microsoft recommends deriving from the Control class if your custom control contains no user interface elements. But if your custom control provides a user interface, you should derive from the WebControl class.




ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

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