ParseChildrenAttribute 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義您可以在開發 ASP.NET 伺服器控制項時使用的中繼資料 (Metadata) 屬性 (Attribute)。 請使用 ParseChildrenAttribute 類別,指出頁面剖析器對於頁面上宣告之伺服器控制項標記內巢狀內容的處理方式。 此類別無法獲得繼承。
public ref class ParseChildrenAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class)]
public sealed class ParseChildrenAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class)>]
type ParseChildrenAttribute = class
inherit Attribute
Public NotInheritable Class ParseChildrenAttribute
Inherits Attribute
- 繼承
- 屬性
範例
本節中的程式碼範例包含兩個部分。 第一個程式碼範例示範如何設定 類別的屬性 ParseChildrenAttribute 。 第二個程式碼範例示範如何在 ASP.NET 網頁中使用類別。
下列程式碼範例示範如何設定 ParseChildrenAttribute 名為 CollectionPropertyControl
的自訂伺服器控制項物件。 會將 ParseChildrenAttribute 屬性設定 ChildrenAsProperties 為 true
,並將 DefaultProperty 屬性設定為 Employee
類別。
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;
namespace Samples.AspNet.CS.Controls
{
// The child element class.
[AspNetHostingPermission(SecurityAction.Demand,
Level=AspNetHostingPermissionLevel.Minimal)]
public sealed 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;
}
}
}
// Use the ParseChildren attribute to set the ChildrenAsProperties
// and DefaultProperty properties. Using this constructor, the
// control parses all child controls as properties and must define
// a public property named Employees, which it declares as
// an ArrayList. Nested (child) elements must correspond to
// child elements of the Employees property or to other
// properties of the control.
[ParseChildren(true, "Employees")]
[AspNetHostingPermission(SecurityAction.Demand,
Level=AspNetHostingPermissionLevel.Minimal)]
public sealed 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;
}
}
// Override the CreateChildControls method to
// add child controls to the Employees property when this
// custom control is requested from a page.
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);
}
}
}
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Security.Permissions
Namespace Samples.AspNet.VB.Controls
' The child element class.
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class Employee
Private _name As String
Private _title As String
Private _alias As String
Public Sub New()
Me.New("", "", "")
End Sub
Public Sub New(ByVal name As String, ByVal title As String, ByVal 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(ByVal value As String)
_name = Value
End Set
End Property
Public Property Title() As String
Get
Return _title
End Get
Set(ByVal value As String)
_title = Value
End Set
End Property
Public Property [Alias]() As String
Get
Return _alias
End Get
Set(ByVal value As String)
_alias = Value
End Set
End Property
End Class
' Use the ParseChildren attribute to set the ChildrenAsProperties
' and DefaultProperty properties. Using this constructor, the
' control parses all child controls as properties and must define
' a public property named Employees, which it declares as
' an ArrayList. Nested (child) elements must correspond to
' child elements of the Employees property or to other
' properties of the control.
<ParseChildren(True, "Employees")> _
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class CollectionPropertyControl
Inherits Control
Private _header As String
Private _employees As New ArrayList()
Public Property Header() As String
Get
Return _header
End Get
Set(ByVal value As String)
_header = Value
End Set
End Property
Public ReadOnly Property Employees() As ArrayList
Get
Return _employees
End Get
End Property
' Override the CreateChildControls method to
' add child controls to the Employees property when this
' custom control is requested from a page.
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
和 Employee
類別。 類別的 Employee
實例會以宣告方式加入。
<%@ Page Language="C#" Debug="true" %>
<%@ Register TagPrefix="AspSample" Assembly="Samples.AspNet.CS.Controls" Namespace="Samples.AspNet.CS.Controls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
// Verify attribute values.
ParseChildrenAttribute p =
(ParseChildrenAttribute)Attribute.GetCustomAttribute(typeof(CollectionPropertyControl),
typeof(ParseChildrenAttribute));
StringBuilder sb = new StringBuilder();
sb.Append("The DefaultProperty property is " + p.DefaultProperty.ToString() + "<br />");
sb.Append("The ChildrenAsProperties property is " + p.ChildrenAsProperties.ToString() + "<br />");
sb.Append("The IsDefaultAttribute method returns " + p.IsDefaultAttribute().ToString());
Message.Text = sb.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ParseChildrenAttribute Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Message"
runat="server"/>
<AspSample:CollectionPropertyControl id="CollectionPropertyControl1"
runat="server">
<AspSample:Employee Name="Employee 1"
Title="Title 1"
Alias="Alias 1" />
<AspSample:Employee Name="Employee 2"
Title="Title 2"
Alias="Alias 2" />
</AspSample:CollectionPropertyControl>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Register TagPrefix="AspSample" Assembly="Samples.AspNet.VB.Controls" Namespace="Samples.AspNet.VB.Controls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' Verify attribute values.
Dim p As ParseChildrenAttribute = _
Attribute.GetCustomAttribute(GetType(CollectionPropertyControl), _
GetType(ParseChildrenAttribute))
Dim sb As New StringBuilder()
sb.Append("The DefaultProperty property is " & p.DefaultProperty.ToString() & "<br />")
sb.Append("The ChildrenAsProperties property is " & p.ChildrenAsProperties.ToString() & "<br />")
sb.Append("The IsDefaultAttribute method returns " & p.IsDefaultAttribute().ToString())
Message.Text = sb.ToString()
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>PersistChildrenAttribute</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<asp:Label ID="Message"
runat="server"/>
<AspSample:CollectionPropertyControl id="CollectionPropertyControl1"
runat="server">
<AspSample:Employee Name="Employee 1"
Title="Title 1"
Alias="Alias 1" />
<AspSample:Employee Name="Employee 2"
Title="Title 2"
Alias="Alias 2" />
</AspSample:CollectionPropertyControl>
</div>
</form>
</body>
</html>
備註
類別 ParseChildrenAttribute 可讓您使用中繼資料屬性標記伺服器控制項,以指定自訂伺服器控制項的 ParseChildrenAttribute 剖析邏輯。
使用中繼資料屬性 ParseChildren(true)
標記伺服器控制項會指示剖析器將伺服器控制項內含的專案解譯為屬性。 在此案例中 ChildrenAsProperties ,屬性為 true
。
使用中繼資料屬性 ParseChildren(true,"<Default Property>")
標記伺服器控制項會將 DefaultProperty 屬性設定為傳遞至 屬性的屬性名稱。
將伺服器控制項標示為中繼資料屬性 ParseChildren(false)
,預設值會指示剖析器將伺服器控制項標籤中所包含的專案解譯為內容,該元素將會與相關聯的 ControlBuilder 剖析為控制項。 在此案例中 ChildrenAsProperties ,屬性為 false
。
如需使用屬性的相關資訊,請參閱 屬性。
建構函式
ParseChildrenAttribute() |
初始化 ParseChildrenAttribute 類別的新執行個體。 |
ParseChildrenAttribute(Boolean) |
使用 ParseChildrenAttribute 屬性,初始化 ChildrenAsProperties 類別的新執行個體,以判斷伺服器控制項中所包含的項目是否剖析成伺服器控制項的屬性。 |
ParseChildrenAttribute(Boolean, String) |
使用 |
ParseChildrenAttribute(Type) |
使用 ParseChildrenAttribute 屬性,初始化 ChildControlType 類別的新執行個體,以判斷伺服器控制項中所包含的哪些項目剖析成控制項。 |
欄位
Default |
定義 ParseChildrenAttribute 類別的預設值。 此欄位為唯讀。 |
ParseAsChildren |
表示伺服器控制項中所包含的巢狀內容剖析成控制項。 |
ParseAsProperties |
表示伺服器控制項中所包含的巢狀內容剖析成控制項的屬性。 |
屬性
ChildControlType |
取得值,指出控制項允許的型別。 |
ChildrenAsProperties |
取得或設定值,指出是否將伺服器控制項中所包含的項目剖析成屬性。 |
DefaultProperty |
取得或設定項目剖析至的伺服器控制項的預設屬性。 |
TypeId |
在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。 (繼承來源 Attribute) |
方法
Equals(Object) |
判斷指定的物件是否等於目前的物件。 |
GetHashCode() |
做為 ParseChildrenAttribute 物件的雜湊函式。 |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
IsDefaultAttribute() |
傳回值,指出 ParseChildrenAttribute 類別目前執行個體的值是否為衍生類別的預設值。 |
Match(Object) |
在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。 (繼承來源 Attribute) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |
明確介面實作
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。 (繼承來源 Attribute) |
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
擷取物件的類型資訊,可以用來取得介面的類型資訊。 (繼承來源 Attribute) |
_Attribute.GetTypeInfoCount(UInt32) |
擷取物件提供的類型資訊介面數目 (0 或 1)。 (繼承來源 Attribute) |
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供物件所公開的屬性和方法的存取權。 (繼承來源 Attribute) |