次の方法で共有


テンプレート コントロールのサンプル

次に示す TemplatedFirstControl テンプレート コントロールのサンプルには、ITemplate 型の FirstTemplate プロパティと、コントロールのデータが格納されている 2 つのプロパティ Text および DateTime が記述されています。TemplatedFirstControl は、「単純 ASP.NET サーバー コントロールの開発」のサンプルをテンプレート コントロールに変換します。これにより、ページ開発者がこのコントロール自体の表示をカスタマイズできるようになります。

このサンプルをビルドする方法については、「サーバー コントロールのサンプル」の手順を参照してください。

using System;
using System.Web;
using System.Web.UI;

namespace CustomControls
{
    [
    ParseChildren(true)
    ]
      public class TemplatedFirstControl : Control, INamingContainer
      {
            private ITemplate firstTemplate;
            private String text = null;
            private Control myTemplateContainer;
            
        protected override void OnDataBinding(EventArgs e) {
            EnsureChildControls();
            base.OnDataBinding(e);
        }


            [TemplateContainer(typeof(FirstTemplateContainer))]
                  
                  public ITemplate FirstTemplate
            {
                  get 
                  {
                        return firstTemplate;
                  }
                  set
                  {
                        firstTemplate = value;
                  }
            }
            
            public String Text
            {
                  get 
                  {
                        return text;
                  }
                  set
                  {
                        text = value;
                  }
            }
            
            public String DateTime
            {
                  get 
                  {
                        return System.DateTime.Now.ToLongTimeString();
                  }
                  
            }
            
            public Control MyTemplateContainer
            {
                  get
                  {
                        return myTemplateContainer;
                  }
            }
            
            protected override void CreateChildControls ()
                  
            {
                  if (FirstTemplate != null)
                  {
                        myTemplateContainer = new FirstTemplateContainer(this);
                        FirstTemplate.InstantiateIn(myTemplateContainer);
                        Controls.Add(myTemplateContainer);
                  }
                  else
                  {
                        Controls.Add(new LiteralControl(Text + " " + DateTime));
                  }
            }
            
      }
}
[Visual Basic]
Option Explicit
Option Strict

Imports System
Imports System.Web
Imports System.Web.UI

Namespace CustomControls
   <ParseChildren(True)> _
   Public Class 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    
      
      <TemplateContainer(GetType(FirstTemplateContainer))> _
      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
      
      
      Protected Overrides Sub CreateChildControls()
         
         If Not (FirstTemplate Is Nothing) Then
            _myTemplateContainer = New FirstTemplateContainer(Me)
            FirstTemplate.InstantiateIn(_myTemplateContainer)
            Controls.Add(_myTemplateContainer)
         Else
            Controls.Add(New LiteralControl(Text + " " + DateTime))
         End If
      End Sub
   End Class 
End Namespace

    

テンプレートのコンテナであるコントロールを次に示します。

using System;
using System.Web;
using System.Web.UI;

namespace CustomControls
{
public class FirstTemplateContainer : Control, INamingContainer
      {
            private TemplatedFirstControl parent;
            public FirstTemplateContainer(TemplatedFirstControl parent)
            {
                  this.parent = parent;
            }
            
            public String Text
            {
                  get
                  {
                        return parent.Text;
                  }
            }
            public String DateTime
            {
                  get 
                  {
                        return parent.DateTime;
                  }
                  
            }
      }
}
[Visual Basic]
Option Explicit
Option Strict

Imports System
Imports System.Web
Imports System.Web.UI

Namespace CustomControls
   Public Class FirstTemplateContainer
      Inherits Control
      Implements INamingContainer
      Private _parent As TemplatedFirstControl
      
      Public Sub New(parent As TemplatedFirstControl)
         Me._parent = parent
      End Sub
      
      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
End Namespace

ページでのテンプレート コントロールの使用

次の ASP.NET ページは、前のサンプルで定義したテンプレート コントロールを使用しています。このページは、2 つの異なるテンプレートを適用してコントロールを使用し、次にテンプートを指定せずに同じコントロールを使用しています。

<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>

<script runat=server language="VB">
   Sub Page_Load()
      First.DataBind()
      Second.DataBind()
   End Sub
</script>

<html>

   <body>
      
      <form  runat=server>
                  
          Here is a custom templated server control:<br><br>
          <Custom:TemplatedFirstControl id = "First" Text= "The time on the server is "  runat=server>                         
          <FirstTemplate>
          <h3><font face="Verdana" color = "red"><%# Container.Text %> <%# Container.DateTime %>
          </font></h3>
          </FirstTemplate>      
        </Custom:TemplatedFirstControl>
          
          Here is the templated server control with a different template:<br><br>
          <Custom:TemplatedFirstControl id = "Second" Text= "The time on the server is "  runat=server>                         
          <FirstTemplate>
          <h1><font face="Arial" color = "Purple"><%# Container.Text %> <%# Container.DateTime %>
          </font></h1>
          </FirstTemplate>     
        </Custom:TemplatedFirstControl>
           
           Here is the templated server control without a template:<br><br>
          <Custom:TemplatedFirstControl id = "Third" Text= "The time on the server is "  runat=server/>
                               
      </form>

   </body>

</html>

参照

template 宣言のあるデータ連結コントロールの開発