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 AspNet
<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
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 classes—DateTimeControl 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.
Showing posts with label Creating Templated Control. Show all posts
Showing posts with label Creating Templated Control. Show all posts
Subscribe to:
Posts (Atom)
Archives
-
▼
2008
(100)
-
▼
October
(21)
- Change the Permissions Given to ASP.NET Code
- ASP.Net Enabling Web Site Debugging
- ASP.NET Web Service Creation
- ASP.NET Stack Collection
- ASP.NET Queue Collection
- ASP.NETHashTable Collection
- ASP.NET Creating Custom Collection
- How to Declare Collections in ASP.NET
- Load User Controls Programmatically
- Dynamically Render an Image
- Add Controls to Web Form Dynamically
- Perform Selective Input Validation
- Use Forms Authentication
- Using IIS Authentication
- Allow User to Upload File
- Programmatically Set Control Focus
- Pop-Up Window with JavaScript
- Respond to Client-Side Events with JavaScript
- Creating Stateful Page Member Variables
- Storing Information Between Postbacsk
- Redirect the User to Another Page
-
▼
October
(21)
RSS Feed (xml)