共用方式為


樣板化的控制項範例

下列樣板化的控制項 TemplatedFirstControl 的範例具有型別為 ITemplate 的屬性 FirstTemplate,以及 TextDateTime 兩個含有控制項資料的屬性。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 網頁使用定義於前面範例的樣板化控制項。它使用具有兩個不同樣板的控制項,而接著不使用樣板。

<%@ 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>

請參閱

開發樣板化的資料繫結控制項