HierarchicalDataBoundControl クラスとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > HierarchicalDataBoundControl クラスの意味・解説 

HierarchicalDataBoundControl クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

データ階層形式で表示するすべての ASP.NET Version 2.0 データ バインド コントロール基本クラスとして機能します

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public MustInherit Class
 HierarchicalDataBoundControl
    Inherits BaseDataBoundControl
Dim instance As HierarchicalDataBoundControl
public abstract class HierarchicalDataBoundControl
 : BaseDataBoundControl
public ref class HierarchicalDataBoundControl
 abstract : public BaseDataBoundControl
public abstract class HierarchicalDataBoundControl
 extends BaseDataBoundControl
public abstract class HierarchicalDataBoundControl
 extends BaseDataBoundControl
解説解説

HierarchicalDataBoundControl クラスは、ASP.NET データ ソース コントロールから階層形式のデータ取得しコントロールユーザー インターフェイス要素をそのデータバインドして表示する ASP.NET コントロール使用される基本クラスです。TreeView クラスおよび Menu クラスは、HierarchicalDataBoundControl から派生します。

ページ開発者は、HierarchicalDataBoundControl クラス直接使用せずに、このクラスから派生するコントロール使用します

コントロール開発者はこのクラス拡張して、IHierarchicalDataSource インターフェイス実装するクラス、および HierarchicalDataSourceControl クラスと HierarchicalDataSourceView クラスから派生するクラス機能するデータ バインド コントロール作成しますHierarchicalDataBoundControl クラスから派生クラス作成する場合、PerformDataBinding メソッドオーバーライドして、コントロールユーザー インターフェイス要素を GetData メソッド取得したデータバインドます。ほとんどの場合派生クラスオーバーライドするメソッドは、PerformDataBinding メソッドだけです。

ASP.NET 2.0 データ バインド コントロール場合、PerformSelect メソッドは DataBind メソッド等価で、実行時データバインドするために呼び出されます。PerformSelect メソッドは、GetData メソッドPerformDataBinding メソッド呼び出します。

使用例使用例

HierarchicalDataBoundControl クラスから派生クラス作成してカスタムデータ バインド コントロール作成する方法次のコード例示しますGeneologyTree コントロールは、HTMLpre セクションを、関連付けられたデータ ソース コントロールから取得したデータテキスト ツリー描画ます。

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Samples.AspNet.VB.Controls

    <AspNetHostingPermission(SecurityAction.Demand, _
      Level:=AspNetHostingPermissionLevel.Minimal), _
      AspNetHostingPermission(SecurityAction.InheritanceDemand, _
      Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public Class GeneologyTree
        Inherits HierarchicalDataBoundControl

        Dim MaxDepth As Integer
 = 0

        Private aRootNode As TreeNode
        Public ReadOnly Property
 RootNode() As TreeNode
            Get
                If aRootNode Is Nothing
 Then
                    aRootNode = New TreeNode(String.Empty)
                End If
                Return aRootNode
            End Get
        End Property

        Private alNodes As ArrayList
        Public ReadOnly Property
 Nodes() As ArrayList
            Get
                If alNodes Is Nothing
 Then
                    alNodes = New ArrayList()
                End If
                Return alNodes
            End Get
        End Property

        Public Property DataTextField() As
 String
            Get
                Dim o As Object
 = ViewState("DataTextField")
                If o Is Nothing
 Then
                    Return String.Empty
                Else
                    Return CStr(o)
                End If
            End Get
            Set(ByVal value As
 String)
                ViewState("DataTextField") = value
                If Initialized Then
                    OnDataPropertyChanged()
                End If
            End Set
        End Property

        Protected Overrides Sub
 PerformDataBinding()
            MyBase.PerformDataBinding()

            ' Do not attempt to bind data if there is no
            ' data source set.
            If Not IsBoundUsingDataSourceID
 AndAlso DataSource Is Nothing
 Then
                Return
            End If

            Dim view As HierarchicalDataSourceView
 = GetData(RootNode.DataPath)

            If view Is Nothing
 Then
                Throw New InvalidOperationException
 _
                ("No view returned by data source control.")
            End If

            Dim enumerable As IHierarchicalEnumerable
 = view.Select()
            If Not (enumerable Is
 Nothing) Then

                Nodes.Clear()

                Try
                    RecurseDataBindInternal(RootNode, enumerable, 1)
                Finally
                End Try
            End If

        End Sub ' PerformDataBinding

        Private Sub RecurseDataBindInternal(ByVal
 node As TreeNode, _
            ByVal enumerable As IHierarchicalEnumerable,
 _
            ByVal depth As Integer)

            Dim item As Object
            For Each item In
 enumerable

                Dim data As IHierarchyData
 = enumerable.GetHierarchyData(item)

                If Not data Is
 Nothing Then

                    ' Create an object that represents the bound data
                    ' to the control.
                    Dim newNode As New
 TreeNode()
                    Dim rvnode As New
 RootViewNode()

                    rvnode.Node = newNode
                    rvnode.Depth = depth

                    ' The dataItem is not just a string, but potentially
                    ' an XML node or some other container. 
                    ' If DataTextField is set, use it to determine which
 
                    ' field to render. Otherwise, use the first field.
                    
                    If DataTextField.Length > 0 Then
                        newNode.Text = DataBinder.GetPropertyValue _
                        (data, DataTextField, Nothing)
                    Else
                        Dim props As PropertyDescriptorCollection
 = _
                        TypeDescriptor.GetProperties(data)

                        ' Set the "default" value of the node.
                        newNode.Text = String.Empty

                        ' Set the true data-bound value of the TextBox
,
                        ' if possible.
                        If props.Count >= 1 Then
                            If Not props(0).GetValue(data)
 Is Nothing Then
                                newNode.Text = props(0).GetValue(data).ToString()
                            End If
                        End If
                    End If

                    Nodes.Add(rvnode)

                    If data.HasChildren Then
                        Dim newEnumerable As
 IHierarchicalEnumerable = _
                            data.GetChildren()
                        If Not (newEnumerable
 Is Nothing) Then
                            RecurseDataBindInternal(newNode, _
                            newEnumerable, depth + 1)
                        End If
                    End If

                    If MaxDepth < depth Then
                        MaxDepth = depth
                    End If
                End If
            Next item

        End Sub 'RecurseDataBindInternal

        Protected Overrides Sub
 Render(ByVal writer As HtmlTextWriter)

            writer.WriteLine("<PRE>")
            Dim currentDepth As Integer
 = 1
            Dim currentTextLen As Integer
 = 0

            Dim rvnode As RootViewNode
            For Each rvnode In
 Nodes
                If rvnode.Depth = currentDepth Then
                    Dim output As String
 = "  " + rvnode.Node.Text + "
  "
                    writer.Write(output)
                    currentTextLen = currentTextLen + output.Length
                Else
                    writer.WriteLine("")

                    ' Some very basic whitespace formatting.
                    ' The implicit conversion to an Integer is fine,
 as 
                    ' a general estimate is acceptable for this 
                    ' simple example.
                    Dim halfLine As Integer
 = CInt(currentTextLen / 2)
                    Dim i As Integer
                    For i = 0 To halfLine
                        writer.Write(" "c)
                    Next i
                    writer.Write("|"c)
                    writer.WriteLine("")
                    currentDepth += 1
                    Dim j As Integer
                    For j = 0 To halfLine
                        writer.Write(" "c)
                    Next j
                    Dim output As String
 = "  " + rvnode.Node.Text + "
  "
                    writer.Write(output)
                    currentTextLen = currentTextLen + output.Length
                End If
            Next rvnode
            writer.WriteLine("</PRE>")

        End Sub 'Render


        Private Class RootViewNode
            Public Node As TreeNode
            Public Depth As Integer
        End Class 'RootViewNode
    End Class 'GeneologyTree
End Namespace
using System;
using System.Collections;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Samples.AspNet.CS.Controls {

    [AspNetHostingPermission(SecurityAction.Demand, 
        Level=AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, 
        Level=AspNetHostingPermissionLevel.Minimal)]
    public class GeneologyTree : HierarchicalDataBoundControl
 {

        private TreeNode rootNode;
        public TreeNode RootNode {
            get {
                if (rootNode == null) {
                    rootNode = new TreeNode(String.Empty);
                }
                return rootNode;
            }
        }
        
        private ArrayList nodes;
        public ArrayList Nodes {
            get {
                if (null == nodes) {
                    nodes = new ArrayList();
                }
                return nodes;
            }
        }
        public string DataTextField {
            get {
                object o = ViewState["DataTextField"];
                return((o == null) ? string.Empty
 : (string)o);
            }
            set {
                ViewState["DataTextField"] = value;
                if (Initialized) {
                    OnDataPropertyChanged();
                }
            }
        }
        private int _maxDepth = 0;
        protected override void PerformDataBinding()
 {
            base.PerformDataBinding();

            // Do not attempt to bind data if there is no
            // data source set.
            if (!IsBoundUsingDataSourceID && (DataSource
 == null)) {
                return;
            }
            
            HierarchicalDataSourceView view = GetData(RootNode.DataPath);
            
            if (view == null) {
                throw new InvalidOperationException
                    ("No view returned by data source control.");
            }                                  
            
            IHierarchicalEnumerable enumerable = view.Select();
            if (enumerable != null) {
                            
                Nodes.Clear();
                                
                try {
                    RecurseDataBindInternal(RootNode, enumerable, 1);
                }
                finally {
                
                }
            }
        }
        private void RecurseDataBindInternal(TreeNode
 node, 
            IHierarchicalEnumerable enumerable, int depth) { 
                                   
                        
            foreach(object item in enumerable)
 {
                IHierarchyData data = enumerable.GetHierarchyData(item);

                if (null != data) {
                    // Create an object that represents the bound data
                    // to the control.
                    TreeNode newNode = new TreeNode();
                    RootViewNode rvnode = new RootViewNode();
                    
                    rvnode.Node = newNode;
                    rvnode.Depth = depth;

                    // The dataItem is not just a string, but potentially
                    // an XML node or some other container. 
                    // If DataTextField is set, use it to determine
 which 
                    // field to render. Otherwise, use the first field.
                    
                    if (DataTextField.Length > 0) {
                        newNode.Text = DataBinder.GetPropertyValue
                            (data, DataTextField, null);
                    }
                    else {
                        PropertyDescriptorCollection props = 
                            TypeDescriptor.GetProperties(data);

                        // Set the "default" value of the
 node.
                        newNode.Text = String.Empty;                        

                        // Set the true data-bound value of the TextBox
,
                        // if possible.
                        if (props.Count >= 1) {           
             
                            if (null != props[0].GetValue(data))
 {
                                newNode.Text = 
                                    props[0].GetValue(data).ToString();
                            } 
                        }
                    }

                    Nodes.Add(rvnode);                    
                    
                    if (data.HasChildren) {
                        IHierarchicalEnumerable newEnumerable = 
                            data.GetChildren();
                        if (newEnumerable != null)
 {                            
                            RecurseDataBindInternal(newNode, 
                                newEnumerable, depth+1 );
                        }
                    }
                    
                    if ( _maxDepth < depth) _maxDepth = depth;
                    
                }
            }
        }
        protected override void Render(HtmlTextWriter
 writer) {
                        
            writer.WriteLine("<PRE>");                        
            int currentDepth = 1;
            int currentTextLen = 0;
            
            foreach (RootViewNode rvnode in
 Nodes) {
                if (rvnode.Depth == currentDepth) {
                    string output = "  " + rvnode.Node.Text
 + "  ";
                    writer.Write(output);
                    currentTextLen = currentTextLen + output.Length;
                }
                else {
                    writer.WriteLine("");
                    // Some very basic whitespace formatting
                    int halfLine = currentTextLen / 2;
                    for (int i=0;i<halfLine;i++)
 {
                        writer.Write(' ');
                    }
                    writer.Write('|');
                    writer.WriteLine("");
                    ++currentDepth; 
                    currentTextLen = 0;
                    for (int j=0;j<halfLine;j++)
 {
                        writer.Write(' ');
                    }
                    string output = "  " + rvnode.Node.Text
 + "  ";
                    writer.Write(output);
                    currentTextLen = currentTextLen + output.Length;            
        
                }                                                           
            }
            writer.WriteLine("</PRE>");
        }
        
        private class RootViewNode { 
            public TreeNode Node;
            public int Depth;
        }
    }
}

前の例で定義した GeneologyTree コントロール使用して、XmlDataSource コントロールバインドする方法次のコード例示します

<%@Page language="VB" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="Samples.AspNet.VB.Controls"
 
    Assembly="Samples.AspNet.VB.Controls"
 %>

<html>
  <head>
    <title>VB Example</title>
  </head>

  <body>
    <form id="Form1" method="post"
 runat="server">

        <aspSample:geneologytree
          id="GeneologyTree1"
          runat="server"
          datatextfield="title"
          datasourceid="XmlDataSource1" />

        <asp:xmldatasource
          id="XmlDataSource1"
          datafile="geneology.xml"          
          runat="server" />
          
    </form>
  </body>
</html>
<%@Page language="c#" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="Samples.AspNet.CS.Controls" 
    Assembly="Samples.AspNet.CS.Controls" %>

<html>
  <head>
    <title>C# Example</title>
  </head>

  <body>
    <form id="Form1" method="post" runat="server">

        <aspSample:geneologytree
          id="GeneologyTree1"
          runat="server"
          datatextfield="title"
          datasourceid="XmlDataSource1" />

        <asp:xmldatasource
          id="XmlDataSource1"
          datafile="geneology.xml"          
          runat="server" />
          
    </form>
  </body>
</html>

コード例アクセスされる geneology.xml ファイルには、次のデータ格納されています。

<family>
  <member title="great-grandfather">
    <member title="grandfather" >
      <member title="child" />
      <member title="father" >
         <member title="son" />
      </member>
    </member>
  </member>
</family>
.NET Framework のセキュリティ.NET Frameworkセキュリティ
継承階層継承階層
System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
       System.Web.UI.WebControls.BaseDataBoundControl
        System.Web.UI.WebControls.HierarchicalDataBoundControl
           System.Web.UI.WebControls.Menu
           System.Web.UI.WebControls.TreeView
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
HierarchicalDataBoundControl メンバ
System.Web.UI.WebControls 名前空間



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「HierarchicalDataBoundControl クラス」の関連用語

HierarchicalDataBoundControl クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



HierarchicalDataBoundControl クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS