Export (0) Print
Expand All

TreeNodeBinding.PopulateOnDemand Property

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

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public bool PopulateOnDemand { get; set; }

Property Value

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

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.

NoteNote

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.

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>

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>

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft