共用方式為


ParseChildrenAttribute 範例

此處說明的範例控制項將說明如何套用 ParseChildrenAttribute 至控制項以指定預設屬性,以及這個屬性如何簡化設定預設屬性的宣告式語法。預設屬性通常是一個集合型別並且可以保存任何系統型別或自訂型別的項目。

範例控制項 (本主題稍後將說明) 的標記如下所述:

[ParseChildren(true, "Employees")]
public class CollectionPropertyControl : Control {...} 

Employees 是型別 System.Collections.ArrayList 的屬性且可保存自訂型別 (Employee) 的項目。

當控制項標記上 ParseChildrenAttribute 時,將 Employee 物件加入至 Employees 屬性的宣告式語法如下所示:

<Custom:CollectionPropertyControl runat = "server">
<Custom:Employee Name = "Alice" Alias = "AliceA" Title = "Manager" />
<Custom:Employee Name = "Jerry" Alias = "JerryR" Title = "Programmer" />
<Custom:Employee Name = "Lynn" Alias = "LynnP" Title = "Architect" />
<Custom:Employee Name = "Mike" Alias = "MikeB" Title = "Tester" />
</Custom:CollectionPropertyControl> 

請注意控制項的標記內並未指定屬性名稱。

<Custom:CollectionPropertyControl runat = "server">
<%-- <Employees> not specified --%>
<Custom:Employee Name = "Alice" Alias = "AliceA" Title = "Manager" />
...
<Custom:Employee Name = "Mike" Alias = "MikeB" Title = "Tester" />
<%-- </Employees> not specified --%>
</Custom:CollectionPropertyControl>

ParseChildrenAttribute 即可使用簡單宣告式語法將項目加入至預設屬性。當預設屬性是一個集合型別時,網頁剖析器將建立子項目 (在控制項的標記內指定) 並將它們加入至集合。本主題結尾處也將提供一個語法的範例網頁。

以下將說明自訂控制項和自訂型別 (屬於集合項目) 的程式碼。若要建置 (Build) 這個範例,請參閱伺服器控制項範例中的說明。

using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomControls
{
   public class Employee
   {
      private String name;
      private String title;
      private String alias;

      public Employee():this ("","",""){}
      
      public Employee (String name, String title, String alias)
      {
         this.name = name;
         this.title = title;
         this.alias = alias;
      }
      public String Name
      {
         get
         {
            return name;
         }
         set
         {
            name = value;
         }
      }
      
      public String Title
      {
         get
         {
            return title;
         }
         set
         {
            title = value;
         }
      }
      
      public String Alias
      {
         get
         {
            return alias;
         }
         set
         {
            alias = value;
         }
      }
   }
   
   [ParseChildren(true, "Employees")]
   public class CollectionPropertyControl : Control
   {  
      private String header;
      private ArrayList employees = new ArrayList();
      
      public String Header
      {
         get
         {
            return header;
         }
         set
         {
            header = value;
         }
      }


      
      public ArrayList Employees
      {
         get 
         {
            return employees;
         }
      }

      protected override void CreateChildControls()
      {
         Label label = new Label();
         label.Text = Header;
         label.BackColor = System.Drawing.Color.Beige;
         label.ForeColor = System.Drawing.Color.Red;
         Controls.Add(label);
         Controls.Add(new LiteralControl("<BR> <BR>"));

         Table table = new Table();
         TableRow htr = new TableRow();

         TableHeaderCell hcell1 = new TableHeaderCell();    
         hcell1.Text = "Name";
         htr.Cells.Add(hcell1);

         TableHeaderCell hcell2 = new TableHeaderCell();
         hcell2.Text = "Title";
         htr.Cells.Add(hcell2);
         
         TableHeaderCell hcell3 = new TableHeaderCell();
         hcell3.Text = "Alias";
         htr.Cells.Add(hcell3);
         table.Rows.Add(htr);

         table.BorderWidth = 2;
         table.BackColor = System.Drawing.Color.Beige;
         table.ForeColor = System.Drawing.Color.Red;
         foreach (Employee employee in Employees)
         {
            TableRow tr = new TableRow();

            TableCell cell1 = new TableCell();
            cell1.Text = employee.Name;
            tr.Cells.Add(cell1);
            
            TableCell cell2 = new TableCell();
            cell2.Text = employee.Title;
            tr.Cells.Add(cell2);
            
            TableCell cell3 = new TableCell();
            cell3.Text = employee.Alias;
            tr.Cells.Add(cell3);
            
            table.Rows.Add(tr);
         }
         Controls.Add(table);
         
      }
   }
}
[Visual Basic]
Option Explicit
Option Strict

Imports System
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace CustomControls
   Public Class Employee
      Private _name As String
      Private _title As String
      Private _alias As String
      
      Public Sub New()
         MyClass.New("", "", "")
      End Sub
       
      Public Sub New(name As String, title As String, employeeAlias As String)
         Me._name = name
         Me._title = title
         Me._alias = employeeAlias
      End Sub
      
      Public Property Name() As String
         Get
            Return _name
         End Get
         Set
            _name = value
         End Set
      End Property
      
      
      Public Property Title() As String
         Get
            Return _title
         End Get
         Set
            _title = value
         End Set
      End Property
      
      
      Public Property [Alias]() As String
         Get
            Return _alias
         End Get
         Set
            _alias = value
         End Set
      End Property
   End Class
   
   <ParseChildren(True, "Employees")> _
   Public Class CollectionPropertyControl
      Inherits Control
      Private _header As String
      Private _employees As New ArrayList()
      
      Public Property Header() As String
         Get
            Return _header
         End Get
         Set
            _header = value
         End Set
      End Property
      
      Public ReadOnly Property Employees() As ArrayList
         Get
            Return _employees
         End Get
      End Property
      
      Protected Overrides Sub CreateChildControls()
         Dim label As New Label()
         label.Text = Header
         label.BackColor = System.Drawing.Color.Beige
         label.ForeColor = System.Drawing.Color.Red
         Controls.Add(label)
         Controls.Add(New LiteralControl("<BR> <BR>"))
         
         Dim table As New Table()
         Dim htr As New TableRow()
         
         Dim hcell1 As New TableHeaderCell()
         hcell1.Text = "Name"
         htr.Cells.Add(hcell1)
         
         Dim hcell2 As New TableHeaderCell()
         hcell2.Text = "Title"
         htr.Cells.Add(hcell2)
         
         Dim hcell3 As New TableHeaderCell()
         hcell3.Text = "Alias"
         htr.Cells.Add(hcell3)
         table.Rows.Add(htr)
         
         table.BorderWidth = Unit.Pixel(2)
         table.BackColor = System.Drawing.Color.Beige
         table.ForeColor = System.Drawing.Color.Red
         Dim employee As Employee
         For Each employee In  Employees
            Dim tr As New TableRow()
            
            Dim cell1 As New TableCell()
            cell1.Text = employee.Name
            tr.Cells.Add(cell1)
            
            Dim cell2 As New TableCell()
            cell2.Text = employee.Title
            tr.Cells.Add(cell2)
            
            Dim cell3 As New TableCell()
            cell3.Text = employee.Alias
            tr.Cells.Add(cell3)
            
            table.Rows.Add(tr)
         Next employee
         Controls.Add(table)
      End Sub 
   End Class
End Namespace

預設屬性控制項測試網頁

下列 ASP.NET 網頁將使用自訂控制項 CollectionPropertyControl 並說明如何以宣告式語法將項目加入至集合屬性。

<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<html>      
<body>         
<form runat=server>              
<Custom:CollectionPropertyControl Header = "Employees" id = "prop" runat = "server">
<Custom:Employee Name = "Alice" Alias = "AliceA" Title = "Manager" />
<Custom:Employee Name = "Jerry" Alias = "JerryR" Title = "Programmer" />
<Custom:Employee Name = "Lynn" Alias = "LynnP" Title = "Architect" />
<Custom:Employee Name = "Mike" Alias = "MikeB" Title = "Tester" />
</Custom:CollectionPropertyControl>                                     
</form>                       
</body>                    
</html>                 

請參閱

使用 ParseChildrenAttribute | 控制項剖析、ParseChildrenAttribute 和控制項產生器