Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Web Control Templates Explained : Page 6

Programming for extensibility assures ease of maintainability in your design. Control templates offer this functionality to custom Web controls.


advertisement
More Template Functionality
A control developer can insert templates anywhere in a custom control. I could have chosen to give this control some templates in between the form fields or just about anywhere I wanted. As you noticed, the template container control gets positioned throughout the composite control just like any other child control. You can also use control templates in repeater controls, or controls whose content is built dynamically within loop iterations. You can repeat templates in each iteration, just like you see in a grid. See the side bars for a good source of reference where you can learn more about this. Template functionality incorporated into controls that are built through loop iteration is also a good candidate for data binding. Once again, the grid is a good example of this. Earlier in the article, I mentioned that I like to use a custom control as my template container even though something like a Panel would work just fine. The reasons for this are that I may want to put some properties in my container control. This can come in handy if I'm creating the template container control inside a loop in the hosting control for repeating purposes. I might set a property within the container control to an iteration number or something along those lines. Later, if I access the template container from the page using FindControl, I can access these properties. Even though I may not have this requirement immediately, it's a good idea to set yourself up for the future.

The new data controls from Microsoft—GridView, DetailView, and FormView—all incorporate template functionality as you would expect them too; but they're not the only ones now. ASP.NET's Security control suite also has template functionality. These controls not only allow you to add other controls to their templates, but they also give you the ability to turn their predefined contents into templated contents. This let's the page developer completely alter the way the control is going to render. The SiteMapPath control (or breadcrumb control as it is also known) also has template functionality allowing you (but not limiting you) to change, for example, the separator that gets displayed between Web pages.

Necessary Workarounds
Unfortunately these controls are also examples of inconsistency even within Microsoft. The ItemCommand event I discussed earlier is not present in some of these controls so any links or buttons you add into their templates will not easily be trapped. I say "not easily" because it's not impossible. If you encounter controls with template functionality but no event to expose for bubbling (ItemCommand is the usual standard), you can manually wire an event to the controls you insert into a template. I'll demonstrate this using the EmailContact control, so for the time being, try to suspend disbelief and imagine the control written in a less than perfect state (no ItemCommand event). Remember the link I dropped into the FooterTemplate? I'm going to put the EmailContact control into template-editing mode by right-clicking on it. I'll select the Edit Templates menu and choose Surrounding Templates. Figure 3 shows the results.



Now if I click the link I can view its properties in the property browser. In a perfect design, I would fill the CommandName and/or CommandArgument properties here so they are exposed through the control's ItemCommand event; but I'm pretending that event does not exist. I can click the lightning bolt icon at the top of the property browser to display the link's events. Now if I type something into the Command event (say ButtonCommand) and press Enter, the code-behind class will appear with a new event on it. While this approach will work, it's not as clean as letting the parent control handle all events and event bubbling. It does break encapsulation by forcing you to handle events for a control not in the immediate child list of the form. If you switch to ASPX view in Visual Studio, you would see the oncommand attribute on the LinkButton tag inside the FooterTemplate tags.

I've introduced you to one of the more powerful technologies in Web control development. How complex templates get all depends on how you use them in your controls but as you can see, they're really quite simple to integrate just about anywhere. As I mentioned at the beginning of the article, you need to consider architecting for extensibility in most systems today and templates allow points of visual extensibility in your custom Web controls. With ASP.NET 2.0, editing templates in the designer is easier than ever making it even more attractive to add template functionality to your controls. While you do this, keep in mind the protocols put in place by Microsoft regarding event bubbling. Following them will give page developers a familiar interface to work with and provide complete encapsulation within your control. Have fun.



Miguel A. Castro is President of InfoTek Consulting Group, Inc., a professional consulting firm that specializes in architecting, designing, and developing solutions using Microsoft .NET technologies. His VB background goes all the way back to 1.0. Miguel's focus for the last couple of years has been the .NET Framework and languages; he is fluent in both VB .NET and C#. His programming experience goes back 20 years when he started on TRS-80s, Apple IIs, and Atari 800 computers. Miguel lives in Lincoln Park, New Jersey with his wife Elena and his daughter Victoria.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date