Export (0) Print
Expand All

Templated Control Sample

.NET Framework 1.1

The following sample of a templated control, TemplatedFirstControl, has a property FirstTemplate, of type ITemplate, and two properties, Text and DateTime, that contain the data for the control. TemplatedFirstControl converts the sample in Developing a Simple ASP.NET Server Control into a templated control, thus allowing a page developer to customize its presentation.

To build the sample, see the instructions in Server Control Samples.

using System;
using System.Web;
using System.Web.UI;

namespace CustomControls
{
    [
    ParseChildren(true)
    ]
      public class TemplatedFirstControl : Control, INamingContainer
      {
            private ITemplate firstTemplate;
            private String text = null;
            private Control myTemplateContainer;
            
        protected override void OnDataBinding(EventArgs e) {
            EnsureChildControls();
            base.OnDataBinding(e);
        }


            [TemplateContainer(typeof(FirstTemplateContainer))]
                  
                  public ITemplate FirstTemplate
            {
                  get 
                  {
                        return firstTemplate;
                  }
                  set
                  {
                        firstTemplate = value;
                  }
            }
            
            public String Text
            {
                  get 
                  {
                        return text;
                  }
                  set
                  {
                        text = value;
                  }
            }
            
            public String DateTime
            {
                  get 
                  {
                        return System.DateTime.Now.ToLongTimeString();
                  }
                  
            }
            
            public Control MyTemplateContainer
            {
                  get
                  {
                        return myTemplateContainer;
                  }
            }
            
            protected override void CreateChildControls ()
                  
            {
                  if (FirstTemplate != null)
                  {
                        myTemplateContainer = new FirstTemplateContainer(this);
                        FirstTemplate.InstantiateIn(myTemplateContainer);
                        Controls.Add(myTemplateContainer);
                  }
                  else
                  {
                        Controls.Add(new LiteralControl(Text + " " + DateTime));
                  }
            }
            
      }
}
[Visual Basic]
Option Explicit
Option Strict

Imports System
Imports System.Web
Imports System.Web.UI

Namespace CustomControls
   <ParseChildren(True)> _
   Public Class TemplatedFirstControl
      Inherits Control
      Implements INamingContainer
      Private _firstTemplate As ITemplate
      Private _text As String = Nothing
      Private _myTemplateContainer As Control
      
      Protected Overrides Sub OnDataBinding(e As EventArgs)
         EnsureChildControls()
         MyBase.OnDataBinding(e)
      End Sub    
      
      <TemplateContainer(GetType(FirstTemplateContainer))> _
      Public Property FirstTemplate() As ITemplate
         Get
            Return _firstTemplate
         End Get
         Set
            _firstTemplate = value
         End Set
      End Property
      
      Public Property Text() As String
         Get
            Return _text
         End Get
         Set
            _text = value
         End Set
      End Property
      
      
      Public ReadOnly Property DateTime() As String
         Get
            Return System.DateTime.Now.ToLongTimeString()
         End Get
      End Property 
      
      
      Public ReadOnly Property MyTemplateContainer() As Control
         Get
            Return _myTemplateContainer
         End Get
      End Property
      
      
      Protected Overrides Sub CreateChildControls()
         
         If Not (FirstTemplate Is Nothing) Then
            _myTemplateContainer = New FirstTemplateContainer(Me)
            FirstTemplate.InstantiateIn(_myTemplateContainer)
            Controls.Add(_myTemplateContainer)
         Else
            Controls.Add(New LiteralControl(Text + " " + DateTime))
         End If
      End Sub
   End Class 
End Namespace

    

The following control is the container for the template.

using System;
using System.Web;
using System.Web.UI;

namespace CustomControls
{
public class FirstTemplateContainer : Control, INamingContainer
      {
            private TemplatedFirstControl parent;
            public FirstTemplateContainer(TemplatedFirstControl parent)
            {
                  this.parent = parent;
            }
            
            public String Text
            {
                  get
                  {
                        return parent.Text;
                  }
            }
            public String DateTime
            {
                  get 
                  {
                        return parent.DateTime;
                  }
                  
            }
      }
}
[Visual Basic]
Option Explicit
Option Strict

Imports System
Imports System.Web
Imports System.Web.UI

Namespace CustomControls
   Public Class FirstTemplateContainer
      Inherits Control
      Implements INamingContainer
      Private _parent As TemplatedFirstControl
      
      Public Sub New(parent As TemplatedFirstControl)
         Me._parent = parent
      End Sub
      
      Public ReadOnly Property Text() As [String]
         Get
            Return _parent.Text
         End Get
      End Property
      
      Public ReadOnly Property DateTime() As [String]
         Get
            Return _parent.DateTime
         End Get
      End Property 
   End Class
End Namespace

Using the templated control on a page

The following ASP.NET page uses the templated control defined in the preceding sample. It uses the control with two different templates and then without a template.

<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>

<script runat=server language="VB">
   Sub Page_Load()
      First.DataBind()
      Second.DataBind()
   End Sub
</script>

<html>

   <body>
      
      <form  runat=server>
                  
          Here is a custom templated server control:<br><br>
          <Custom:TemplatedFirstControl id = "First" Text= "The time on the server is "  runat=server>                         
          <FirstTemplate>
          <h3><font face="Verdana" color = "red"><%# Container.Text %> <%# Container.DateTime %>
          </font></h3>
          </FirstTemplate>      
        </Custom:TemplatedFirstControl>
          
          Here is the templated server control with a different template:<br><br>
          <Custom:TemplatedFirstControl id = "Second" Text= "The time on the server is "  runat=server>                         
          <FirstTemplate>
          <h1><font face="Arial" color = "Purple"><%# Container.Text %> <%# Container.DateTime %>
          </font></h1>
          </FirstTemplate>     
        </Custom:TemplatedFirstControl>
           
           Here is the templated server control without a template:<br><br>
          <Custom:TemplatedFirstControl id = "Third" Text= "The time on the server is "  runat=server/>
                               
      </form>

   </body>

</html>

See Also

Developing a Templated Data-Bound Control

Show:
© 2014 Microsoft