TreeNodeBindingCollection クラス
アセンブリ: System.Web (system.web.dll 内)
構文
解説TreeNodeBindingCollection クラスは、TreeView コントロールに存在する TreeNodeBinding オブジェクトのコレクションを格納および管理する場合に使用されます。TreeView コントロールは、その DataBindings プロパティに TreeNodeBindingCollection クラスを使用します。
DataBindings プロパティには、データ項目とそのバインド先のノードとの間の関係を定義する TreeNodeBinding オブジェクトが含まれています。複数の属性を持つ XML 要素などのように、各データ項目に複数のプロパティが含まれているデータ ソースにバインドする場合、ノードにはデータ項目の ToString メソッドから返される値が既定で表示されます。XML 要素の場合、ノードには要素名が表示されます。これはメニュー ツリーの基になる構造体を示す以外はあまり用途はありません。ツリー ノードのバインディングを指定することにより、ノードを特定のデータ項目のプロパティにバインドできます。DataBindings コレクションはプログラムにより設定できますが、通常は宣言によって設定されます。
ツリー ノード バインディングを宣言して設定するには、次のように操作します。
-
TreeView コントロールの開始タグと終了タグの間で <DataBindings> の開始タグと終了タグを入れ子にします。
-
指定するツリー ノード バインディングごとに、<DataBindings> の開始タグと終了タグの間に <asp:TreeNodeBinding> 要素を入れます。
TreeNodeBinding オブジェクトを追加および削除することにより、TreeNodeBindingCollection オブジェクトをプログラムによって管理できます。TreeNodeBinding オブジェクトをコレクションに追加するには、Add メソッドまたは Insert メソッドを使用します。コレクションからノードを削除するには、Remove、RemoveAt、または StateManagedCollection.Clear の各メソッドを使用します。
TreeNodeBindingCollection クラスは、コレクション内の項目にアクセスするための複数の方法をサポートしています。
使用例このセクションには、2 つのコード例が含まれています。最初のコード例では、宣言によって TreeNodeBindingCollection オブジェクトに値を代入する方法を示します。2 番目のコード例では、プログラムによって TreeNodeBindingCollection オブジェクトに値を代入する方法を示します。
宣言によって TreeNodeBindingCollection オブジェクトに値を代入するコード例を次に示します。この例を正しく実行するには、このセクションの末尾にある XML データを Book.xml ファイルにコピーする必要があります。
<%@ Page Language="VB" %> <html> <body> <form runat="server"> <h3>TreeView XML Data Binding Example</h3> <asp:TreeView id="BookTreeView" DataSourceID=BookXmlDataSource runat="server"> <DataBindings> <asp:TreeNodeBinding DataMember="Book" TextField="Title"/> <asp:TreeNodeBinding DataMember="Chapter" TextField="Heading"/> <asp:TreeNodeBinding DataMember="Section" TextField="Heading"/> </DataBindings> </asp:TreeView> <asp:XmlDataSource id="BookXmlDataSource" DataFile="Book.xml" runat="server"> </asp:XmlDataSource> </form> </body> </html>
<%@ Page Language="C#" %> <html> <body> <form runat="server"> <h3>TreeView XML Data Binding Example</h3> <asp:TreeView id="BookTreeView" DataSourceID=BookXmlDataSource runat="server"> <DataBindings> <asp:TreeNodeBinding DataMember="Book" TextField="Title"/> <asp:TreeNodeBinding DataMember="Chapter" TextField="Heading"/> <asp:TreeNodeBinding DataMember="Section" TextField="Heading"/> </DataBindings> </asp:TreeView> <asp:XmlDataSource id="BookXmlDataSource" DataFile="Book.xml" runat="server"> </asp:XmlDataSource> </form> </body> </html>
プログラムによって TreeNodeBindingCollection オブジェクトに値を代入するコード例を次に示します。この例を正しく実行するには、このセクションの末尾にあるサンプル XML データを Book.xml ファイルにコピーする必要があります。
<%@ Page Language="VB" %> <script runat="server"> Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) ' Create a new TreeView control. Dim NewTree As New TreeView ' Set the properties of the TreeView control. NewTree.ID = "BookTreeView" NewTree.DataSourceID = "BookXmlDataSource" ' Create the tree node binding relationship. ' Create the root node binding. Dim RootBinding As New TreeNodeBinding RootBinding.DataMember = "Book" RootBinding.TextField = "Title" ' Create the parent node binding. Dim ParentBinding As New TreeNodeBinding ParentBinding.DataMember = "Chapter" ParentBinding.TextField = "Heading" ' Create the leaf node binding. Dim LeafBinding As New TreeNodeBinding LeafBinding.DataMember = "Section" LeafBinding.TextField = "Heading" ' Add bindings to the DataBindings collection. NewTree.DataBindings.Add(RootBinding) NewTree.DataBindings.Add(ParentBinding) NewTree.DataBindings.Add(LeafBinding) ' Manually register the event handler for the SelectedNodeChanged event. AddHandler NewTree.SelectedNodeChanged, AddressOf Node_Change ' Add the TreeView control to the Controls collection of the PlaceHolder control. ControlPlaceHolder.Controls.Add(NewTree) End Sub Sub Node_Change(ByVal sender As Object, ByVal e As EventArgs) ' Retrieve the TreeView control from the Controls collection of the PlaceHolder control. Dim LocalTree As TreeView = CType(ControlPlaceHolder.FindControl("BookTreeView"), TreeView) ' Display the selected node. Message.Text = "You selected: " & LocalTree.SelectedNode.Text End Sub </script> <html> <body> <form runat="server"> <h3>TreeView Constructor Example</h3> <asp:PlaceHolder id="ControlPlaceHolder" runat="server"> </asp:PlaceHolder> <asp:XmlDataSource id="BookXmlDataSource" DataFile="Book.xml" runat="server"> </asp:XmlDataSource> <br><br> <asp:Label id="Message" runat="server"/> </form> </body> </html>
<%@ Page Language="C#" %> <script runat="server"> void Page_Load(Object sender, EventArgs e) { // Create a new TreeView control. TreeView NewTree = new TreeView(); // Set the properties of the TreeView control. NewTree.ID = "BookTreeView"; NewTree.DataSourceID = "BookXmlDataSource"; // Create the tree node binding relationship. // Create the root node binding. TreeNodeBinding RootBinding = new TreeNodeBinding(); RootBinding.DataMember = "Book"; RootBinding.TextField = "Title"; // Create the parent node binding. TreeNodeBinding ParentBinding = new TreeNodeBinding(); ParentBinding.DataMember = "Chapter"; ParentBinding.TextField = "Heading"; // Create the leaf node binding. TreeNodeBinding LeafBinding = new TreeNodeBinding(); LeafBinding.DataMember = "Section"; LeafBinding.TextField = "Heading"; // Add bindings to the DataBindings collection. NewTree.DataBindings.Add(RootBinding); NewTree.DataBindings.Add(ParentBinding); NewTree.DataBindings.Add(LeafBinding); // Manually register the event handler for the SelectedNodeChanged event. NewTree.SelectedNodeChanged += new EventHandler(this.Node_Change); // Add the TreeView control to the Controls collection of the PlaceHolder control. ControlPlaceHolder.Controls.Add(NewTree); } void Node_Change(Object sender, EventArgs e) { // Retrieve the TreeView control from the Controls collection of the PlaceHolder control. TreeView LocalTree = (TreeView)ControlPlaceHolder.FindControl("BookTreeView"); // Display the selected node. Message.Text = "You selected: " + LocalTree.SelectedNode.Text; } </script> <html> <body> <form runat="server"> <h3>TreeView Constructor Example</h3> <asp:PlaceHolder id="ControlPlaceHolder" runat="server"> </asp:PlaceHolder> <asp:XmlDataSource id="BookXmlDataSource" DataFile="Book.xml" runat="server"> </asp:XmlDataSource> <br><br> <asp:Label id="Message" runat="server"/> </form> </body> </html>
継承階層System.Web.UI.StateManagedCollection
System.Web.UI.WebControls.TreeNodeBindingCollection
スレッド セーフ
プラットフォームWindows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
参照Weblioに収録されているすべての辞書からTreeNodeBindingCollection クラスを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からTreeNodeBindingCollection クラス
を検索
- TreeNodeBindingCollection クラスのページへのリンク