.net Framework拾遗(4) Itemplater,TemplateContainerAttribute
目录:
1.Itemplater接口
2.ItemplateContainerAttribute类
1.Itemplate接口
当在 .aspx 文件中声明时,为在使用具有内联模板的控件时用子控件填充 ASP.NET 服务器控件定义要实现的方法。
Public Interface ITemplate
该接口供自定义服务器控件使用,但从不通过这些服务器控件实现,asp.net始终负责实现该接口.
成员只有一个公共方法
sub Instantiatein(byval Container as control)
Container:包含内联模板中的实例化控件的control对象
开发模板服务器控件时,不需要实现此方法。
Public Sub InstantiateIn(container As Control) Implements ITemplate.InstantiateIn
Dim l As New Literal()
AddHandler l.DataBinding, AddressOf Me.BindData
container.Controls.Add(l)
End Sub 'InstantiateIn
'------------------------------------------------------------------------------------------------
2.ItemplateContainerAttribute类
声明在创建后将包含模板的 INamingContainer 的类型。
System.Object
System.Attribute
System.Web.UI.TemplateContainerAttribute
<AttributeUsage(AttributeTargets.Property)>
NotInheritable Public Class TemplateContainerAttribute
Inherits Attribute
'**********************************************************************************************
' File name:templatecontainerattribute.cs.
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Collections
Namespace CustomControls
<ParseChildren(True)> _
Public Class VB_TemplatedFirstControl
Inherits Control
Implements INamingContainer
Private _firstTemplate As ITemplate
Private [_text] As [String] = Nothing
Private _myTemplateContainer As Control
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub OnDataBinding(e As EventArgs)
EnsureChildControls()
MyBase.OnDataBinding(e)
End Sub
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
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub CreateChildControls()
If Not (FirstTemplate Is Nothing) Then
_myTemplateContainer = New VB_FirstTemplateContainer(Me)
FirstTemplate.InstantiateIn(_myTemplateContainer)
Controls.Add(_myTemplateContainer)
Else
Controls.Add(New LiteralControl([Text] + " " + DateTime))
End If
End Sub 'CreateChildControls
End Class 'VB_TemplatedFirstControl
Public Class VB_FirstTemplateContainer
Inherits Control
Implements INamingContainer
Private _parent As VB_TemplatedFirstControl
Public Sub New(parent As VB_TemplatedFirstControl)
Me._parent = parent
End Sub 'New
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 'VB_FirstTemplateContainer
End Namespace 'CustomControls
'***********************************************************************************