Wednesday, 25 December 2013

Bind XML to TreeView control

In this article we are going to see how to bind the XML to TreeView Control, Make a xml which consists of navigate url and text value.

XML :
<?xml version="1.0" encoding="utf-8" ?>
<Menus>
  <Menu value="Home" url="~/Homes.aspx" />
  <Menu value="Employee" url="~/Employee.aspx">
    <Menu value="Add Employee" url="~/AddEmployee.aspx" />
  </Menu>
  <Menu value="Admin" url="~/Adm.aspx">
    <Menu value="Add User" url="~/AddUser.aspx" />
  </Menu>
</Menus>


HTML:

<table>
    <tr>
    <td>
    <div style="padding:10px;">
    <br />

        <asp:TreeView ID="TreeView1" DataSourceID="XmlDataSource1" runat="server">
        <DataBindings>
        <asp:TreeNodeBinding DataMember="Menu" TextField="value" NavigateUrlField="url" />
        </DataBindings>
        </asp:TreeView>

        <asp:XmlDataSource ID="XmlDataSource1" DataFile="~/Menus.xml" XPath="Menus/Menu" runat="server"></asp:XmlDataSource>

    </div>
    </td>
    <td>
       <div>        
            <ContentTemplate>             
                <asp:ContentPlaceHolder ID="ContentPlaceHolder1"  runat="server">                       
                </asp:ContentPlaceHolder>                           
            </ContentTemplate>           
       </div>
       </td>
    </tr>
    </table>         

OTUPUT:



This article will have the code for implementation for treeview control in master page.


1 comment: