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 によって、既定のプロパティに項目を追加するための宣言構文がより簡単になります。既定のプロパティがコレクション タイプの場合、ページ パーサーは、(コントロールのタグ内で指定されている) 子要素を作成してコレクションに追加します。構文の使用法を示すサンプル ページがトピックの最後に提供されています。
カスタム コントロールと (コレクション項目の) カスタム タイプのコードも示されています。このサンプルをビルドするには、「サーバー コントロールのサンプル」の手順を参照してください。
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、およびコントロール ビルダ