Programming Microsoft ASP.NET 4 - Dino Esposito [287]
placeHolder.Controls.Add(Page.LoadControl("usercontrol.ascx"));
The right time to call this code is early in the control life cycle—that is, in an Init event handler. Using the LoadControl method, the code of the template is insulated in a separate file. This can be a good thing or a bad thing, depending on the context. If the template you want to implement is complex, keeping it off the main page is positive. Otherwise, it would certainly add a layer of unnecessary complexity. Having the template directly available in the source code of the page makes authoring the page much more intuitive and fast because you don’t have to follow code into a separate file.
There’s also a sort of compromise between the two approaches. You can define an ITemplate property in the control and leave the page author free to decide how to set it—with statically defined markup or using the contents of an .ascx file.
Defining a Template Property
A template property represents a collection of text and controls that is hosted within a container. The container is also responsible for exposing properties that page authors can use to create data-bound expressions. The following code snippet shows how to define a template property named TitleTemplate:
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateContainer(typeof(TitleTemplateContainer))]
public ITemplate TitleTemplate
{
get { return _titleTemplate; }
set { _titleTemplate = value; }
}
The storage of the template is guaranteed by the private member _titleTemplate, defined as follows:
private ITemplate _titleTemplate = null;
A template property is characterized by a couple of attributes: PersistenceMode and TemplateContainer.
The PersistenceMode attribute indicates how a control property is persisted declaratively in a host page. Table 12-5 lists possible modes of persistence.
Table 12-5. Persistence Modes for Control Properties
Property
Description
Attribute
The property persists as an encoded HTML attribute in the final markup.
EncodedInnerDefaultProperty
The property persists as the only inner text of the control. The property value is HTML encoded. Only a string can be given this designation.
InnerDefaultProperty
The property persists in the control as inner text and is the element’s default property. Only one property can be designated the default property.
InnerProperty
The property persists in the control as a nested tag. This is commonly used for complex objects with templates and styles.
The most common setting is InnerProperty, which instructs Microsoft Visual Studio to save the contents of the template as a nested tag named after the property:
...
If you choose InnerDefaultProperty, you can have only one nested tag; by opting for InnerProperty, you can have as many nested tags as needed. This is good for rich controls with multiple templates and styles.
The TemplateContainer attribute declares the type of the naming container that will contain the template once it is created. As mentioned, a template is hosted by a container which, in turn, is appended to the control’s Controls collection. The TemplateContainer attribute references a type that you, as a control developer, are responsible for declaring.
Defining a Template Container
A template container type is a simple Web control decorated with the INamingContainer interface. This control can be given any public members you