PersistChildrenAttribute Klasse

Definition

Definiert ein Attribut, das von ASP.NET-Serversteuerelementen verwendet wird, um zur Entwurfszeit anzugeben, ob die in einem Serversteuerelement enthaltenen, geschachtelten Inhalte Steuerelementen oder Eigenschaften des Serversteuerelements entsprechen. Diese Klasse kann nicht vererbt werden.

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
Vererbung
PersistChildrenAttribute
Attribute

Beispiele

Das Codebeispiel in diesem Abschnitt enthält zwei Teile. Im ersten Codebeispiel wird veranschaulicht, wie die Metadaten eines benutzerdefinierten Steuerelements so festgelegt werden, dass zur Entwurfszeit dessen geschachtelter Inhalt als Eigenschaften des Steuerelements beibehalten wird. Im zweiten Codebeispiel wird veranschaulicht, wie Klassen in einer ASP.NET Seite verwendet werden.

Im folgenden Codebeispiel wird veranschaulicht, wie das PersistChildrenAttribute Attribut angewendet wird, damit keines der geschachtelten Steuerelemente eines benutzerdefinierten Serversteuerelements als geschachtelte Steuerelemente beibehalten wird. Das benutzerdefinierte Serversteuerelement mit dem Namen CollectionPropertyControl hat das PersistChildrenAttribute Attribut auf false festgelegt, sodass die Employee hinzugefügten Objekte als geschachtelte Elemente beibehalten werden.

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

Im folgenden Codebeispiel wird veranschaulicht, wie die CollectionPropertyControl - und Employee -Klasse auf einer ASP.NET Seite verwendet wird.

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

Hinweise

Wird PersistChildrenAttribute in Kombination mit dem ParseChildrenAttribute verwendet, um zu bestimmen, wie geschachtelter Inhalt eines Steuerelements interpretiert wird. Wenn PersistChildrenAttribute und ParseChildrenAttribute isttrue, wird falseder in einem ASP.NET Serversteuerelement enthaltene geschachtelte Inhalt als Steuerelemente beibehalten. Wenn PersistChildrenAttribute und ParseChildrenAttribute ist falsetrue, wird der geschachtelte Inhalt als Eigenschaften des Serversteuerelements beibehalten. Weitere Informationen zur Verwendung von Attributen finden Sie unter Attribute.

Konstruktoren

PersistChildrenAttribute(Boolean)

Initialisiert eine neue Instanz der PersistChildrenAttribute-Klasse, wobei mithilfe eines booleschen Werts angegeben wird, ob geschachtelte Inhalte als geschachtelte Steuerelemente beibehalten werden sollen.

PersistChildrenAttribute(Boolean, Boolean)

Initialisiert eine neue Instanz der PersistChildrenAttribute-Klasse unter Verwendung von zwei booleschen Werten. Ein Wert gibt an, ob geschachtelte Inhalte als geschachtelte Steuerelemente beibehalten werden; der andere gibt an, ob eine benutzerdefinierte Beibehaltungsmethode verwendet wird.

Felder

Default

Gibt den standardmäßigen Attributzustand an. Das Default-Feld ist schreibgeschützt.

No

Gibt an, dass geschachtelte Inhalte zur Entwurfszeit nicht als geschachtelte Steuerelemente beibehalten werden sollen. Dieses Feld ist schreibgeschützt.

Yes

Gibt an, dass geschachtelte Inhalte zur Entwurfszeit als Steuerelemente beibehalten werden sollen. Das Yes-Feld ist schreibgeschützt.

Eigenschaften

Persist

Ruft einen Wert ab, der angibt, ob die geschachtelten Inhalte zur Entwurfszeit als geschachtelte Steuerelemente beibehalten werden.

TypeId

Ruft bei Implementierung in einer abgeleiteten Klasse einen eindeutigen Bezeichner für dieses Attribute ab.

(Geerbt von Attribute)
UsesCustomPersistence

Ruft einen Wert ab, der angibt, ob das Serversteuerelement zur Entwurfszeit eine benutzerdefinierte Beibehaltung von geschachtelten Steuerelementen bereitstellt.

Methoden

Equals(Object)

Bestimmt, ob das angegebene Objekt gleich dem aktuellen Objekt ist.

GetHashCode()

Fungiert als eine Hashfunktion für die PersistChildrenAttribute-Klasse.

GetType()

Ruft den Type der aktuellen Instanz ab.

(Geerbt von Object)
IsDefaultAttribute()

Gibt einen Wert zurück, der angibt, ob der Wert der aktuellen Instanz der PersistChildrenAttribute-Klasse der Standardwert der abgeleiteten Klasse ist.

Match(Object)

Beim Überschreiben in einer abgeleiteten Klasse wird ein Wert zurückgegeben, der angibt, ob diese Instanz einem bestimmten Objekt entspricht.

(Geerbt von Attribute)
MemberwiseClone()

Erstellt eine flache Kopie des aktuellen Object.

(Geerbt von Object)
ToString()

Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.

(Geerbt von Object)

Explizite Schnittstellenimplementierungen

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Ordnet eine Reihe von Namen einer entsprechenden Reihe von Dispatchbezeichnern zu.

(Geerbt von Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Ruft die Typinformationen für ein Objekt ab, mit deren Hilfe die Typinformationen für eine Schnittstelle abgerufen werden können.

(Geerbt von Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Ruft die Anzahl der Schnittstellen mit Typinformationen ab, die von einem Objekt bereitgestellt werden (0 oder 1).

(Geerbt von Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Stellt den Zugriff auf von einem Objekt verfügbar gemachte Eigenschaften und Methoden bereit.

(Geerbt von Attribute)

Gilt für:

Weitere Informationen