TreeView.TreeNodePopulate イベントとは? わかりやすく解説

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

TreeView.TreeNodePopulate イベント

メモ : このイベントは、.NET Framework version 2.0新しく追加されたものです。

PopulateOnDemand プロパティtrue設定されているノードが TreeView コントロール展開されたときに発生します

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

Public Event TreeNodePopulate As
 TreeNodeEventHandler
Dim instance As TreeView
Dim handler As TreeNodeEventHandler

AddHandler instance.TreeNodePopulate, handler
public event TreeNodeEventHandler TreeNodePopulate
public:
event TreeNodeEventHandler^ TreeNodePopulate {
    void add (TreeNodeEventHandler^ value);
    void remove (TreeNodeEventHandler^ value);
}
/** @event */
public void add_TreeNodePopulate (TreeNodeEventHandler
 value)

/** @event */
public void remove_TreeNodePopulate (TreeNodeEventHandler
 value)
JScript では、イベント使用できますが、新規に宣言することはできません。
解説解説

データサイズカスタム コンテンツユーザー入力依存する場合もあるため、ツリー構造をあらかじめ静的定義するのは現実的ではありません。このためTreeView コントロールノードへの動的なデータ設定サポートしてます。ノードPopulateOnDemand プロパティtrue設定されノードは、実行時ノード展開され時点データ設定されます。

ノード動的に値を代入するには、まずノードPopulateOnDemand プロパティtrue設定します次にプログラムによってノードに値を設定する TreeNodePopulate イベントイベント処理メソッド定義します通常イベント処理メソッドデータ ソースからノード データ取得してデータノード構造配置し、値が設定されるノードの ChildNodes コレクションにそのノード構造追加します。TreeNode オブジェクト親ノードChildNodes コレクション追加すると、ノード構造作成されます。

メモメモ

ノードPopulateOnDemand プロパティtrue設定されている場合ノードには動的に値が設定される必要がありますノードの下に別のノード宣言によって入れ子にすることはできません。それ以外場合ページエラー発生します

サポートされブラウザ (Microsoft Internet Explorer Version 4.0 以降互換性があるブラウザ) では、クライアント側ノードへのデータ設定利用できますクライアント側ノード ポピュレーション有効にすると、ノードの展開時に TreeView コントロール使用してクライアントノードに値を動的に代入でき、サーバーポストバックする必要がなくなりますクライアント側ノードへのデータ設定詳細については、PopulateNodesFromClient のトピック参照してください

イベント処理詳細については、「イベント利用」を参照してください

使用例使用例

TreeNodePopulate イベント使用してサーバー上の TreeView コントロールノードに値を動的に設定する方法次のコード例示します。EnableClientScript プロパティfalse設定して展開するノードイベントクライアント処理されないようにします。

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data"
 %>
<%@ Import Namespace="System.Data.SqlClient"
 %>

<script runat="server">

  Sub PopulateNode(ByVal sender As
 Object, ByVal e As TreeNodeEventArgs)

    ' Call the appropriate method to populate a node at a particular
 level.
    Select Case e.Node.Depth

      Case 0
        ' Populate the first-level nodes.
        PopulateCategories(e.Node)

      Case 1
        ' Populate the second-level nodes.
        PopulateProducts(e.Node)

      Case Else
        ' Do nothing.

    End Select

  End Sub

  Sub PopulateCategories(ByVal node As
 TreeNode)

    ' Query for the product categories. These are the values
    ' for the second-level nodes.
    Dim ResultSet As DataSet = RunQuery("Select
 CategoryID, CategoryName From Categories")

    ' Create the second-level nodes.
    If ResultSet.Tables.Count > 0 Then

      ' Iterate through and create a new node for each row in the query
 results.
      ' Notice that the query results are stored in the table of the
 DataSet.
      Dim row As DataRow

      For Each row In ResultSet.Tables(0).Rows

        ' Create the new node. Notice that the CategoryId is stored
 in the Value property 
        ' of the node. This will make querying for items in a specific
 category easier when
        ' the third-level nodes are created. 
        Dim NewNode As TreeNode = New
 TreeNode(row("CategoryName").ToString(), row("CategoryID").ToString())

        ' Set the PopulateOnDemand property to true so that the child
 nodes can be 
        ' dynamically populated.
        NewNode.PopulateOnDemand = True

        ' Set additional properties for the node.
        NewNode.SelectAction = TreeNodeSelectAction.Expand

        ' Add the new node to the ChildNodes collection of the parent
 node.
        node.ChildNodes.Add(NewNode)

      Next

    End If

  End Sub

  Sub PopulateProducts(ByVal node As
 TreeNode)

    ' Query for the products of the current category. These are the
 values
    ' for the third-level nodes.
    Dim ResultSet As DataSet = RunQuery("Select
 ProductName From Products Where CategoryID=" & node.Value)

    ' Create the third-level nodes.
    If ResultSet.Tables.Count > 0 Then

      ' Iterate through and create a new node for each row in the query
 results.
      ' Notice that the query results are stored in the table of the
 DataSet.
      Dim row As DataRow

      For Each row In ResultSet.Tables(0).Rows

        ' Create the new node.
        Dim NewNode As TreeNode = New
 TreeNode(row("ProductName").ToString())

        ' Set the PopulateOnDemand property to false because these are
 leaf nodes and
        ' do not need to be populated.
        NewNode.PopulateOnDemand = False

        ' Set additional properties for the node.
        NewNode.SelectAction = TreeNodeSelectAction.None

        ' Add the new node to the ChildNodes collection of the parent
 node.
        node.ChildNodes.Add(NewNode)

      Next

    End If

  End Sub

  Function RunQuery(ByVal QueryString As
 String) As DataSet

    ' Declare the connection string. This example uses Microsoft SQL
 Server and connects to the
    ' Northwind sample database.
    Dim ConnectionString As String
 = "server=localhost;database=NorthWind;Integrated Security=SSPI"

    Dim DBConnection As SqlConnection = New
 SqlConnection(ConnectionString)
    Dim DBAdapter As SqlDataAdapter
    Dim ResultsDataSet As DataSet = New
 DataSet

    Try

      ' Run the query and create a DataSet.
      DBAdapter = New SqlDataAdapter(QueryString, DBConnection)
      DBAdapter.Fill(ResultsDataSet)

      ' Close the database connection.
      DBConnection.Close()

    Catch ex As Exception

      ' Close the database connection if it is still open.
      If DBConnection.State = ConnectionState.Open Then

        DBConnection.Close()

      End If

      Message.Text = "Unable to connect to the database."

    End Try

    Return ResultsDataSet

  End Function

</script>

<html>
  <body>
    <form runat="server">
    
      <h3>TreeView TreeNodePopulate Example</h3>
    
      <asp:TreeView id="LinksTreeView"
        Font-Name= "Arial"
        ForeColor="Blue"
        EnableClientScript="false" 
        OnTreeNodePopulate="PopulateNode"
        runat="server">
         
        <Nodes>
        
          <asp:TreeNode Text="Inventory" 
            SelectAction="Expand"  
            PopulateOnDemand="true"/>
        
        </Nodes>
        
      </asp:TreeView>
      
      <br><br>
      
      <asp:Label id="Message" runat="server"/>

    </form>
  </body>
</html>

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">

  void PopulateNode(Object sender, TreeNodeEventArgs e)
  {

    // Call the appropriate method to populate a node at a particular
 level.
    switch(e.Node.Depth)
    {
      case 0:
        // Populate the first-level nodes.
        PopulateCategories(e.Node);
        break;
      case 1:
        // Populate the second-level nodes.
        PopulateProducts(e.Node);
        break;
      default:
        // Do nothing.
        break;
    }
    
  }

  void PopulateCategories(TreeNode node)
  {
    
    // Query for the product categories. These are the values
    // for the second-level nodes.
    DataSet ResultSet = RunQuery("Select CategoryID, CategoryName From Categories");

    // Create the second-level nodes.
    if(ResultSet.Tables.Count > 0)
    {
    
      // Iterate through and create a new node for each row in the query
 results.
      // Notice that the query results are stored in the table of the
 DataSet.
      foreach (DataRow row in ResultSet.Tables[0].Rows)
      {
        
        // Create the new node. Notice that the CategoryId is stored
 in the Value property 
        // of the node. This will make querying for items in a specific
 category easier when
        // the third-level nodes are created. 
        TreeNode NewNode = new TreeNode(row["CategoryName"].ToString(),
 row["CategoryID"].ToString());
        
        // Set the PopulateOnDemand property to true so that the child
 nodes can be 
        // dynamically populated.
        NewNode.PopulateOnDemand = true;
        
        // Set additional properties for the node.
        NewNode.SelectAction = TreeNodeSelectAction.Expand;
        
        // Add the new node to the ChildNodes collection of the parent
 node.
        node.ChildNodes.Add(NewNode);
        
      }
      
    }
    
  }

  void PopulateProducts(TreeNode node)
  {

    // Query for the products of the current category. These are the
 values
    // for the third-level nodes.
    DataSet ResultSet = RunQuery("Select ProductName From Products Where CategoryID="
 + node.Value);

    // Create the third-level nodes.
    if(ResultSet.Tables.Count > 0)
    {
    
      // Iterate through and create a new node for each row in the query
 results.
      // Notice that the query results are stored in the table of the
 DataSet.
      foreach (DataRow row in ResultSet.Tables[0].Rows)
      {
      
        // Create the new node.
        TreeNode NewNode = new TreeNode(row["ProductName"].ToString());
        
        // Set the PopulateOnDemand property to false because these
 are leaf nodes and
        // do not need to be populated.
        NewNode.PopulateOnDemand = false;
        
        // Set additional properties for the node.
        NewNode.SelectAction = TreeNodeSelectAction.None;
        
        // Add the new node to the ChildNodes collection of the parent
 node.
        node.ChildNodes.Add(NewNode);
        
      }
      
    }

  }

  DataSet RunQuery(String QueryString)
  {

    // Declare the connection string. This example uses Microsoft SQL
 Server and connects to the
    // Northwind sample database.
    String ConnectionString = "server=localhost;database=NorthWind;Integrated
 Security=SSPI"; 

    SqlConnection DBConnection = new SqlConnection(ConnectionString);
    SqlDataAdapter DBAdapter;
    DataSet ResultsDataSet = new DataSet();

    try
    {

      // Run the query and create a DataSet.
      DBAdapter = new SqlDataAdapter(QueryString, DBConnection);
      DBAdapter.Fill(ResultsDataSet);

      // Close the database connection.
      DBConnection.Close();

    }
    catch(Exception ex)
    {

      // Close the database connection if it is still open.
      if(DBConnection.State == ConnectionState.Open)
      {
        DBConnection.Close();
      }
      
      Message.Text = "Unable to connect to the database.";

    }

    return ResultsDataSet;

  }

</script>

<html>
  <body>
    <form runat="server">
    
      <h3>TreeView TreeNodePopulate Example</h3>
    
      <asp:TreeView id="LinksTreeView"
        Font-Name= "Arial"
        ForeColor="Blue"
        EnableClientScript="false" 
        OnTreeNodePopulate="PopulateNode"
        runat="server">
         
        <Nodes>
        
          <asp:TreeNode Text="Inventory" 
            SelectAction="Expand"  
            PopulateOnDemand="true"/>
        
        </Nodes>
        
      </asp:TreeView>
      
      <br><br>
      
      <asp:Label id="Message" runat="server"/>

    </form>
  </body>
</html>

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
TreeView クラス
TreeView メンバ
System.Web.UI.WebControls 名前空間
OnTreeNodePopulate
TreeNode.PopulateOnDemand プロパティ
TreeView.PopulateNodesFromClient プロパティ
その他の技術情報
イベント利用


このページでは「.NET Framework クラス ライブラリ リファレンス」からTreeView.TreeNodePopulate イベントを検索した結果を表示しています。
Weblioに収録されているすべての辞書からTreeView.TreeNodePopulate イベントを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からTreeView.TreeNodePopulate イベント を検索

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

辞書ショートカット

すべての辞書の索引

TreeView.TreeNodePopulate イベントのお隣キーワード
検索ランキング

   

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



TreeView.TreeNodePopulate イベントのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS