| Advanced ASP.NET Server-side Controls | |
| George Shepherd | |
n my October 2000 column I looked at the motivation for the new Active Server Pages.NET (ASP.NET) server-side controls and their basic structure. In this month's column I'll take a closer look at ASP.NET and a deeper look at ASP.NET server-side controls, including control state management, round-trips and posting data, and developing a richer UI using both compositional and noncompositional controls. ASP.NET Of course, generating raw UI from the server is basically how ASP works now. An ASP page generates HTML dynamically based upon various runtime factors on the server side. ASP has an internal object model that's accessible as a set of IDispatch pointers. To get some HTML out to the client, you just grab the ASP Response object (available through an object's context) and call IResponse::Write. The string you hand to the Write method is the one that shows up at the client's browser. Composing from other Controls This month I'll use a simple Fahrenheit/Centigrade conversion control as an example of a compositional control, or a server-side control composed of other standard controls (like listboxes and radio buttons). ASP.NET will include a number of lightweight controls like this to make your life easier. In my control, the temperature converter control gathers together two textboxes and two push buttons into a single component. Type a number into the Fahrenheit box, click on the Fahrenheit To Centigrade button, and the control converts Fahrenheit temperatures to Centigrade temperatures. Type a number into the Centigrade box, click on the Centigrade To Fahrenheit button, and the control converts Centigrade temperatures to Fahrenheit temperatures. Figure 1 shows the control in action within Microsoft Internet Explorer 5.5. Properties The idea behind a control is usually to provide a user interface for managing a small piece of data, normally represented as a property within the control. This control maintains two propertiesâ€"the actual Fahrenheit temperature and the Centigrade temperatureâ€"represented as doubles. C# promotes using accessor and mutator functions for managing a control's properties. Rather than simply listing a member variable as a type, like this:
C# provides a useful syntax, letting you hook functionality into the process of assigning and retrieving values. For example, the code in Figure 3 shows the C# syntax for managing a property through accessor and mutator functions. Child Controls The server-side control I described in my October 2000 column didn't do muchâ€"it just squirted a single line of HTML out to the browser. To make a control with any functionality at all usually means composing a control from other elements (such as other standard controls). The ASP.NET server-side control framework provides this functionality in two ways. You may either compose a control from separate subcontrols by creating them at page load-time or you can have the ASP.NET page render the controls out as HTML. Now let's look at composing server-side controls by creating child controls at runtime. Responding to Button Presses So far, adding state to the control and having the main control create child controls gets you pretty far. If you request an ASP.NET page using this control, you'll see pretty much the same thing as the dialog in Figure 1. However, when the user presses one of the conversion buttons, you want the control to respond by performing the requested version. So you need to tack handlers onto the push buttons.
When you convert from Fahrenheit to Centigrade, simply accessing the FahrenheitValue property within the call to F2C gets the data out of the Fahrenheit textbox. Assigning a value to the CentigradeValue property puts the Centigrade property value into the Centigrade textbox. Using the Control on the Page Once the control is compiled into an assembly, you can use it within ASP.NET. Figure 4 shows some ASP.NET code that declares an instance of the temperature converter. When the ASP.NET framework loads this page, ASP.NET writes out the body, center, and font tags. Then the form tag manages the input controls that will be emitted by the server-side control. For the complete picture, look at the HTML emitted by the server-side control (see Figure 5). HTML Produced by the Server-side Control The basic job of an ASP.NET server-side control is to emit some markup language that the browser can deal with. Notice in Figure 5 that in the place of the server-side control declaration are two labels, two edit controls with numerical values in them, and two push buttons to submit data entered on the form to the server. The beauty of the server-side control architecture is that you don't have to code up those lines. Anytime you need a temperature converter control, you can just drop in the temperature converter server-side control shown in Figure 2. Conclusion The ASP.NET server-side control architecture promises to dramatically speed up Web site creation and management. It's very similar to when developers went from the old days of having to program everything in a Windows-based application by using C and the SDK to using MFC. MFC introduced a framework over the collective programming details known as the Windows SDK. Rather than having to code every Window class and set up every WndProc by hand to get an application working, MFC does a lot of the preliminary work for you. In the same way, rather than having to write out every little line of HTML to make a Web page work, you can collapse functionality into an ASP.NET server-side control and then simply use them wherever you need to. This month I looked at creating a server-side control composed of child controls. In a subsequent column I'll look at writing a noncompositional server-side controlâ€"or one that manages child controls by rendering them out to the client as HTML. |
|
| George Shepherd is an Associate Director at Plural where he helps companies use Microsoft technologies effectively. In addition, George delivers seminars with DevelopMentor and is the coauthor of Programming Visual C++ (Microsoft Press, 1998) and MFC Internals (Addison-Wesley, 1996). |
From the January 2001 issue of MSDN Magazine
.gif)