ParseChildrenAttribute Costruttori
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Inizializza una nuova istanza della classe ParseChildrenAttribute.
Overload
ParseChildrenAttribute() |
Inizializza una nuova istanza della classe ParseChildrenAttribute. |
ParseChildrenAttribute(Boolean) |
Inizializza una nuova istanza della classe ParseChildrenAttribute utilizzando la proprietà ChildrenAsProperties per determinare se gli elementi inclusi in un controllo server vengono analizzati come proprietà del controllo server. |
ParseChildrenAttribute(Type) |
Inizializza una nuova istanza della classe ParseChildrenAttribute utilizzando la proprietà ChildControlType per determinare quali elementi inclusi in un controllo server vengono analizzati come controlli. |
ParseChildrenAttribute(Boolean, String) |
Inizializza una nuova istanza della classe ParseChildrenAttribute utilizzando i parametri |
ParseChildrenAttribute()
Inizializza una nuova istanza della classe ParseChildrenAttribute.
public:
ParseChildrenAttribute();
public ParseChildrenAttribute ();
Public Sub New ()
Commenti
Viene creata una nuova istanza della ParseChildrenAttribute classe con la ChildrenAsProperties proprietà impostata su false
.
Vedi anche
Si applica a
ParseChildrenAttribute(Boolean)
Inizializza una nuova istanza della classe ParseChildrenAttribute utilizzando la proprietà ChildrenAsProperties per determinare se gli elementi inclusi in un controllo server vengono analizzati come proprietà del controllo server.
public:
ParseChildrenAttribute(bool childrenAsProperties);
public ParseChildrenAttribute (bool childrenAsProperties);
new System.Web.UI.ParseChildrenAttribute : bool -> System.Web.UI.ParseChildrenAttribute
Public Sub New (childrenAsProperties As Boolean)
Parametri
- childrenAsProperties
- Boolean
true
per analizzare gli elementi come proprietà del controllo server; in caso contrario, false
.
Esempio
L'esempio di codice in questa sezione contiene due parti. Il primo esempio di codice illustra come impostare le proprietà per la ParseChildrenAttribute classe. Il secondo esempio di codice illustra come usare le classi in una pagina di ASP.NET.
Nell'esempio di codice seguente viene illustrato come impostare l'oggetto ParseChildrenAttribute per un controllo server personalizzato denominato CollectionPropertyControl
. Quando l'oggetto ParseChildrenAttribute viene dichiarato prima della classe che definisce CollectionPropertyControl
, la ParseChildrenAttribute proprietà imposta su ChildrenAsProperties true
.
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
{
// 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 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 this Boolean version of the ParseChildrenAttribute constructor
// to set the ChildrenAsProperties property to true. Any properties of the
// the CollectionPropertyControl custom control will be used as parsable
// children.
[ParseChildren(true)]
[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
' 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(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 this Boolean version of the ParseChildrenAttribute constructor
' to set the ChildrenAsProperties property to true. Any properties of the
' the CollectionPropertyControl custom control will be used as parsable
' children.
<ParseChildren(True)> _
<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 ' ParseChildrenSampleVB_2
Nell'esempio di codice seguente viene illustrato come usare le CollectionPropertyControl
classi e Employee
in una pagina ASP.NET. Viene aggiunta un'istanza della Employee
classe dichiarativamente e un'altra viene aggiunta a livello di codice.
<%@ Page Language="C#" %>
<%@ 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)
{
// Create a new employee object and add it to the custom control.
Employee e1 = new Employee("Employee 2", "Title 2", "Alias 2");
CollectionPropertyControl1.Employees.Add(e1);
// Verify attribute values.
ParseChildrenAttribute p =
(ParseChildrenAttribute)Attribute.GetCustomAttribute(typeof(CollectionPropertyControl),
typeof(ParseChildrenAttribute));
StringBuilder sb = new StringBuilder();
sb.Append("The ChildControlType property is " + p.ChildControlType.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">
<Employees>
<AspSample:Employee Name="Employee 1"
Title="Title 1"
Alias="Alias 1" />
</Employees>
</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)
' Create a new employee object and add it to custom control.
Dim e1 As New Employee("Employee 2", "Title 2", "Alias 2")
CollectionPropertyControl1.Employees.Add(e1)
' Verify attribute values.
Dim p As ParseChildrenAttribute = _
Attribute.GetCustomAttribute(GetType(CollectionPropertyControl), _
GetType(ParseChildrenAttribute))
Dim sb As New StringBuilder()
sb.Append("The ChildControlType property is " & p.ChildControlType.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">
<Employees>
<AspSample:Employee Name="Employee 1"
Title="Title 1"
Alias="Alias 1" />
</Employees>
</AspSample:CollectionPropertyControl>
</div>
</form>
</body>
</html>
Commenti
Se childrenAsProperties
è false
, gli elementi contenuti nel controllo server vengono analizzati come controllo. false
è il valore predefinito per ParseChildrenAttribute.
Vedi anche
Si applica a
ParseChildrenAttribute(Type)
Inizializza una nuova istanza della classe ParseChildrenAttribute utilizzando la proprietà ChildControlType per determinare quali elementi inclusi in un controllo server vengono analizzati come controlli.
public:
ParseChildrenAttribute(Type ^ childControlType);
public ParseChildrenAttribute (Type childControlType);
new System.Web.UI.ParseChildrenAttribute : Type -> System.Web.UI.ParseChildrenAttribute
Public Sub New (childControlType As Type)
Parametri
- childControlType
- Type
Tipo di controllo da analizzare come proprietà.
Eccezioni
childControlType
è null
.
Vedi anche
Si applica a
ParseChildrenAttribute(Boolean, String)
Inizializza una nuova istanza della classe ParseChildrenAttribute utilizzando i parametri childrenAsProperties
e defaultProperty
.
public:
ParseChildrenAttribute(bool childrenAsProperties, System::String ^ defaultProperty);
public ParseChildrenAttribute (bool childrenAsProperties, string defaultProperty);
new System.Web.UI.ParseChildrenAttribute : bool * string -> System.Web.UI.ParseChildrenAttribute
Public Sub New (childrenAsProperties As Boolean, defaultProperty As String)
Parametri
- childrenAsProperties
- Boolean
true
per analizzare gli elementi come proprietà del controllo server; in caso contrario, false
.
- defaultProperty
- String
Stringa che definisce una proprietà Collection del controllo server in cui il contenuto annidato viene analizzato per impostazione predefinita.
Esempio
L'esempio di codice in questa sezione contiene due parti. Il primo esempio di codice illustra come impostare le proprietà per la ParseChildrenAttribute classe. Il secondo esempio di codice illustra come usare le classi in una pagina di ASP.NET.
Nell'esempio di codice seguente viene illustrato come impostare l'oggetto ParseChildrenAttribute di un controllo server personalizzato denominato CollectionPropertyControl
. Imposta ParseChildrenAttribute la ChildrenAsProperties proprietà su true
e la DefaultProperty proprietà sulla Employee
classe.
// 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);
}
}
' 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
Nell'esempio di codice seguente viene illustrato come usare le CollectionPropertyControl
classi e Employee
in una pagina ASP.NET.
<%@ 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>
Commenti
Se childrenAsProperties
è false
, gli elementi vengono analizzati come controlli.