PersistChildrenAttribute 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義 ASP.NET 伺服器控制項使用的屬性 (Attribute),指出設計階段時伺服器控制項中包含的巢狀內容對應至控制項,或是伺服器控制項的屬性 (Property)。 此類別無法獲得繼承。
public ref class PersistChildrenAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class)]
public sealed class PersistChildrenAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class)>]
type PersistChildrenAttribute = class
inherit Attribute
Public NotInheritable Class PersistChildrenAttribute
Inherits Attribute
- 繼承
- 屬性
範例
本節中的程式代碼範例包含兩個部分。 第一個程式代碼範例示範如何設定自定義控件的元數據,以便在設計時間將其巢狀內容保存為控件的屬性。 第二個程式代碼範例示範如何在 ASP.NET 頁面中使用類別。
下列程式代碼範例示範如何套用 PersistChildrenAttribute 屬性,讓自定義伺服器控件的巢狀控件不會保存為巢狀控件。 名為 CollectionPropertyControl
的自定義伺服器控制項已 PersistChildrenAttribute 將 屬性設定 false
為 , Employee
以便新增的物件會保存為巢狀專案。
using System;
using System.Collections;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;
namespace PersistChildrenSamples
{
// 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 PersistChildren attribute to set the Persist
// property to false so that none of this class's
// child controls will be persisted as controls. They will
// be persisted only as child elements of this class.
// If you set the PersistChildren attribute to true, or if you
// do not include this attribute when you create a control,
// the child controls will be persisted as controls.
[PersistChildren(false)]
[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 = Color.Beige;
label.ForeColor = 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 = Color.Beige;
table.ForeColor = 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);
}
}
}
' Create a namespace that defines two classes, one a custom control, Employee,
' which is created for every instance of a child element with its name
' declared in a page associated with this namespace, the other, Employees,
' which contains these child elements.
Imports System.Collections
Imports System.Drawing
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Security.Permissions
Namespace PersistChildrenSampleVB
' Create a class that will be rendered as a child of the control
' that has the ParseChildren attribute applied to it.
<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(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
' Use the PersistChildren attribute to set the Persist
' property to false so that none of this class's
' child controls will be persisted as controls. They will
' be persisted only as child elements of this class.
' If you set the PersistChildren attribute to true, or if you
' do not include this attribute when you create a control,
' the child controls will be persisted as controls.
<PersistChildren(False)> _
<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
_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 = Color.Beige
label.ForeColor = 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 = Color.Beige
table.ForeColor = 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 ' PersistChildrenSampleVB
下列程式代碼範例示範如何在 ASP.NET 頁面中使用 CollectionPropertyControl
和 Employee
類別。
<%@ Page Language="C#" %>
<%@ Register TagPrefix="AspSample" Assembly="Samples.AspNet.CS.Controls" Namespace="PersistChildrenSamples" %>
<!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)
{
// Create two new employees and add them to the custom control.
Employee e1 = new Employee("Employee 1", "Title 1", "Alias 1");
Employee e2 = new Employee("Employee 2", "Title 2", "Alias 2");
CollectionPropertyControl1.Employees.Add(e1);
CollectionPropertyControl1.Employees.Add(e2);
// Verify attribute values.
PersistChildrenAttribute p =
(PersistChildrenAttribute)Attribute.GetCustomAttribute(typeof(CollectionPropertyControl),
typeof(PersistChildrenAttribute));
StringBuilder sb = new StringBuilder();
sb.Append("The Persist property is " + p.Persist.ToString() + "<br />");
sb.Append("The UseCustomPersistence property is " + p.UsesCustomPersistence.ToString() + "<br />");
sb.Append("The IsDefault method returns " + p.IsDefaultAttribute().ToString());
Message.Text = sb.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head 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:CollectionPropertyControl>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Register TagPrefix="AspSample" Assembly="Samples.AspNet.VB.Controls" Namespace="PersistChildrenSampleVB" %>
<!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)
' Create two new employees and add them to the custom control.
Dim e1 As New Employee("Employee 1", "Title 1", "Alias 1")
Dim e2 As New Employee("Employee 2", "Title 2", "Alias 2")
CollectionPropertyControl1.Employees.Add(e1)
CollectionPropertyControl1.Employees.Add(e2)
' Verify attribute values.
Dim p As PersistChildrenAttribute = _
Attribute.GetCustomAttribute(GetType(CollectionPropertyControl), _
GetType(PersistChildrenAttribute))
Dim sb As New StringBuilder()
sb.Append("The Persist property is " & p.Persist.ToString() & "<br />")
sb.Append("The UseCustomPersistence property is " & p.UsesCustomPersistence.ToString() & "<br />")
sb.Append("The IsDefault 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:CollectionPropertyControl>
</div>
</form>
</body>
</html>
備註
PersistChildrenAttribute會搭配 ParseChildrenAttribute 使用 ,以判斷控件的巢狀內容如何解譯。 如果 PersistChildrenAttribute 是 且 ParseChildrenAttribute 為 false
true
,則 ASP.NET 伺服器控制項中包含的巢狀內容會保存為控件。 如果 PersistChildrenAttribute 是 false
且 ParseChildrenAttribute 為 true
,則巢狀內容會保存為伺服器控件的屬性。 如需使用屬性的詳細資訊,請參閱 屬性。
建構函式
PersistChildrenAttribute(Boolean) |
使用布林值指出是否將巢狀內容保存為巢狀控制項,初始化 PersistChildrenAttribute 類別的新執行個體。 |
PersistChildrenAttribute(Boolean, Boolean) |
使用兩個布林值,初始化 PersistChildrenAttribute 類別的新執行個體。 一個指出是否將巢狀內容保存為巢狀控制項,另一個指出是否使用自訂持續性方法。 |
欄位
Default |
指示預設的屬性狀態。 Default 欄位是唯讀的。 |
No |
表示設計階段時巢狀內容不應保存為巢狀控制項。 此欄位為唯讀。 |
Yes |
表示設計階段時巢狀內容應保存為控制項。 Yes 欄位是唯讀的。 |
屬性
Persist |
取得值,指出設計階段時巢狀內容是否保存為巢狀控制項。 |
TypeId |
在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。 (繼承來源 Attribute) |
UsesCustomPersistence |
取得值,指出設計階段時伺服器控制項是否提供巢狀控制項的自訂持續性。 |
方法
Equals(Object) |
判斷指定的物件是否等於目前的物件。 |
GetHashCode() |
做為 PersistChildrenAttribute 類別的雜湊函式。 |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
IsDefaultAttribute() |
傳回值,指出 PersistChildrenAttribute 類別目前執行個體的值是否為衍生類別的預設值。 |
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) |