in

ASP.NET Weblogs

Andy Smith's Blog

Page.RegisterStartupScript('Andy', 'MetaBuilders_WebControls_GainKnowledge();');

Favorite Whidbey Features #2, Single Instance Templates

Templates are great at providing a way to specify the UI for a databound control. The control takes care of the behavior, and the template, designed by the page developer, takes care of the UI. However, not everybody wants to use templates with databinding. Some people want to provide controls that offer behavior tied to a specific template, only instanciated once. It's handy to use Templates to allow the page developer to specify the look of a view, but there's a small problem having to do with codegen.

You see, people are used to dragging controls onto a form, and being able to use the ID of that control as a variable tied to that control. This is some “magic” that the asp.net aspx compiler does that creates a member on the Page that is tied to the object instance declared in the tag. However, in V1.x, controls inside templates weren't created as members on the page, presumably because it was assumed that template controls would be part of a repeating pattern such as in a repeater. This assumption is not always true, as in the case of wanting a template to be used only once, to provide the UI for you control's behavior.

So now v2 offers a TemplateInstanceAttribute with an associated TemplateInstance enum, with Single and Multiple values. So now, if your control does not implement INamingContainer and you have a Template property with the TemplateInstance.Single attribute applied, any controls declared inside your template, it is now available as a Page level member.

Neat, eh?

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add