TreeNode.Checked Properti
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.
Mendapatkan atau mengatur nilai yang menunjukkan apakah kotak centang simpul dipilih.
public:
property bool Checked { bool get(); void set(bool value); };
public bool Checked { get; set; }
member this.Checked : bool with get, set
Public Property Checked As Boolean
Nilai Properti
true jika kotak centang simpul dipilih; jika tidak, false. Defaultnya adalah false.
Contoh
Contoh kode berikut menunjukkan cara menggunakan Checked properti untuk menentukan apakah kotak centang simpul dipilih. Ini menginisialisasi kotak centang simpul dengan kedalaman satu ke status yang dipilih. Agar contoh ini berfungsi dengan benar, Anda harus menyalin data XML sampel di bawah ini ke file bernama Newsgroup.xml.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Data_Bound(Object sender, TreeNodeEventArgs e)
{
// Check the depth of a node as it is being bound to data.
// Initialize the Checked property to true if the depth is 1.
if(e.Node.Depth == 1)
{
e.Node.Checked = true;
}
else
{
e.Node.Checked = false;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TreeNode Checked Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeNode Checked Example</h3>
<asp:TreeView id="NewsgroupTreeView"
DataSourceID="NewsgroupXmlDataSource"
OnTreeNodeDataBound="Data_Bound"
ShowCheckBoxes="All"
ExpandDepth="2"
runat="server">
<DataBindings>
<asp:TreeNodeBinding DataMember="category" TextField="Name"/>
<asp:TreeNodeBinding DataMember="group" TextField="Name"/>
</DataBindings>
</asp:TreeView>
<asp:XmlDataSource id="NewsgroupXmlDataSource"
DataFile="Newsgroup.xml"
runat="server">
</asp:XmlDataSource>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub Data_Bound(ByVal sender As Object, ByVal e As TreeNodeEventArgs)
' Check the depth of a node as it is being bound to data.
' Initialize the Checked property to true if the depth is 1.
If e.Node.Depth = 1 Then
e.Node.Checked = True
Else
e.Node.Checked = False
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TreeNode Checked Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>TreeNode Checked Example</h3>
<asp:TreeView id="NewsgroupTreeView"
DataSourceID="NewsgroupXmlDataSource"
OnTreeNodeDataBound="Data_Bound"
ShowCheckBoxes="All"
ExpandDepth="2"
runat="server">
<DataBindings>
<asp:TreeNodeBinding DataMember="category" TextField="Name"/>
<asp:TreeNodeBinding DataMember="group" TextField="Name"/>
</DataBindings>
</asp:TreeView>
<asp:XmlDataSource id="NewsgroupXmlDataSource"
DataFile="Newsgroup.xml"
runat="server">
</asp:XmlDataSource>
</form>
</body>
</html>
Kode berikut adalah contoh data XML untuk contoh sebelumnya.
<category name="news.microsoft.com">
<group name="microsoft.public.dotnet.framework.aspnet"/>
<group name="microsoft.public.dotnet.framework.aspnet.mobile"/>
<group name="microsoft.public.dotnet.framework.aspnet.webservices"/>
</category>
Keterangan
Saat simpul menampilkan kotak centang, Checked properti biasanya digunakan untuk menentukan apakah kotak centang dipilih. Ketika kotak centang yang terkait dengan simpul dipilih, simpul secara otomatis ditambahkan ke CheckedNodes kumpulan TreeView kontrol. Checked Properti juga dapat digunakan untuk menentukan apakah kotak centang dipilih.
Nota
Lebih umum untuk menentukan simpul mana dalam TreeView kontrol yang memiliki kotak centang yang dipilih dengan melakukan iterasi melalui CheckedNodes koleksi.
Nilai properti ini disimpan dalam status tampilan.