Chapter 16: User Controls and Custom Controls

 
Chapter 16 - User Controls and Custom Controls
bySimon Robinsonet al.
Wrox Press 2002
  

Overview

It has often been the case with web development that the tools available, however powerful, don't quite match up with your requirements for a specific project. Perhaps a given control doesn't quite work as you'd like it to, or perhaps one section of code, intended for reuse on several pages, would be unworkably complex in the hands of multiple developers. In cases such as these there is a strong argument for building your own controls. Such controls can, at their simplest, wrap multiple existing controls together, perhaps with additional properties specifying layout. They can also be completely unlike any existing control. Using a control you have built yourself can be as simple as using any other control in ASP.NET (if you have written them well), which can certainly ease web site coding.

In the past it has been tricky to implement such custom-built controls, especially on large-scale systems where complex registration procedures may be required in order to use them. Even on simple systems, the coding required to create a custom control could become a very involved process. The scripting capabilities of older web languages also suffered by not giving the perfect access to your cunningly crafted object models, and resulted in poor performance all round.

The .NET Framework provides an ideal setting for the creation of custom controls, using simple programming techniques. Every aspect of ASP.NET server controls is exposed for you to customize should you wish, including such capabilities as templating, client-side scripting, and so on. However, there is no need to write code for all of these eventualities if you don't want or need to; simpler controls can be a lot easier to create.

In addition, the dynamic discovery of assemblies that is inherent in a .NET system makes installation of web applications on a new web server as simple as copying the directory structure containing your code. To make use of the controls you have created you simply copy the assemblies containing those controls along with the rest of the code. You can even place frequently used controls in an assembly located in the global assembly cache (GAC) on the web server, such that all web applications on the server have access to them.

In this chapter we will look at two different kinds of controls:

  • User controls  converting existing ASP.NET pages into controls

  • Custom controls  grouping the functionality of several controls, extending existing controls, and creating new controls from scratch

We'll illustrate user controls by creating a simple control that displays a card suit (club, diamond, heart, or spade), so that we can embed it in other ASP.NET pages with ease. In the case of custom controls, we'll create a straw poll control allowing the user to vote for a candidate in a list and see how the vote is progressing.

As is implied by the above discussion, custom control building is a large and at times complicated topic. For a more complete treatment please refer to Professional ASP.NET Server Controls (ISBN: 1-861005-64-4), also published by Wrox Press.

  


Professional C#. 2nd Edition
Performance Consulting: A Practical Guide for HR and Learning Professionals
ISBN: 1576754359
EAN: 2147483647
Year: 2002
Pages: 244

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