PersistChildrenAttribute 类

定义

定义一个特性,该特性由 ASP.NET 服务器控件用于指示在设计时服务器控件中包含的嵌套内容是与控件对应,还是与服务器控件的属性对应。 此类不能被继承。

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
继承
PersistChildrenAttribute
属性

示例

本节中的代码示例包含两个部分。 第一个代码示例演示如何设置自定义控件的元数据,以便在设计时,其嵌套内容作为控件的属性持久保存。 第二个代码示例演示如何在 ASP.NET 页中使用类。

下面的代码示例演示如何应用 PersistChildrenAttribute 属性,以便自定义服务器控件的嵌套控件都不会保留为嵌套控件。 名为 的 CollectionPropertyControl 自定义服务器控件的 PersistChildrenAttribute 属性设置为 falseEmployee 以便添加的对象作为嵌套元素持久保存。

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 页中使用 CollectionPropertyControlEmployee 类。

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

注解

PersistChildrenAttributeParseChildrenAttribute 结合使用来确定如何解释控件的嵌套内容。 如果 PersistChildrenAttributetrueParseChildrenAttributefalse,则 ASP.NET 服务器控件中包含的嵌套内容将保留为控件。 如果 PersistChildrenAttributefalseParseChildrenAttributetrue,则嵌套内容将保留为服务器控件的属性。 有关使用特性的详细信息,请参阅 特性

构造函数

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)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

另请参阅