Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
New Feature: Community Sites:
Create your own .NET community website and start earning from Google AdSense !
It's Free !
|
Implementing TreeView Data Binding with an XML File
Posted Date: 16 Apr 2008 Resource Type: Articles Category: Web Applications
|
Posted By: kamlesh Member Level: Silver Rating: Points: 20
|
To accomplish this, you need to go through the following two steps.
1. Add an XmlDataSource control to the page and set its DataFile property to the name of the XML File
2. Then add a TreeView control and set its DataSourceID property to the ID of the XmlDataSource control. Then map the nodes and their attributes in the XML file to the TreeView nodes by declaring the asp:TreeNodeBinding elements as part of the TreeView declaration.
For the purposes of this example, let us create an XML file named Categories.xml that looks like the following.
<?xmlversion='1.0'?>
<!-- This file represents a fragment of a Categories inventory database -->
<Categories>
<CategoryID="1" Name="Beverages">
<DescriptionValue="Soft drinks, coffees, teas, beers, and ales"/>
</Category>
<CategoryID="2" Name="Condiments">
<DescriptionValue="Sweet and savory sauces, relishes, spreads, and
seasonings"/>
</Category>
<CategoryID="3" Name="Confections">
<DescriptionValue="Desserts, candies, and sweet breads"/>
</Category>
<CategoryID="4" Name="Dairy Products">
<DescriptionValue="Cheeses"/>
</Category>
</Categories>
Once the XML file is created, create a new Web Form named TreeViewDataBinding.aspx and modify its code to look like the following.
<%@ PageLanguage="C#"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title> Binding TreeView Control with an XML File</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:TreeViewExpandImageUrl="Images/closed.gif"
CollapseImageUrl="Images/open.gif"ID="TreeView1"
Runat="server"DataSourceID="XmlDataSource1">
<DataBindings>
<asp:TreeNodeBindingDataMember="Category"
ValueField="ID"TextField="Name">
</asp:TreeNodeBinding>
<asp:TreeNodeBindingDataMember="Description"
ValueField="Value"
TextField="Value">
</asp:TreeNodeBinding>
</DataBindings>
</asp:TreeView>
</div>
<div>
<asp:XmlDataSourceID="XmlDataSource1"Runat="server"
DataFile="~/Data/Categories.xml">
</asp:XmlDataSource>
</div>
</form>
</body>
</html>
Code Summary The above code listing contains a TreeView control that is bound to an XML file through an XmlDataSource control. This is accomplished by setting the DataSourceID property of the TreeView control to the XmlDataSource control. The TreeView associates properties of individual TreeNode objects to attributes of XML nodes in the hierarchy. When you perform data binding, attributes in the individual XML elements are promoted to properties of the data item. Also note that the TreeView hierarchy exactly matches the hierarchy of the source XML. Because of this, you normally construct the XML specifically for binding to the TreeView.
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|