TreeView.PopulateNodesFromClient プロパティ

定義

クライアントからの要求に応じてノードにデータを設定するかどうかを示す値を取得または設定します。

public:
 property bool PopulateNodesFromClient { bool get(); void set(bool value); };
public bool PopulateNodesFromClient { get; set; }
member this.PopulateNodesFromClient : bool with get, set
Public Property PopulateNodesFromClient As Boolean

プロパティ値

クライアントからの要求に応じてツリー ノード データを設定する場合は true。それ以外の場合は false。 既定値は、true です。

次のコード例では、 プロパティを使用 PopulateNodesFromClient して、コントロール内のノードのクライアント側の作成を有効にする方法を TreeView 示します。 クライアント側ノードの作成が有効になっている場合、サーバーにポストバックする必要なく、ノードがクライアント上で動的に設定されます。


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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();
        newNode.Text = row["CategoryName"].ToString(); 
        newNode.Value = 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 xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeView PopulateNodesFromClient Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeView PopulateNodesFromClient Example</h3>
    
      <asp:TreeView id="LinksTreeView"
        Font-Names= "Arial"
        ForeColor="Blue"
        EnableClientScript="true"
        PopulateNodesFromClient="true"  
        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="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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()
        Newnode.Text = row("CategoryName").ToString() 
        Newnode.Value = 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 xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeView PopulateNodesFromClient Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeView PopulateNodesFromClient Example</h3>
    
      <asp:TreeView id="LinksTreeView"
        Font-Names= "Arial"
        ForeColor="Blue"
        EnableClientScript="true"
        PopulateNodesFromClient="true"  
        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 動的ノードの作成をサポートしています。 ノードの PopulateOnDemand プロパティが に true設定されている場合、そのノードは、ノードが展開されるときに実行時に設定されます。

必要に応じてノードを設定するだけでなく、サポートされているクライアント ブラウザーでノードを直接設定することもできます。 プロパティが PopulateNodesFromClienttrue設定されている場合、ツリー ノードを設定するためにクライアントからサービスが呼び出されるため、サーバーにポストバックする必要がなくなります。 それ以外の場合、コントロールは TreeView サーバーにポストバックしてノードを設定します。

注意

プロパティを EnableClientScripttrue 設定するには、 プロパティも PopulateNodesFromClient に設定する true必要があります。

クライアント上のノードを設定するには、最初に プロパティを PopulateNodesFromClienttrue 設定し、ノードの プロパティを PopulateOnDemandtrue設定します。 次に、プログラムによってノードにデータを設定するイベントの TreeNodePopulate イベント処理メソッドを定義します。 一般的なイベント処理メソッドは、データ ソースからノード データを取得し、データをノード構造に配置してから、設定されているノードのコレクションに ChildNodes ノード構造を追加します。 ノード構造を作成するには、親ノードのコレクションにChildNodesオブジェクトを追加TreeNodeします。

注意

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

注意

クライアント側ノード作成機能は、コールバック スクリプトをサポートするブラウザーでサポートされています。 ブラウザーでコールバック スクリプト へのアクセスがサポートされているかどうかをチェックするには、 クラスの プロパティをSupportsCallbackHttpBrowserCapabilities使用します。 クラスの プロパティを HttpBrowserCapabilities 使用して Browser 、現在の要求の クラスのインスタンスに HttpRequest アクセスできます。

このプロパティの値はビュー ステートに格納されます。

適用対象

こちらもご覧ください