ParseChildrenAttribute Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menentukan atribut metadata yang dapat Anda gunakan saat mengembangkan kontrol server ASP.NET. Gunakan kelas ParseChildrenAttribute untuk menunjukkan bagaimana pengurai halaman harus memperlakukan konten yang ditumpuk di dalam tag kontrol server yang dideklarasikan pada halaman. Kelas ini tidak dapat diwariskan.
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
- Warisan
- Atribut
Contoh
Contoh kode di bagian ini berisi dua bagian. Contoh kode pertama menunjukkan cara mengatur properti untuk ParseChildrenAttribute kelas . Contoh kode kedua menunjukkan cara menggunakan kelas di halaman ASP.NET.
Contoh kode berikut menunjukkan cara mengatur ParseChildrenAttribute objek kontrol server kustom bernama CollectionPropertyControl. Mengatur ParseChildrenAttribute properti ke ChildrenAsPropertiestrue dan DefaultProperty properti ke Employee kelas .
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
Contoh kode berikut menunjukkan cara menggunakan CollectionPropertyControl kelas dan Employee di halaman ASP.NET. Instans Employee kelas ditambahkan secara deklaratif.
<%@ 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>
Keterangan
Kelas ini ParseChildrenAttribute memungkinkan Anda menentukan logika penguraian untuk kontrol server kustom dengan menandai kontrol server dengan ParseChildrenAttribute atribut metadata.
Menandai kontrol server Anda dengan atribut ParseChildren(true) metadata menginstruksikan pengurai untuk menginterpretasikan elemen yang terkandung dalam tag kontrol server sebagai properti. Dalam skenario ini, ChildrenAsProperties propertinya adalah true.
Menandai kontrol server Anda dengan atribut ParseChildren(true,"<Default Property>") metadata mengatur DefaultProperty properti ke nama properti yang diteruskan ke atribut .
Menandai kontrol server Anda dengan atribut ParseChildren(false)metadata , nilai default, menginstruksikan pengurai untuk menginterpretasikan elemen yang terkandung ControlBuilder dalam tag kontrol server sebagai konten yang akan diurai dengan yang terkait, sebagai kontrol. Dalam skenario ini, ChildrenAsProperties propertinya adalah false.
Untuk informasi tentang menggunakan atribut, lihat Atribut.
Konstruktor
| Nama | Deskripsi |
|---|---|
| ParseChildrenAttribute() |
Menginisialisasi instans baru dari kelas ParseChildrenAttribute. |
| ParseChildrenAttribute(Boolean, String) |
Menginisialisasi instans ParseChildrenAttribute baru kelas menggunakan |
| ParseChildrenAttribute(Boolean) |
Menginisialisasi instans ParseChildrenAttribute baru kelas menggunakan ChildrenAsProperties properti untuk menentukan apakah elemen yang terkandung dalam kontrol server diurai sebagai properti kontrol server. |
| ParseChildrenAttribute(Type) |
Menginisialisasi instans ParseChildrenAttribute baru kelas menggunakan ChildControlType properti untuk menentukan elemen mana yang terkandung dalam kontrol server yang diurai sebagai kontrol. |
Bidang
| Nama | Deskripsi |
|---|---|
| Default |
Menentukan nilai default untuk ParseChildrenAttribute kelas . Bidang ini hanya dapat dibaca. |
| ParseAsChildren |
Menunjukkan bahwa konten berlapis yang terkandung dalam kontrol server diurai sebagai kontrol. |
| ParseAsProperties |
Menunjukkan bahwa konten berlapis yang terkandung dalam kontrol server diurai sebagai properti kontrol. |
Properti
| Nama | Deskripsi |
|---|---|
| ChildControlType |
Mendapatkan nilai yang menunjukkan jenis kontrol yang diizinkan. |
| ChildrenAsProperties |
Mendapatkan atau menetapkan nilai yang menunjukkan apakah akan mengurai elemen yang terkandung dalam kontrol server sebagai properti. |
| DefaultProperty |
Mendapatkan atau mengatur properti default untuk kontrol server tempat elemen diurai. |
| TypeId |
Ketika diimplementasikan dalam kelas turunan, mendapatkan pengidentifikasi unik untuk Attributeini. (Diperoleh dari Attribute) |
Metode
| Nama | Deskripsi |
|---|---|
| Equals(Object) |
Menentukan apakah objek yang ditentukan sama dengan objek saat ini. |
| GetHashCode() |
Berfungsi sebagai fungsi hash untuk ParseChildrenAttribute objek . |
| GetType() |
Mendapatkan Type instans saat ini. (Diperoleh dari Object) |
| IsDefaultAttribute() |
Mengembalikan nilai yang menunjukkan apakah nilai instans ParseChildrenAttribute kelas saat ini adalah nilai default dari kelas turunan. |
| Match(Object) |
Saat ditimpa dalam kelas turunan, mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu. (Diperoleh dari Attribute) |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| ToString() |
Mengembalikan string yang mewakili objek saat ini. (Diperoleh dari Object) |
Implementasi Antarmuka Eksplisit
| Nama | Deskripsi |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Memetakan sekumpulan nama ke sekumpulan pengidentifikasi pengiriman yang sesuai. (Diperoleh dari Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Mengambil informasi jenis untuk objek, yang dapat digunakan untuk mendapatkan informasi jenis untuk antarmuka. (Diperoleh dari Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Mengambil jumlah antarmuka informasi jenis yang disediakan objek (baik 0 atau 1). (Diperoleh dari Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Menyediakan akses ke properti dan metode yang diekspos oleh objek. (Diperoleh dari Attribute) |