first, the introduction
So far, you've probably learned a lot about the ASP.net 2.0 new features-master pages, themes, providers, and so on ... All of this is quite exciting, but have you learned any significant changes in the development of custom Web controls? That's what I want to discuss in this article. If you are already engaged in control development, then I think the new features in ASP.net 2.0 described in this article will be applied immediately to your control development.
The first thing you should be aware of is that all Web controls you have previously developed with ASP.net 1.1 (or 1.0) will continue to operate well under version 2.0-Microsoft has not corrupted your existing code. In this article, I'll introduce you to all the relevant content, including many new exciting technologies, all of which you can add to existing controls or use in a new control environment.
Author note This article assumes that you already have a basic understanding of custom Web control development. In this article, I take an enhanced version of the Emailcontact control as an example to make a comprehensive discussion of the Web control improvement techniques in asp.net 2.0.
Table 1 describes most of the significant improvements that ASP.net 2.0 has made in customizing Web control development. In this series of articles, I'll take a discussion of these features.
Table 1:asp.net 2.0 Web controls improve functionality.
Iii. Enhanced Emailcontact Web Controls
|The new base class
||ASP.net 2.0 also introduces some new base classes for you to derive your own controls. Later, I would highlight a control-compositecontrol.
||This is the "Diqin" of the viewstate we are familiar with. There is no longer a need for a dedicated approach to state management in ControlState, and on the other hand, when a control user closes a state management mechanism on a site (or page), it helps you avoid errors.
||This is a new feature that makes Windows control developers quite exciting! In this article, I'll show you how to add this functionality to your own custom controls.
|Template design-time editing
||In version 1.1, it is quite difficult to add design-time editing capabilities to a control template. Fortunately, ASP.net 2.0 makes it fairly easy to add this feature. I'll discuss this in the last installment of this series.
||It's also a huge improvement! You can select an established format from a list of predefined formats when you use the DataGrid control. ASP.net 2.0 makes it easy to add this functionality to your custom controls.
||This is an improvement I like very much because I always develop some of the controls associated with images. In asp.net 2.0, you can now publish a control's DLL without having to think about anything else. In other words, you can now embed images and other resources into the control's DLL file.
The custom Emailcontact Web controls in this article (refer to Figure 1) allow you to add a "contact us" form to your site that has full e-mail capabilities. In this article, I will use this feature to enhance this control.
四、一个 the new base class
Figure 1. emailcontact Control in default state
Previously, developers derived their visual Web controls from the WebControl class. The reason I use visualization here is because, typically, controls that do not generate anything in the browser are derived from the control class. This has not changed-you should continue to use the control class to derive any such non-visual controls-they perform invisible functions or generate any other content in the browser other than the visual HTML content. Also, when you are developing a Visual Web control, you should continue to use the WebControl class. However, most of the composite controls that we develop are designed to take advantage of the functionality of existing controls. In this case, you should always derive from the WebControl class, but you should also remember some other details-otherwise, it could lead to many problems.
The composite control must implement the INamingContainer interface, and it needs to be included in your control class. This interface ensures that you have unique tag names in all HTML tags in your control and the entire control hierarchy that it can generate. This is quite critical when you have multiple composite controls of the same type on a single page. In such cases, you need to make sure that any generated child element has a unique name. Forgetting to implement this interface can lead to a variety of problems.
Before ASP.net 2.0, the composite control developer also needed to remember to invoke EnsureChildControls in the Render method of a control. In my previous article, I have explained how to overload the Render method and call this method before calling the render method of the base class. It is necessary to make the control correctly build at the Visual Studio design time, otherwise it can cause a lot of inconvenience.
The above two steps are so common in composite control development that many developers tend to build a base class that includes both of these details, and then derive all of their new composite controls from the base class. As a substitute, ASP.net 2.0 provided (or, more precisely, "name") CompositeControl. By using this class to build your composite control, you don't have to remember to implement INamingContainer or execute a ensurechildcontrols call from the Render method.
In addition, there are some other new base classes, such as controls for data binding, and so on, no longer repeat.