. NET Framework Supplements (4) Itemplater,templatecontainerattribute
Directory:
1.Itemplater interface
2.ItemplateContainerAttribute class
1.Itemplate interface
When declared in an. aspx file, populates the ASP.net server control with child controls when using controls with inline templates to define the method to implement.
Public Interface ITemplate
This interface is intended for use by custom server controls, but is never implemented through these server controls, and ASP.net is always responsible for implementing the interface.
Members have only one public method
Sub InstantiateIn (ByVal Container As Control)
Container: Control object containing instantiated controls in inline templates
You do not need to implement this method when developing a template server 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 class
Declares the type of INamingContainer that will contain the template after it is created.
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 are 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
'***********************************************************************************