Use ASP. NET to bind the XML data source of TreeView to the instance code

Source: Internet
Author: User

The TreeView control can use XML documents as data sources to display nodes based on the hierarchical structure of XML documents. Access to XML documents is completed by the XmlDataSource control, specifying the XML document path from the DataFile attribute of the XmlDataSource control, and then setting the correspondence with the nodes in the XML document in the TreeView control. This example shows how to bind the TreeView control to an XML data source.

Technical Points
The technical points for binding a TreeView control to an XML data source are as follows.

Use the XmlDataSource control to access XML documents.

In the DataBindings attribute of the TreeView control, specify the ing between nodes and XML documents.


Steps

(1) Create an ASP. NET Website in VS2008 and name it "TreeViewSample2 ".

(2) Add a page named "TreeViewControl. aspx" and set this page as the start page.

(3) create an XML document named "XMLFile. xml" in the "App_Data" folder, which contains three layers: contacts, regions, and owners. The Code is as follows.

Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Contact name = "contact">
<Genre name = "South China">
<Person Text = "owner">
<Name>
Zhang San </name>
<Sex>
Male </sex>
<Age>
25 </age>
<Address>
Huangpu District, Guangzhou </address>
</Person>
<Person Text = "owner">
<Name>
Li Si </name>
<Sex>
Male </sex>
<Age>
28 </age>
<Address>
Nanhai District, Foshan City </address>
</Person>
</Genre>
<Genre name = "North China">
<Person Text = "owner">
<Name>
Wang Wu </name>
<Sex>
Male </sex>
<Age>
30 </age>
<Address>
Haidian District, Beijing </address>
</Person>
</Genre>
</Contact>

(4) Select the TreeView control and XmlDataSource control design page from the toolbox, and set the DataFile attribute of the XmlDataSource control to "~ /App_Data/XMLFile. xml ", and set the performanceid attribute of the TreeView control to" xmlperformance1 ".

(5) When loading the page for the first time, add the correspondence between nodes and the XML document through the TreeNodeBinding object programmatically (you can also specify it using the designer ). The Code is as follows.

Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" %>
<Script runat = "server">
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
This. TreeView1.ShowLines = true;
// The following describes the relationship between adding nodes and binding data sources.
TreeNodeBinding contact = new TreeNodeBinding ();
Contact. DataMember = "contact"; // specify the bound Member.
Contact. ValueField = "name"; // Value Field
This. TreeView1.DataBindings. Add (contact );
TreeNodeBinding genre = new TreeNodeBinding ();
Genre. DataMember = "genre"; // Add and bind with "region"
Genre. ValueField = "name ";
This. TreeView1.DataBindings. Add (genre );
TreeNodeBinding person = new TreeNodeBinding ();
Person. DataMember = "person"; // Add and bind with "owner"
Person. ValueField = "Text ";
This. TreeView1.DataBindings. Add (person );
TreeNodeBinding name = new TreeNodeBinding ();
Name. DataMember = "name"; // Add and bind with "name"
Name. ValueField = "# InnerText ";
This. TreeView1.DataBindings. Add (name );
TreeNodeBinding sex = new TreeNodeBinding ();
Sex. DataMember = "sex"; // Add to "gender"
Sex. ValueField = "# InnerText ";
This. TreeView1.DataBindings. Add (sex );
TreeNodeBinding age = new TreeNodeBinding ();
Age. DataMember = "age"; // Add and bind with "age"
Age. ValueField = "# InnerText ";
This. TreeView1.DataBindings. Add (age );
TreeNodeBinding address = new TreeNodeBinding ();
Address. DataMember = "address"; // Add and bind with "address"
Address. ValueField = "# InnerText ";
This. TreeView1.DataBindings. Add (address );
}
}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> XML binding example of TreeView </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: XmlDataSource ID = "xmlperformance1" runat = "server"
DataFile = "~ /App_Data/XMLFile. xml ">
</Asp: XmlDataSource>
</Div>
<Asp: TreeView ID = "TreeView1" runat = "server"
Performanceid = "xmlperformance1">
</Asp: TreeView>
</Form>
</Body>
</Html>

(6) press Ctrl + F5 to run the program, as shown in the running result.

Source program explanation

(1) The TreeNodeBinding class defines the relationship between a data item and the node to which the data item is bound in the TreeView control. The DataMember attribute of this class specifies the XML node corresponding to the data source displayed on the node. The ValueField attribute corresponds to the Value attribute of the TreeNode object.

(2) The Text attribute of the TreeNodeBinding class specifies the Text to be displayed to the user. If this attribute is not specified, it is the same as the ValueField attribute by default.

(3) The performanceid attribute of the TreeView control specifies the source ID of the data source control.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.