Condividi tramite


Esempio di ParseChildrenAttribute

Il controllo di esempio descritto indica come applicare ParseChildrenAttribute a un controllo per specificare una proprietà predefinita e il modo in cui questo attributo semplifica la sintassi dichiarativa per le impostazioni della proprietà predefinita. La proprietà predefinita è di norma un tipo di insieme e può contenere elementi di qualsiasi tipo di sistema o di tipo predefinito.

Il controllo di esempio (descritto in seguito in questo argomento) è contrassegnato come descritto di seguito:

[ParseChildren(true, "Employees")]
public class CollectionPropertyControl : Control {...} 

Employees è una proprietà di tipo System.Collections.ArrayList e contiene elementi di tipo predefinito (Employee).

Quando il controllo viene contrassegnato con ParseChildrenAttribute, la sintassi dichiarativa per l'aggiunta degli oggetti Employee alla proprietà Employees è la seguente:

<Custom:CollectionPropertyControl runat = "server">
<Custom:Employee Name = "Alice" Alias = "AliceA" Title = "Manager" />
<Custom:Employee Name = "Jerry" Alias = "JerryR" Title = "Programmer" />
<Custom:Employee Name = "Lynn" Alias = "LynnP" Title = "Architect" />
<Custom:Employee Name = "Mike" Alias = "MikeB" Title = "Tester" />
</Custom:CollectionPropertyControl> 

Si noti che il nome della proprietà non è specificato all'interno dei tag del controllo.

<Custom:CollectionPropertyControl runat = "server">
<%-- <Employees> not specified --%>
<Custom:Employee Name = "Alice" Alias = "AliceA" Title = "Manager" />
...
<Custom:Employee Name = "Mike" Alias = "MikeB" Title = "Tester" />
<%-- </Employees> not specified --%>
</Custom:CollectionPropertyControl>

ParseChildrenAttribute quindi consente una sintassi dichiarativa più semplice per l'aggiunta di elementi a una proprietà predefinita. Quando la proprietà predefinita è un tipo di insieme, il parser di pagina crea elementi figlio (specificati all'interno dei tag del controllo) e li aggiunge all'insieme. Al termine di questo argomento viene fornita una pagina di esempio in cui viene descritta la sintassi di utilizzo.

Segue il codice per il controllo personalizzato e per il tipo predefinito (degli elementi di insieme). Per generare questo esempio, vedere le istruzioni riportate in Esempi di controlli server.

using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomControls
{
   public 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;
         }
      }
   }
   
   [ParseChildren(true, "Employees")]
   public 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;
         }
      }

      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);
         
      }
   }
}
[Visual Basic]
Option Explicit
Option Strict

Imports System
Imports System.Collections
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace CustomControls
   Public Class Employee
      Private _name As String
      Private _title As String
      Private _alias As String
      
      Public Sub New()
         MyClass.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
   
   <ParseChildren(True, "Employees")> _
   Public 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
      
      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

Pagina di prova per il controllo di proprietà predefinito

Sulla pagina ASP.NET seguente è utilizzato il controllo personalizzato CollectionPropertyControl e viene descritto come aggiungere elementi alla proprietà dell'insieme in modo dichiarativo.

<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<html>      
<body>         
<form runat=server>              
<Custom:CollectionPropertyControl Header = "Employees" id = "prop" runat = "server">
<Custom:Employee Name = "Alice" Alias = "AliceA" Title = "Manager" />
<Custom:Employee Name = "Jerry" Alias = "JerryR" Title = "Programmer" />
<Custom:Employee Name = "Lynn" Alias = "LynnP" Title = "Architect" />
<Custom:Employee Name = "Mike" Alias = "MikeB" Title = "Tester" />
</Custom:CollectionPropertyControl>                                     
</form>                       
</body>                    
</html>                 

Vedere anche

Utilizzo di ParseChildrenAttribute | Analisi dei controlli, ParseChildrenAttribute e generatori di controlli