テンプレート (コントロールのデータをコントロール自体の表示とは分離できる ASP.NET 機能) を実装するユーザー コントロールを作成できます。テンプレート コントロールは、ユーザー インターフェイス (UI) を提供しません。コントロールの UI は、ページ開発者がインライン テンプレートを使用して提供します。インライン テンプレートを使用すると、コントロールの UI をカスタマイズできます。テンプレート ユーザー コントロールを作成することによって、カスタム コントロールを .dll ファイルにコンパイルせずにユーザー コントロールをアプリケーションのページに簡単に追加できるようになります。
カスタム テンプレート コントロールの作成の詳細については、「テンプレート コントロールの開発」を参照してください。
テンプレート ユーザー コントロールを作成するには
.ascx ファイルで、テンプレートを表示する ASP.NET Placeholder Web サーバー コントロールを宣言によって作成します。
ユーザー コントロールのコード宣言ブロックまたは分離コード クラスで、ITemplate 型のプロパティを実装します。
同じコード ブロックで、INamingContainer を実装するサーバー コントロールを、テンプレートのインスタンスを作成するためのコンテナとして定義します。このコンテナは名前付けコンテナと呼ばれます。
メモ 基本的に、このコントロールは、ユーザー コントロールの入れ子にされたクラスになりますが、これは必須ではありません。
TemplateContainerAttribute を ITemplate プロパティに適用し、そのテンプレートの名前付けコンテナの型を引数としてコンストラクタまたは属性に渡します。
Page_Init メソッドで、次の手順を 1 回以上繰り返します。
名前付けコンテナ クラスのインスタンスを作成します。
名前付けコンテナ内のテンプレートのインスタンスを作成します。
名前付けコンテナ インスタンスを PlaceHolder サーバー コントロールの Controls プロパティに追加します。
メモ ユーザー コントロールを使用するページの観点からすると、テンプレート ユーザー コントロールの構文はカスタム テンプレート コントロールの構文と同じになります。
テンプレート ユーザー コントロールとそのテンプレート ユーザー コントロールを含むページの例を次に示します。
メモ この例では、1 つのテンプレート プロパティだけを公開していますが、アプリケーションを正常に動作させるために必要なだけテンプレート プロパティを公開できます。
テンプレート ユーザー コントロール
<%@ Control language="VB" debug="True" %>
<script runat="server" >
Private messageTemplate As ITemplate = Nothing
<TemplateContainer(GetType(MessageContainer))>Public Property MessageTemplate() As ITemplate
Get
Return messageTemplate
End Get
Set
messageTemplate = value
End Set
End Property
Sub Page_Init()
If Not (messageTemplate Is Nothing) Then
Dim i As Integer
For i = 0 To 4
Dim container As New MessageContainer(i)
messageTemplate.InstantiateIn(container)
msgholder.Controls.Add(container)
Next i
End If
End Sub 'Page_Init
Public Class MessageContainer
Inherits Control
Implements INamingContainer
Private index As Integer
Friend Sub New(index As Integer)
Me.index = index
End Sub 'New
Public ReadOnly Property Index() As Integer
Get
Return index
End Get
End Property
End Class 'MessageContainer
</script>
<hr>
<asp:placeholder runat=server id=msgholder/>
<hr>
[C#]
<%@ Control language=c# debug=true %>
<script runat=server>
private ITemplate messageTemplate = null;
[ TemplateContainer(typeof(MessageContainer)) ]
public ITemplate MessageTemplate {
get { return messageTemplate; }
set { messageTemplate = value; }
}
void Page_Init() {
if (messageTemplate != null) {
for (int i=0; i<5; i++) {
MessageContainer container = new MessageContainer(i);
messageTemplate.InstantiateIn(container);
msgholder.Controls.Add(container);
}
}
}
public class MessageContainer: Control, INamingContainer {
private int index;
internal MessageContainer(int index) { this.index = index; }
public int Index { get { return index; } }
}
</script>
<hr>
<asp:placeholder runat=server id=msgholder/>
<hr>
テンプレート ユーザー コントロールを含む ASP.NET ページ
<%@ language=vb debug=true %>
<%@ Register TagPrefix="acme" tagname="test" src=TemplatedUserControl.ascx %>
<html>
<script runat=server>
Sub Page_Load()
DataBind()
End Sub 'Page_Load
</script>
<body>
' The Container in the data-binding syntax will automatically be of type
' MessageContainer, since the TemplateContainer attribute was applied
' to the ITemplate class in the .ascx file.
<form runat=server>
<acme:test runat=server>
<MessageTemplate>
Hello #<%# Container.Index %>.<br>
</MessageTemplate>
</acme:test>
</form>
</body>
</html>
[C#]
<%@ language=c# debug=true %>
<%@ Register TagPrefix="acme" tagname="test" src=TemplatedUserControl.ascx %>
<html>
<script runat=server>
void Page_Load() {
DataBind();
}
</script>
<body>
// The Container in the data-binding syntax will automatically be of type
// MessageContainer, since the TemplateContainer attribute was applied
// to the ITemplate class in the .ascx file.
<form runat=server>
<acme:test runat=server>
<MessageTemplate>
Hello #<%# Container.Index %>.<br>
</MessageTemplate>
</acme:test>
</form>
</body>
</html>
参照
Web フォーム ユーザー コントロール | Web フォームのコード モデル | Web フォーム ページへのユーザー コントロールの取り込み | @ Control | UserControl