Bewerken

Share via


TreeNodeBinding.PopulateOnDemand Property

Definition

Gets or sets a value indicating whether the node to which the TreeNodeBinding object is applied is populated dynamically.

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

Property Value

true to populate the node to which the TreeNodeBinding object is applied dynamically; otherwise, false. The default is false.

Examples

This section contains two code examples. The first code example demonstrates how to use the ShowCheckBox property to specify whether a check box is displayed for a node. The second code example provides sample XML data for the first code example.

The following example demonstrates how to use the ShowCheckBox property to specify whether a check box is displayed for a node. For this example to work correctly, you must copy the sample XML data, provided after this code example, to a file named 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>

The following code example provides sample XML data for the preceding code example.

<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>  

Remarks

Sometimes, it is not practical to statically predefine the tree structure due to data size or custom content that depends on user input. Because of this, the TreeView control supports dynamic node population. When the PopulateOnDemand property is set to true, the child nodes of the node that the TreeNodeBinding object is applied to gets populated at run time when the node is expanded.

When data bindings are created by setting the AutoGenerateDataBindings of the TreeView control to true, the bindings that are created have the PopulateOnDemand property set to true. Data bindings that are created declaratively have the PopulateOnDemand property set to false. Using the declarative syntax allows you to control the behavior of individual data bindings.

Note

Unlike the PopulateOnDemand property of the TreeNode class, the PopulateOnDemand property does not require an event-handling method to be defined for the TreeNodePopulate event, if you are using a data source control, such as XmlDataSource. Instead, the TreeView control dynamically generates an event-handling method using the properties of the TreeNodeBinding objects in the DataBindings collection. You can still define an event-handling method for the TreeNodePopulate event; however, it will be called after the event-handling method for the TreeView control.

Supported browsers can also take advantage of client-side node population. When enabled, this allows the TreeView control to populate a node dynamically on the client when that node is expanded, preventing the need to post back to the server. For more information on client-side node population, see PopulateNodesFromClient.

The value of this property is stored in view state.

Applies to

See also