TreeNodeBinding.PopulateOnDemand 屬性

定義

取得或設定值,指出是否動態填入套用 TreeNodeBinding 物件的節點。

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

屬性值

true 表示動態填入套用 TreeNodeBinding 物件的節點,否則為 false。 預設為 false

範例

本節包含兩個程式碼範例。 第一個程式碼範例示範如何使用 ShowCheckBox 屬性來指定是否顯示節點的核取方塊。 第二個程式碼範例會針對第一個程式碼範例提供範例 XML 資料。

下列範例示範如何使用 ShowCheckBox 屬性來指定是否顯示節點的核取方塊。 若要讓此範例正常運作,您必須將此程式碼範例之後提供的範例 XML 資料複製到名為 Book.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 Page_Load(Object sender, EventArgs e)
  {

    // Create a TreeNodeBinding object and set its 
    // properties.
    TreeNodeBinding binding = new TreeNodeBinding();
    binding.DataMember = "Section";
    binding.Depth = 2;
    binding.TextField = "Heading";

    // Set the PopulateOnDemand property of the
    // TreeNodeBinding object programmatically.
    binding.PopulateOnDemand = false;

    // Add the TreeNodeBinding object to the DataBindings
    // collection of the TreeView control.
    BookTreeView.DataBindings.Add(binding);

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNodeBinding PopulateOnDemand Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNodeBinding PopulateOnDemand Example</h3>
    
      <asp:TreeView id="BookTreeView" 
        DataSourceID="BookXmlDataSource"
        ExpandDepth="2"  
        runat="server">
         
        <DataBindings>
          <asp:TreeNodeBinding DataMember="Book" 
            TextField="Title"/>
          <asp:TreeNodeBinding DataMember="Chapter" 
            TextField="Heading"
            PopulateOnDemand="False"/>
          <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="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 Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    ' Create a TreeNodeBinding object and set its 
    ' properties.
    Dim binding As TreeNodeBinding = New TreeNodeBinding
    binding.DataMember = "Section"
    binding.Depth = 2
    binding.TextField = "Heading"

    ' Set the PopulateOnDemand property of the
    ' TreeNodeBinding object programmatically.
    binding.PopulateOnDemand = False

    ' Add the TreeNodeBinding object to the DataBindings
    ' collection of the TreeView control.
    BookTreeView.DataBindings.Add(binding)

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeNodeBinding PopulateOnDemand Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeNodeBinding PopulateOnDemand Example</h3>
    
      <asp:TreeView id="BookTreeView" 
        DataSourceID="BookXmlDataSource"
        ExpandDepth="2"  
        runat="server">
         
        <DataBindings>
          <asp:TreeNodeBinding DataMember="Book" 
            TextField="Title"/>
          <asp:TreeNodeBinding DataMember="Chapter" 
            TextField="Heading"
            PopulateOnDemand="False"/>
          <asp:TreeNodeBinding DataMember="Section" 
            TextField="Heading"/>
        </DataBindings>
         
      </asp:TreeView>
      
      <asp:XmlDataSource id="BookXmlDataSource"  
        DataFile="Book.xml"
        runat="server">
      </asp:XmlDataSource>
    
    </form>
  </body>
</html>

下列程式碼範例提供上述程式碼範例的 XML 資料範例。

<Book Title="Book Title">  
    <Chapter Heading="Chapter 1">  
        <Section Heading="Section 1">  
        </Section>  
        <Section Heading="Section 2">  
        </Section>  
    </Chapter>  
    <Chapter Heading="Chapter 2">  
        <Section Heading="Section 1">  
        </Section>  
    </Chapter>  
    <Appendix Heading="Appendix A">  
    </Appendix>  
</Book>  

備註

有時候,因為視使用者輸入的資料大小或自訂內容而定,靜態預先定義樹狀結構並不實用。 因此, TreeView 控制項支援動態節點母體擴展。 PopulateOnDemand當 屬性設定為 true 時,當節點展開時, TreeNodeBinding 物件要套用至之節點的子節點會在執行時間填入。

將資料系結設定 AutoGenerateDataBindings 為 來建立資料系結時,所建立的 TreeViewtrue 系結會將 PopulateOnDemand 屬性設定為 true 。 以宣告方式建立的資料系結會將 PopulateOnDemand 屬性設定為 false 。 使用宣告式語法可讓您控制個別資料系結的行為。

注意

PopulateOnDemand不同于 類別的 TreeNode 屬性, PopulateOnDemand 如果您使用資料來源控制項,則屬性不需要為 TreeNodePopulate 事件定義事件處理方法,例如 XmlDataSource 。 相反地, TreeView 控制項會使用集合中 DataBindings 物件的屬性 TreeNodeBinding 動態產生事件處理方法。 您仍然可以定義 TreeNodePopulate 事件的事件處理方法;不過,它會在 控制項的 TreeView 事件處理常式方法之後呼叫。

支援的瀏覽器也可以利用用戶端節點母體擴展。 啟用時,這可讓 TreeView 控制項在展開該節點時動態填入用戶端上的節點,以避免需要回傳至伺服器。 如需用戶端節點母體擴展的詳細資訊,請參閱 PopulateNodesFromClient

此屬性的值會儲存在檢視狀態中。

適用於

另請參閱