WebDataTree是包含在infragistics ASP.NET里面的一个树型控件,可以绑定多层数据源以及XML数据源,数据绑定也可以设计时绑定和代码绑定,这里主要介绍如何快速通过代码绑定XML数据源到WebDataTree
1.创建一个ASP.NET页面,然后从Visual Studio工具箱里拖拽ScriptManager控件到WEB 页面
2. 拖拽WebDataTree控件到Web页面
3.创建一个Orders.xml文件,并把下面的代码复制到XML里
<?xml version="1.0" encoding="utf-8" ?>
<Customers>
<Customer CustomerID="ALFKI" CustomerName="Alfreds Futterkiste">
<Order OrderID="1024">
<OrderDetail ProductID="1" ProductName="Chocolate" Quantity="10" />
<OrderDetail ProductID="2" ProductName="Apples" Quantity="20" />
<OrderDetail ProductID="3" ProductName="Peanuts" Quantity="30" />
</Order>
<Order OrderID="1029">
<OrderDetail ProductID="10" ProductName="Flour" Quantity="100" />
</Order>
</Customer>
<Customer CustomerID="BLONP" CustomerName="Blondel père et fils">
<Order OrderID="2021">
<OrderDetail ProductID="4" ProductName="Rigatoni" Quantity="30" />
<OrderDetail ProductID="5" ProductName="Ricotta" Quantity="10" />
</Order>
</Customer>
<Customer CustomerID="WOLZA" CustomerName="Wolski Zajazd">
<Order OrderID="2029">
<OrderDetail ProductID="6" ProductName="Hot Dogs" Quantity="45" />
<OrderDetail ProductID="7" ProductName="Brown Rice" Quantity="12" />
<OrderDetail ProductID="8" ProductName="Ketchup" Quantity="100" />
<OrderDetail ProductID="9" ProductName="Mustard" Quantity="100" />
</Order>
</Customer>
</Customers>
4.在page_load事件里添加如下代码
//Create a new XmlDataSource
XmlDataSource xmlDataSource1 = new XmlDataSource();
//Give the path where the file is located
xmlDataSource1.DataFile = "~/Orders.Xml";
WebDataTree1.DataSourceID = "xmlDataSource1";
//Create Customer data binding
DataTreeNodeBinding customer = new DataTreeNodeBinding();
customer.DataMember = "Customer";
customer.TextField = "CustomerName";
customer.ValueField = "CustomerID";
//Create Order data binding
DataTreeNodeBinding orders = new DataTreeNodeBinding();
orders.DataMember = "Order";
orders.TextField = "OrderID";
orders.ValueField = "OrderID";
//Create OrderDetails data binding
DataTreeNodeBinding orderDetails = new DataTreeNodeBinding();
orderDetails.DataMember = "OrderDetail";
orderDetails.TextField = "ProductName";
orderDetails.ValueField = "ProductID";
//Add the data bindings to the WebDataTree
WebDataTree1.DataBindings.Add(customer);
WebDataTree1.DataBindings.Add(orders);
WebDataTree1.DataBindings.Add(orderDetails);
//Bind the data to the tree.
//Make sure you call the DataBind method after creating and adding the data bindings.
WebDataTree1.DataBind();