- Introduction
- Declaring a Simple Custom Control
- Extending Existing Web Controls
- Creating ViewState-Enabled Control Properties
- Creating a Composite Control
- Creating a Data-bound Control
- Creating a Templated Control
- Dynamically Adding Controls to a Web Form
- Using the Treeview IE Web Control
- Using the TabControl and PageView IE Web Controls
- Using the ToolBar IE Web Control
- Data-binding a TreeView Control
- Installing a Component in the Global Assembly Cache (GAC)
3.6. Creating a Templated Control
You want to create a control that supports the use of templates.
Technique
This example creates a simple templated control. It displays the current time on the server on which it runs and allows you to add dynamic text and so on.
First, the main control class, DateTimeControl:
Imports System Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.ComponentModel Namespace AspNetCookbook <ToolboxData("<{0}:DateTimeControl runat=server></{0}:DateTimeControl>"), _ ParseChildren(True)> _ Public Class DateTimeControl Inherits Control Implements INamingContainer Private _template As ITemplate Private _container As DateTimeContainer Private _text As String <TemplateContainer(GetType(DateTimeContainer))> _ Public Overridable Property Template() As ITemplate Get Return _template End Get Set(ByVal Value As ITemplate) _template = Value End Set End Property Public Overridable ReadOnly Property Container() As DateTimeContainer Get Return _container End Get End Property Public Overridable Property Text() As String Get Return _text End Get Set(ByVal Value As String) _text = Value End Set End Property Public Overridable ReadOnly Property DateTime() As String Get Return System.DateTime.Now.ToShortTimeString() End Get End Property Protected Overrides Sub OnDataBinding(ByVal e As EventArgs) EnsureChildControls() MyBase.OnDataBinding(e) End Sub 'OnDataBinding Protected Overrides Sub CreateChildControls() If Not (Template Is Nothing) Then _container = New DateTimeContainer(Text, DateTime) Template.InstantiateIn(Container) Controls.Add(Container) Else Controls.Add(New LiteralControl("" + [Text] + " " + DateTime)) End If End Sub End Class
Now let's take a look at the container control DateTimeContainer. It enables you to use <%# Container.Text %> and so forth.
Public Class DateTimeContainer Inherits Control Implements INamingContainer Private _text As String Private _dateTime As String Public Sub New(ByVal text As String, ByVal dateTime As String) Me._text = text Me._dateTime = dateTime End Sub 'New Public ReadOnly Property Text() As String Get Return _text End Get End Property Public ReadOnly Property DateTime() As String Get Return _dateTime End Get End Property End Class End Namespace
Comments
This control allows you to add a template and thus choose how you want the data to be presented. The control is built using two classesDateTimeControl is the actual control you add to the page and DateTimeContainer is the container control that holds the actual template data. Use of a container class is, strictly speaking, not necessary if you don't want to use custom properties. If you just want to display static content, you can instantiate the control in a Panel control or a similar control.
See Also
Section 3.4, "Creating a Composite Control"
Section 3.5, "Creating a Data-bound Control"