ParseChildrenAttribute Klasa

Definicja

Definiuje atrybut metadanych, który można użyć podczas tworzenia kontrolek serwera ASP.NET. ParseChildrenAttribute Użyj klasy, aby wskazać, jak analizator strony powinien traktować zawartość zagnieżdżona wewnątrz tagu sterowania serwera zadeklarowanego na stronie. Klasa ta nie może być dziedziczona.

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
Dziedziczenie
ParseChildrenAttribute
Atrybuty

Przykłady

Przykład kodu w tej sekcji zawiera dwie części. W pierwszym przykładzie kodu pokazano, jak ustawić właściwości dla ParseChildrenAttribute klasy. Drugi przykład kodu pokazuje, jak używać klas na stronie ASP.NET.

W poniższym przykładzie kodu pokazano, jak ustawić ParseChildrenAttribute obiekt niestandardowej kontrolki serwera o nazwie CollectionPropertyControl. Właściwość ParseChildrenAttribute ustawia ChildrenAsProperties właściwość na true i DefaultProperty właściwość na klasę 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

W poniższym przykładzie kodu pokazano, jak używać CollectionPropertyControl klas i Employee na stronie ASP.NET. Employee Wystąpienia klasy są dodawane deklaratywnie.

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

Uwagi

Klasa ParseChildrenAttribute umożliwia określenie logiki analizowania dla niestandardowej kontrolki serwera przez oznaczenie kontrolki serwera za pomocą atrybutu ParseChildrenAttribute metadanych.

Oznaczanie kontrolki serwera za pomocą atrybutu ParseChildren(true) metadanych powoduje, że analizator interpretuje elementy zawarte w tagach kontrolki serwera jako właściwości. W tym scenariuszu ChildrenAsProperties właściwość to true.

Oznaczanie kontrolki serwera za pomocą atrybutu ParseChildren(true,"<Default Property>") metadata ustawia DefaultProperty właściwość na nazwę właściwości przekazanej do atrybutu.

Oznaczanie kontrolki serwera za pomocą atrybutu ParseChildren(false)metadanych , wartość domyślna, nakazuje analizatorowi interpretowanie elementów zawartych w tagach kontrolki serwera jako zawartości, która będzie analizowana ze skojarzonym ControlBuilder elementem, czyli jako kontrolkami. W tym scenariuszu ChildrenAsProperties właściwość to false.

Aby uzyskać informacje na temat używania atrybutów, zobacz Atrybuty.

Konstruktory

ParseChildrenAttribute()

Inicjuje nowe wystąpienie klasy ParseChildrenAttribute.

ParseChildrenAttribute(Boolean)

Inicjuje nowe wystąpienie ParseChildrenAttribute klasy przy użyciu właściwości w ChildrenAsProperties celu określenia, czy elementy zawarte w kontrolce serwera są analizowane jako właściwości kontrolki serwera.

ParseChildrenAttribute(Boolean, String)

Inicjuje ParseChildrenAttribute nowe wystąpienie klasy przy użyciu childrenAsProperties parametrów i defaultProperty .

ParseChildrenAttribute(Type)

Inicjuje nowe wystąpienie ParseChildrenAttribute klasy przy użyciu właściwości , ChildControlType aby określić, które elementy zawarte w kontrolce serwera są analizowane jako kontrolki.

Pola

Default

Definiuje wartość domyślną ParseChildrenAttribute klasy. To pole jest tylko do odczytu.

ParseAsChildren

Wskazuje, że zagnieżdżona zawartość zawarta w kontrolce serwera jest analizowana jako kontrolka.

ParseAsProperties

Wskazuje, że zagnieżdżona zawartość zawarta w kontrolce serwera jest analizowana jako właściwości kontrolki.

Właściwości

ChildControlType

Pobiera wartość wskazującą dozwolony typ kontrolki.

ChildrenAsProperties

Pobiera lub ustawia wartość wskazującą, czy przeanalizować elementy zawarte w kontrolce serwera jako właściwości.

DefaultProperty

Pobiera lub ustawia właściwość domyślną kontrolki serwera, w której elementy są analizowane.

TypeId

Po zaimplementowaniu w klasie pochodnej pobiera unikatowy identyfikator dla tego elementu Attribute.

(Odziedziczone po Attribute)

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

GetHashCode()

Służy jako funkcja skrótu ParseChildrenAttribute dla obiektu.

GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
IsDefaultAttribute()

Zwraca wartość wskazującą, czy wartość bieżącego ParseChildrenAttribute wystąpienia klasy jest wartością domyślną klasy pochodnej.

Match(Object)

W przypadku zastąpienia w klasie pochodnej zwraca wartość wskazującą, czy to wystąpienie jest równe określonemu obiektowi.

(Odziedziczone po Attribute)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

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

Zestaw nazw jest mapowany na odpowiedni zestaw identyfikatorów wysyłania.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Pobiera informacje o typie obiektu, którego można użyć do pobrania informacji o typie interfejsu.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Pobiera informację o liczbie typów interfejsów, jakie zawiera obiekt (0 lub 1).

(Odziedziczone po Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Umożliwia dostęp do właściwości i metod udostępnianych przez obiekt.

(Odziedziczone po Attribute)

Dotyczy

Zobacz też