User Controls


User Controls

Although we have not explicitly discussed creating user controls in this chapter (see Chapter 4 for more on user controls), all of the features shown can be pretty much applied to them.

A key benefit of user controls is that complex sections of the UI can be declared using only control declarations. In a server control, you'd have to write code to achieve this. Although not difficult, this can be time consumingchanging or maintaining it can take longerand also is more error-prone .

Why Create Your Own Server Controls?

"Why create your own controls?" isn't really the right question to ask. Instead, the decision you have to make as an ASP.NET developer is whether you let ASP.NET build server controls automatically for you (by just building your sites using ASP.NET pages and/or user controls), or whether you take over some of the control creation yourself. In the latter case, you can build your site using a mixture of ASP.NET pages, user controls, and custom server controls.

Thus, there's no right answer to the original question. However, a few pointers should help you decide for yourself:

  • ASP.NET controls enable a fine-grained level of black-box reuse. ASP.NET user controls and pages can also provide this reuse, but since they are far more coarse-grained and typically have fixed UI traits (pages don't support templates, although user controls can), they are likely to provide far less reuse.

  • ASP.NET controls ultimately provide the most flexibility, but take longer to write and require more coding skill.

  • If you want a custom control, but are not 100% sure, start with a user control; you can always change it to become a custom control later. All the code you write will work as a control, but you will need to convert any HTML sections of your user control into code using control composition.

  • Only custom controls and user controls can be lookless , and therefore support templates. If you want people to be able to extend and manage your control's UI, use these control types.

  • User controls and custom controls can be written in different languages, and then used within the same hosting page. At present, only one language can be used directly within any single ASP.NET page.

  • Only custom controls are compiled. You may therefore want to use them if you need to protect your sourcecode.




Professional ASP. NET 1.1
Professional ASP.NET MVC 1.0 (Wrox Programmer to Programmer)
ISBN: 0470384611
EAN: 2147483647
Year: 2006
Pages: 243

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