Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 160 - Binding treeview control to an xml file using xmldatasource control

Suggested Videos
Part 157 - SiteMapPath control in
Part 158 - Binding menu control to web.sitemap file
Part 159 - TreeView control

In this video, we will discuss binding TreeView control to an xml file using xmldatasource control. This is continuation to Part 159, so please watch Part 159 before proceeding.

1. Add an XML file and name it TreeViewData.xml. Copy and paste the following xml.
<?xml version="1.0" encoding="utf-8" ?>
  <TreeViewItem NavigateUrl="~/Home.aspx" Text="Home"/>
  <TreeViewItem NavigateUrl="~/Employee.aspx" Text="Employee">
    <TreeViewItem NavigateUrl="~/UploadResume.aspx" Text="Upload Resume"/>
    <TreeViewItem NavigateUrl="~/EditResume.aspx" Text="Edit Resume"/>
    <TreeViewItem NavigateUrl="~/ViewResume.aspx" Text="View Resume"/>
  <TreeViewItem NavigateUrl="~/Employer.aspx" Text="Employer">
    <TreeViewItem NavigateUrl="~/UploadJob.aspx" Text="Upload Job"/>
    <TreeViewItem NavigateUrl="~/EditJob.aspx" Text="Edit Job"/>
    <TreeViewItem NavigateUrl="~/ViewJob.aspx" Text="View Job"/>
  <TreeViewItem NavigateUrl="~/Admin.aspx" Text="Admin">
    <TreeViewItem NavigateUrl="~/AddUser.aspx" Text="Add User"/>
    <TreeViewItem NavigateUrl="~/EditUser.aspx" Text="Edit User"/>
    <TreeViewItem NavigateUrl="~/ViewUser.aspx" Text="View User"/>

2. Drag and drop an XmlDataSource control on the webform. Set XPath and DataFile attributes as shown below. Notice that DataFile attribute points to the XML file that we added in Step 1.
<asp:XmlDataSource ID="XmlDataSource1" runat="server" 
    XPath="/Items/TreeViewItem" DataFile="~/TreeViewData.xml">

3. Drag and drop a TreeView control and set DataSourceID attribute to the xmldatasource control we created in Step 2. Also, set DataBindings as shown below.
<asp:TreeView ID="TreeView1" DataSourceID="XmlDataSource1" runat="server">
        <asp:TreeNodeBinding DataMember="TreeViewItem" TextField="Text"
        NavigateUrlField="NavigateUrl" Target="_blank" />

1 comment:

  1. i really don't know where is the income to you when you put it all free..
    google ads just a very little bit they click because they are it person and know that's an ads..
    so how come did you can share this free? why did you get ?
    answer me please..

    i'm and million other people very help with this free and very thanks to you.


If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.