Adding Templates to a Control


Several of the standard ASP.NET controls, such as Repeater , DataList , and DataGrid , support templates. By adding templates to a control, you can enable its users to customize its output.

To add a template to a control, you need to complete each of the following steps:

  1. Add a property that represents the template to the custom control. This property must implement the ITemplate interface.

  2. Create a new control that functions as a container for the template (the template container control).

  3. Instantiate the template in the template container control by calling the template's InstantiateIn method.

In Listing 29.1, for example, a new custom control named SimpleTemplate is created. This control has a property named ItemTemplate that represents a template. This property implements the ITemplate interface.

Listing 29.1 also contains a declaration for a second control named TemplateItem . This control functions as a container control for the template.

Finally, the SimpleTemplate control contains a CreateChildControls subroutine. Within this subroutine, the TemplateItem control is instantiated . Next, the InstantiateIn method is called on the template to instantiate all the elements of the template in the TemplateItem control. Finally, the TemplateItem is added to the SimpleTemplate control's Controls collection.

Listing 29.1 SimpleTemplate.vb
 Imports System Imports System.Web Imports System.Web.UI Namespace myControls Public Class TemplateItem Inherits Control Implements INamingContainer   Private _dataItem As String   Public Sub New( DataItem As String )     _dataItem = DataItem   End Sub   Public Property DataItem As String     Get       Return _dataItem     End Get     Set       _dataItem = Value     End Set   End Property End Class <ParseChildren(true)> Public Class SimpleTemplate Inherits Control Implements INamingContainer   Private _itemTemplate As ITemplate   Private _text As String   Public Property Text As String     Get       Return _text     End Get     Set       _text = Value     End Set   End Property   <TemplateContainer(GetType(TemplateItem))> _   Public Property ItemTemplate As ITemplate     Get       Return _itemTemplate     End Get     Set       _itemTemplate = Value     End Set   End Property   Public Overrides Sub DataBind()     EnsureChildControls()     MyBase.DataBind()   End Sub   Protected Overrides Sub CreateChildControls()     Dim objTemplateItem As TemplateItem     Controls.Clear()     objTemplateItem = New TemplateItem( Me.Text )     ItemTemplate.InstantiateIn( objTemplateItem )     Controls.Add( objTemplateItem )   End Sub End Class End Namespace 

The C# version of this code can be found on the CD-ROM.

After you create the file in Listing 29.1, you need to compile it by executing the following statement from the command line:

 
 vbc /t:library /r:System.dll,System.Web.dll SimpleTemplate.vb 

This statement compiles the file into an assembly named SimpleTemplate.dll . You need to copy the SimpleTemplate.dll file into your application's /bin directory before you can use the SimpleTemplate control in an ASP.NET page.

The page in Listing 29.2 uses the SimpleTemplate control to format the value of a property named Text .

Listing 29.2 DisplaySimpleTemplate.aspx
 <%@ Register TagPrefix="myControls" Namespace="myControls" Assembly="SimpleTemplate"%> <Script Runat="Server"> Sub Page_Load   DataBind() End Sub </Script> <html> <head><title>DisplaySimpleTemplate.aspx</title></head> <body> <form Runat="Server"> <myControls:SimpleTemplate   Text="Hello World!"   Runat="Server"> <ItemTemplate>   The value of DataItem is:   <b><%# Container.DataItem %></b> </ItemTemplate> </myControls:SimpleTemplate> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

In Listing 29.2, an instance of the SimpleTemplate control is declared. Within this control, the ItemTemplate template is declared.

The myTemplate template formats the output of a property of the SimpleTemplate property named Text . The value of the Text property is displayed in bold by the template.

Notice that you must call the DataBind() method in the Page_Load subroutine. The DataBind() method causes all the databinding expressions in the page to be interpreted including the databinding expression in the ItemTemplate .

Creating Multiple Instances of a Template

After you create a template, you can instantiate it multiple times within a control. For example, imagine that you decided to create a control that displays the numbers 1 through 10. Now imagine that you want to add a template to the control to format how each number is displayed. The control in Listing 29.3 does exactly that.

Listing 29.3 TemplateNumbers.vb
 Imports System Imports System.Web Imports System.Web.UI Namespace myControls Public Class TemplateNumbersItem Inherits Control Implements INamingContainer   Private _dataItem As Integer   Public Sub New( DataItem As Integer )     _dataItem = DataItem   End Sub   Public Property DataItem As Integer     Get       Return _dataItem     End Get     Set       _dataItem = Value     End Set   End Property End Class <ParseChildren(true)> Public Class TemplateNumbers Inherits Control Implements INamingContainer   Private _numberTemplate As ITemplate   <TemplateContainer(GetType(TemplateNumbersItem))> _   Public Property NumberTemplate As ITemplate     Get       Return _numberTemplate     End Get     Set       _numberTemplate = Value     End Set   End Property   Public Overrides Sub DataBind()     EnsureChildControls()     MyBase.DataBind()   End Sub   Protected Overrides Sub CreateChildControls()     Dim intCounter As Integer     Dim objTemplateNumbersItem As TemplateNumbersItem     Controls.Clear()     For intCounter = 1 To 10       objTemplateNumbersItem = New TemplateNumbersItem( intCounter )       NumberTemplate.InstantiateIn( objTemplateNumbersItem )       Controls.Add( objTemplateNumbersItem )     Next   End Sub End Class End Namespace 

The C# version of this code can be found on the CD-ROM.

Listing 29.3 contains the declaration of a control named TemplateNumbers . This control contains the CreateChildControls subroutine, which creates multiple instances of the TemplateNumbersItem template container. Within the subroutine, a FOR...NEXT loop steps through the numbers 1 through 10. For each step in the loop, an instance of TemplateNumbersItem is created and added to the controls collection.

The page in Listing 29.4 illustrates how you can use the TemplateNumbers control in an ASP.NET page.

Listing 29.4 DisplayTemplateNumbers.aspx
 <%@ Register TagPrefix="myControls" Namespace="myControls" Assembly="TemplateNumbers"%> <Script runat="Server"> Sub Page_Load   DataBind() End Sub </Script> <html> <head><title>DisplayTemplateNumbers.aspx</title></head> <body> <form Runat="Server"> <myControls:TemplateNumbers   Runat="Server">   <NumberTemplate>   <li><i><%# Container.DataItem %></i>   </NumberTemplate> </myControls:TemplateNumbers> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

The page in Listing 29.4 uses the TemplateNumbers control to display the numbers 1 through 10. Each number is formatted with the NumberTemplate template.

Adding Multiple Templates to a Control

Most likely, you need to add multiple templates to a control. For example, the standard ASP.NET Repeater control has six templates: HeaderTemplate , ItemTemplate , AlternatingItemTemplate , SeparatorTemplate , and FooterTemplate .

You can create multiple templates for a control by adding multiple properties that implement the ITemplate interface to the control. For example, the control in Listing 29.5 contains both ItemTemplate and AlternatingItemTemplate .

Listing 29.5 MultipleTemplates.vb
 Imports System Imports System.Web Imports System.Web.UI Namespace myControls Public Class MultipleTemplatesTemplateItem Inherits Control Implements INamingContainer     Private _dataItem As Integer     Public Sub New( DataItem As Integer )       _dataItem = DataItem     End Sub     Public Property DataItem As Integer     Get       Return _dataItem     End Get     Set       _dataItem = Value     End Set   End Property End Class <ParseChildren(true)> Public Class MultipleTemplates Inherits Control Implements INamingContainer   Private _itemTemplate As ITemplate   Private _alternatingItemTemplate As ITemplate   <TemplateContainer(GetType(MultipleTemplatesTemplateItem))> _   Public Property ItemTemplate As ITemplate     Get       Return _itemTemplate     End Get     Set       _itemTemplate = Value     End Set   End Property   <TemplateContainer(GetType(MultipleTemplatesTemplateItem))> _   Public Property AlternatingItemTemplate As ITemplate     Get       Return _alternatingItemTemplate     End Get     Set       _alternatingItemTemplate = Value     End Set   End Property   Public Overrides Sub DataBind()     EnsureChildControls()     MyBase.DataBind()   End Sub   Protected Overrides Sub CreateChildControls()     Dim intCounter As Integer     Dim objTemplateItem As MultipleTemplatesTemplateItem     Controls.Clear()     For intCounter = 1 To 10       objTemplateItem = New MultipleTemplatesTemplateItem( intCounter )       If Decimal.Remainder( intCounter, 2 ) = 0 Then         ItemTemplate.InstantiateIn( objTemplateItem )       Else         AlternatingItemTemplate.InstantiateIn( objTemplateItem )     End If     Controls.Add( objTemplateItem )   Next End Sub End Class End Namespace 

The C# version of this code can be found on the CD-ROM.

Listing 29.5 contains declarations for two templates. The MultipleTemplate control has properties named ItemTemplate and AlternatingItemTemplate .

Within the FOR...NEXT loop, every number is displayed in either ItemTemplate or AlternatingItemTemplate . The Remainder method of the Decimal class determines whether a number is even or odd. If a number is even, it's displayed by AlternatingItemTemplate ; otherwise , it's displayed with ItemTemplate .

The page in Listing 29.6 illustrates how you can specify ItemTemplate and AlternatingItemTemplate within an ASP.NET page.

Listing 29.6 DisplayMultipleTemplates.aspx
 <%@ Register TagPrefix="myControls" Namespace="myControls" Assembly="MultipleTemplates"%> <Script runat="Server"> Sub Page_Load   DataBind() End Sub </Script> <html> <head><title>DisplayMultipleTemplates.aspx</title></head> <body> <form Runat="Server"> <myControls:MultipleTemplates   Runat="Server">   <ItemTemplate>     <li><i><%# Container.DataItem %></i>   </ItemTemplate>   <AlternatingItemTemplate>     <li><h2><%# Container.DataItem %></h2>   </AlternatingItemTemplate> </myControls:MultipleTemplates> </form> </body> </html> 

The C# version of this code can be found on the CD-ROM.

In Listing 29.6, an instance of the MultipleTemplates control is declared. ItemTemplate formats each number in italics, and AlternatingItemTemplate formats each number with the HTML <H2> heading tag (see Figure 29.1).

Figure 29.1. Displaying content with multiple templates.

graphics/29fig01.jpg



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

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