User Tools

Site Tools


notes:asp.net:treeview

ASP.NET TreeView

The following code creates a simple TreeView that displays data from an XML file:

<asp:TreeView ID="TreeView1" DataSourceID="XmlDataSource1" ImageSet="XPFileExplorer" 
    NodeIndent="15" AutoGenerateDataBindings="False" runat="server">
    <ParentNodeStyle Font-Bold="False" />
    <HoverNodeStyle ForeColor="#6666AA" Font-Underline="True" />
    <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" 
        HorizontalPadding="0px" VerticalPadding="0px" />
    <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" 
        HorizontalPadding="2px" NodeSpacing="0px" VerticalPadding="2px" />
    <DataBindings>
        <asp:TreeNodeBinding Text="My Bookstore" Value="My Bookstore" />
        <asp:TreeNodeBinding DataMember="genre" TextField="name" />
        <asp:TreeNodeBinding DataMember="book" FormatString="ISBN: {0}" TextField="ISBN" />
        <asp:TreeNodeBinding DataMember="title" TextField="#InnerText" />
        <asp:TreeNodeBinding DataMember="price" TextField="#InnerText" />
        <asp:TreeNodeBinding DataMember="comments" Value="comments" />
        <asp:TreeNodeBinding DataMember="userComment" TextField="#InnerText" />
    </DataBindings>
</asp:TreeView>
 
<asp:XmlDataSource ID="XmlDataSource1" DataFile="~/App_Data/test.xml" runat="server" />

This is the test.xml file that populates the TreeView. It is located at the App_Data folder:

<?xml version="1.0" standalone="yes"?>
<bookstore>
  <genre name="computer">
    <book ISBN="978-1568817200">
      <title>Practical Rendering and Computation with Direct3D 11</title>
      <price>61.62</price>
      <comments>
        <userComment rating="4">
          Pretty good.
        </userComment>
        <userComment rating="5">
          Awesome.
        </userComment>
      </comments>
    </book>
    <book ISBN="978-0262033848">
      <title>Introduction to Algorithms</title>
      <price>80.00</price>
      <comments>
        <userComment rating="3">
          OK.
        </userComment>
        <userComment rating="4">
          Excellent.
        </userComment>
      </comments>
    </book>
  </genre>
  <genre name="nonfiction">
    <book ISBN="978-0123749031">
      <title>Collapse: How Societies Choose to Fail or Succeed</title>
      <price>13.49</price>
      <comments>
        <userComment rating="5">History, ecology, technology, and politics, rolled into one</userComment>
      </comments>
    </book>
    <book ISBN="978-0316069885">
      <title>Eating Animals </title>
      <price>11.35</price>
      <comments>
        <userComment rating="5">Best book on the food industry</userComment>
      </comments>
    </book>
  </genre>
</bookstore>
notes/asp.net/treeview.txt · Last modified: 2016/04/19 by admin