ITemplate 介面
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義子控制項填入樣板化 ASP.NET 伺服器控制項的行為。 子控制項表示網頁上定義的內嵌樣板。
public interface class ITemplate
public interface ITemplate
type ITemplate = interface
Public Interface ITemplate
- 衍生
範例
下列程式碼範例示範使用 介面建立樣板化屬性的簡單樣板化伺服器控制項 ITemplate 。
using System;
using System.Web;
using System.Web.UI;
namespace TemplateControlSamples {
public class TemplateItem : Control, INamingContainer {
private String _message = null;
public TemplateItem(String message) {
_message = message;
}
public String Message {
get {
return _message;
}
set {
_message = value;
}
}
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
[ParseChildren(true)]
public class Template1 : Control, INamingContainer {
private ITemplate _messageTemplate = null;
private String _message = null;
public String Message {
get {
return _message;
}
set {
_message = value;
}
}
[
PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(TemplateItem))
]
public ITemplate MessageTemplate {
get {
return _messageTemplate;
}
set {
_messageTemplate = value;
}
}
protected override void CreateChildControls() {
// If a template has been specified, use it to create children.
// Otherwise, create a single LiteralControl with the message value.
if (MessageTemplate != null) {
Controls.Clear();
TemplateItem i = new TemplateItem(this.Message);
MessageTemplate.InstantiateIn(i);
Controls.Add(i);
}
else {
this.Controls.Add(new LiteralControl(this.Message));
}
}
}
}
Imports System.Web
Imports System.Web.UI
Namespace TemplateControlSamplesVB
Public Class TemplateItem
Inherits Control
Implements INamingContainer
Private _message As String = Nothing
Public Sub New(Message As String)
_message = message
End Sub
Public Property Message As String
Get
Return _message
End Get
Set
_message = Value
End Set
End Property
End Class
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust"), _
ParseChildren(true)> _
Public Class Template1VB
Inherits Control
Implements INamingContainer
Private _messageTemplate As ITemplate = Nothing
Private _message As String = Nothing
Public Property Message As String
Get
Return _message
End Get
Set
_message = Value
End Set
End Property
<TemplateContainer(GetType(TemplateItem))> _
Public Property MessageTemplate As ITemplate
Get
Return _messageTemplate
End Get
Set
_messageTemplate = Value
End Set
End Property
Protected Overrides Sub CreateChildControls()
' If a template has been specified, use it to create children.
' Otherwise, create a single LiteralControl with the message value.
If Not (MessageTemplate Is Nothing)
Controls.Clear()
Dim I As New TemplateItem(Me.Message)
MessageTemplate.InstantiateIn(I)
Controls.Add(I)
Else
Me.Controls.Add(New LiteralControl(Me.Message))
End If
End Sub
End Class
End Namespace
備註
自訂伺服器控制項會使用此介面,但絕對不會由它們實作。 ASP.NET 一律會實作它。
方法
InstantiateIn(Control) |
當類別實作時,定義子控制項和樣板所屬的 Control 物件。 這些子控制項依次定義在內嵌樣板內。 |