Use the TreeView control and XML in ASP.NET

xiaoxiao2021-03-06  47

Previously, if you want to use the tree control in the web page, there will be some troubles, sometimes you should write the code yourself to reach the purpose of displaying the data with the tree list. In ASP.NET, we can easily use the Internet Exploer Web Controls control provided by Microsoft to implement the tree list. In Microsoft, this Internet Explore Web Controls control collection includes MultiPage, Tabstrip, Toolbar, TreeView controls. In this article, let's look at how to use the TreeView control and XML in ASP.NET to implement the tree list.

Microsoft's set of controls can be downloaded in http://asp.net/iewebcontrols/download.aspx?tabindex=0&tabid=1, and run the setup installation after downloading. Now let's try to make a simple example with the TreeView control.

In the new web project in VS.NET, then in the toolbox, the mouse button pops up, select "Add New Item", in the custom toolbox, select the TreeView control (Note that the namespace is Microsoft Internet EXPLOERE Web Control's namespace), you can appear in the toolbox in the toolbox.

Next, drag the TreeView control into the form, switch to the HTML view, then find the following code:

<% @ Register tagprefix = "IE" namespace = "microsoft.web.ui.webcontrols" assembly = "Microsoft.Web.ui.WebControls"%>

Of course, you can change the tagrefix tag value, for example, change to FOOBAR, then use the TreeView control, use it as follows:

Now, we can add all kinds of nodes to the tree by selecting the Nodes attributes in the property box of the TreeView control, which is not detailed herein. Below is the code after adding a variety of nodes:

We specialize in the expanded attribute in Expanded = "True", which is set when the page is loaded, and when the page is loaded, the tree control is all expanded.

The above is a method of static adding data to a tree control when designing. Since the XML is essentially a structure of the tree structure, the data can be dynamically loaded into the control by binding to the tree control by using an XML file, there are two ways to implement:

1) Two additional XML files in line with TreeView format

2) Transform XML via XSL.

Let's first look at the first method, build an XML file as an example, named aspnetbooks.xml:

TEACH YOURSELF ACTIVE SERVER PAGES 3.0 in 21 days

Mitchell

Atkinson

1999

Designing Active Server Pages

Mitchell

2000

ASP.NET: TIPS, TUTORIALS, AND CODE

Mitchell

Mack

Walther

Seven

Anders

Nathan

Wahlin

2001

ASP unleashed

Walther

1998

If we use the first method, you must rewrite the XML, indicated in the following form, to be bound to the tree control: ...

That is to say, root nodes must be Treenodes (case in cases don't matter), each sub-node must

The form is arranged. So, we have rewritten the original XML file as the following form:

Add the following code:

This will then bind the XML file to the tree control. You can see the result after running:

ASP.NET Books Teach Yourself Active Server Pages 3.0 in 21 Daysdesigning Active Server PageSASP.NET: TIPS, TUTORIALS, AND CODEPROGRAMMING ASP.NET

It can be seen that the first method is confirmed, and the node of XML cannot be filtered or otherwise. And if the XSL of the second method is used, the original XML can be formatted to the original XML can be performed as needed. It is very convenient.

When using XSL, the tree control can be binded by using the following method:

Among them, the XSL file you want to convert in the properties of TreenodexSLTSRC, and our XSL files we design are as follows:

http://www.w3.org/1999/xsl/transform "Version = '1.0'>

PRICE - $

Year Published -

In the XSL above, we pass the like:

Attribute setting, extract each node in the XML file, assign it to the TEXT property of Treenode. Of course, it is also possible to set the node to display in XPath or the like in XSL.

After running, the result is the same as the first method, the tree control can be correctly displayed, and the flexibility is relatively high.

转载请注明原文地址:https://www.9cbs.com/read-117303.html

New Post(0)