.NET Framework Pickup (4) ItemPlater, TemplateContainerattribute

xiaoxiao2021-03-06  119

.NET Framework Pickup (4) ItemPlater, TemplateContainerattribute

Directory: 1.Itemplater interface 2.ItemplateContainerattribute class

IIIMPLATE interface

When declared in the .aspx file, the method of defining the ASP.NET server control to define the method of implementing the ASP.NET server control when using the control with the inner template. Public interface Itemplate This interface is used to customize server controls, but from these server controls, ASP.NET is always responsible for implementing the interface.

This method does not need to be implemented when there is only one public method Sub Instantiatein (ByVal Container AS Control) Container:

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

Declare the type of InamingContainer that will contain templates after creating. System.Object System.attribute System.Web.ui.TemplateContainerattribute

Notinheritable Public Class TemplateContainerattribute Inherits Attribute

'********************************************************** ***************************************************

'File Name: TemplateContainerattribute.cs.

Imports SystemImports System.Webimports System.Web.ui.WebControlsimports System.Collections System.collections System

Namespace CustomTrols _ Public Class VB_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 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 E nd Get End Property _ 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 'CreateChildControlsEnd 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

'********************************************************** ****************************************

转载请注明原文地址:https://www.9cbs.com/read-100295.html

New Post(0)