Nota:
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
El control de ejemplo que se describe aquí muestra cómo aplicar ParseChildrenAttribute a un control para especificar una propiedad predeterminada y cómo este atributo simplifica la sintaxis declarativa para configurar la propiedad predeterminada. La propiedad predeterminada suele ser un tipo de colección y puede almacenar elementos de cualquier tipo de sistema o personalizado.
El control de ejemplo (que se describe más adelante en este tema) se marca de la siguiente manera:
[ParseChildren(true, "Employees")]
public class CollectionPropertyControl : Control {...}
Employees es una propiedad de tipo System.Collections.ArrayList y contiene elementos de un tipo personalizado (Employee).
Cuando el control se marca con ParseChildrenAttribute, la sintaxis declarativa para agregar objetos Employee a la propiedad Employees es la siguiente:
<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>
Observe que el nombre de la propiedad no se especifica dentro de las etiquetas del control.
<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 permite utilizar una sintaxis declarativa más sencilla para agregar elementos a una propiedad predeterminada. Cuando la propiedad predeterminada es un tipo de colección, el analizador de páginas crea elementos secundarios (especificados dentro de las etiquetas del control) y los agrega a la colección. Al final de este tema se incluye una página de ejemplo que muestra la sintaxis de uso.
A continuación se muestra el código para el control personalizado y para el tipo personalizado (de los elementos de colección). Para generar este ejemplo, vea las instrucciones de Ejemplos de controles de servidor.
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
Página de prueba para el control de propiedad predeterminada
En la siguiente página ASP.NET se utiliza el control personalizado CollectionPropertyControl y se muestra cómo agregar elementos a la propiedad de colección de manera declarativa.
<%@ 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>
Vea también
Utilizar ParseChildrenAttribute | Análisis de controles, ParseChildrenAttribute y generadores de controles