TreeView.AutoGenerateDataBindings Property

Definition

Gets or sets a value indicating whether the TreeView control automatically generates tree node bindings.

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

Property Value

true to have the TreeView control automatically generate tree node bindings; otherwise, false. The default is true.

Examples

This section contains two code examples. The first code example demonstrates how to disable automatic tree node binding generation by setting the AutoGenerateDataBindings property to false. The second code example provides sample site map data for the first code example.

The following code example demonstrates how to disable automatic tree node binding generation by setting the AutoGenerateDataBindings property to false. A DataBindings collection is then defined to establish the relationship between the data items in a SiteMapDataSource control and the nodes of the TreeView control. For this example to work correctly, you must copy the sample site map data, provided after this code example, to a file named Web.sitemap.


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeView AutoGenerateBindings Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeView AutoGenerateBindings Example</h3>
    
      <!-- Set the AutoGenerateBindings property -->
      <!-- to false declaratively to allow for   -->
      <!-- the user-defined Bindings collection. -->
      <asp:TreeView id="SiteTreeView" 
        DataSourceID="SiteMapSource"
        AutoGenerateDataBindings="False"
        runat="server">
        
        <DataBindings>
        
          <asp:TreeNodeBinding TextField="title" NavigateUrlField="url"/>
        
        </DataBindings>
            
      </asp:TreeView>
      
      <asp:SiteMapDataSource ID="SiteMapSource" runat="server"/>
         
    </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">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>TreeView AutoGenerateBindings Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>TreeView AutoGenerateBindings Example</h3>
    
      <!-- Set the AutoGenerateBindings property -->
      <!-- to false declaratively to allow for   -->
      <!-- the user-defined Bindings collection. -->
      <asp:TreeView id="SiteTreeView" 
        DataSourceID="SiteMapSource"
        AutoGenerateDataBindings="False"
        runat="server">
        
        <DataBindings>
        
          <asp:TreeNodeBinding TextField="title" NavigateUrlField="url"/>
        
        </DataBindings>
            
      </asp:TreeView>
      
      <asp:SiteMapDataSource ID="SiteMapSource" runat="server"/>
         
    </form>
  </body>
</html>

The following code example provides sample site map data for the preceding example.

<siteMap>
    <siteMapNode title="Home" description="Home" url="default.aspx">
        <siteMapNode title="Products" description="Products" url="Products.aspx">
            <siteMapNode title="Computers" url="Computers.aspx"/>
            <siteMapNode title="Accessories" url="Accessories.aspx"/>
        </siteMapNode>
    </siteMapNode>
</siteMap>

Remarks

Tree node bindings define the relationship between a data item and the node it is binding to. When binding to a data source where each data item contains multiple properties (such as an XML element with several attributes), the TreeView control needs to know which data item property to bind to which TreeNode property. If the AutoGenerateDataBindings property is set to true, the TreeView control automatically generates default tree node bindings that set the Text property of the TreeNode object that is associated with the binding to the value that is returned by the ToString method of the data item.

Note

The automatically generated tree node bindings are not added to the DataBindings collection.

In the case of an XML element, the value that is returned by the ToString method is the element name, which shows the underlying structure of the XML file, but is not very useful otherwise. To create a more useful tree, you must define your own tree node bindings by populating the DataBindings collection.

Note

If you define a DataBindings collection, the TreeView control uses those values instead of automatically generated tree node bindings.

The value of this property is stored in view state.

Applies to

See also