전체 페이지뷰

2013년 9월 3일 화요일

Composite Controls

Custom composite controls

Composite controls can contain an arbitrary collection of controls (as many children as memory will accommodate), and the controls can be nested as deeply as necessary. Of course, there’s a practical limit to the number and depth of the child controls. Adding too many controls or nesting them too deeply adds complexity to a page, and it can become unsightly. In addition, adding too many nested controls can greatly inhibit the performance of the application—it takes time to walk the control collection and have each one render.

User controls

User controls are composite controls that contain child controls very much like binary composite controls do. However, instead of deriving from System.Web.UI.CompositeControl, they derive from System.Web.UI.UserControl. Perhaps a better description is that they’re very much like miniature Web Forms. They have a UI component (an .ascx file) that works with the Visual Studio Designer, and they employ a matching class to manage the execution.

When to Use Each Type of Control

Binary composite controls and User controls are similar, so there seems to be some redundancy in the framework. Because User controls have such an affinity for the Designer, perhaps it seems you don’t need custom composite controls at all. However, each style of composite control has distinct advantages and disadvantages.

The major advantage of binary composite controls is that they are deployed as individual assemblies. Because binary composite controls are packaged in distinct assemblies, you can sign them and deploy them across the enterprise. You also can install them in the global assembly cache. Signing and deploying global assemblies is an advanced topic—but I mention it here because this is one of the main reasons to choose a binary control over a User control. The primary downside to using binary composite controls is that they require you to pay more attention to detail in the coding process (there’s no Designer support as you write them because they are created entirely from code).

The primary advantage of User controls is that they do include Designer support, which makes them very easy to design visually. However, User controls have a downside in their deployment: They go with the project in which they were created, and they are deployed that way. You can include them as part of other projects, but that requires copying the .ascx and the .cs files to the new project. They are not deployed as signed, secure assemblies.

댓글 없음:

댓글 쓰기