在ASP.NET中使用Treeview控件和XML
在我們使用Treeview控件和XML來(lái)實(shí)現(xiàn)樹(shù)形列表之前,我們需要先下載微軟的Internet Exploere Web Controls控件,控件可以在http://asp.net/IEWebControls/Download.aspx?tabindex=0&tabid=1中下載,下載后運(yùn)行setup安裝就可以了。現(xiàn)在我們來(lái)試下用Treeview控件做個(gè)簡(jiǎn)單的例子。
在vs.net中新建一個(gè)WEB工程,之后在工具箱中,鼠標(biāo)右鍵彈出的菜單中,選擇“添加新項(xiàng)”,在自定義工具箱中,選擇TREEVIEW控件(注意選擇的是命名空間為Microsoft Internet Exploere web control的命名空間),按確定后,就可以在工具箱中出現(xiàn)Treeview控件了。
接著,將Treeview控件拖拉到窗體中,切換到HTML視圖,這時(shí)會(huì)發(fā)現(xiàn)有如下代碼:
<%@ Register TagPrefix="ie" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %> |
當(dāng)然,你可以改變TagPrefix的標(biāo)記值,比如,改為FooBar,那么以后在引用Treeview控件時(shí),就用如下方式引用:
<FooBar:TreeView runat="server" ... /> |
現(xiàn)在,我們可以通過(guò)點(diǎn)選Treeview控件的屬性框中的nodes屬性,來(lái)為該樹(shù)添加各類(lèi)結(jié)點(diǎn)了,由于比較簡(jiǎn)單,這里不詳細(xì)講述。下面是添加完各類(lèi)結(jié)點(diǎn)后的代碼:
<form runat="server">
<ie:TreeView runat="server">
<ie:TreeNode Text="Isaac Gibson" Expanded="True">
<ie:TreeNode Text="Birth - 1766" />
<ie:TreeNode Text="Death - 1827" />
<ie:TreeNode Text="Spouse">
<ie:TreeNode Text="Ritty Gibson" />
<ie:TreeNode Text="Married 1789" />
<ie:TreeNode Text="Children">
<ie:TreeNode Text="Phoebe Gibson">
<ie:TreeNode Text="Birth - 1790" />
<ie:TreeNode Text="Death - 1884" />
<ie:TreeNode Text="Spouse">
<ie:TreeNode Text="James K. Mason" />
<ie:TreeNode Text="Married 1819" />
</ie:TreeNode>
</ie:TreeNode>
<ie:TreeNode Text="John Gibson">
<ie:TreeNode Text="Birth - 1793" />
<ie:TreeNode Text="Death - 1802" />
......
</ie:TreeNode>
</ie:TreeView>
</form>
其中我們特別注意一下Expanded="True"中的Expanded屬性,該屬性當(dāng)被設(shè)置為true時(shí),則當(dāng)頁(yè)面被裝載時(shí),樹(shù)形控件被全部展開(kāi)。
#p#
以上是在設(shè)計(jì)時(shí),靜態(tài)添加數(shù)據(jù)到樹(shù)形控件的方法。而由于XML實(shí)質(zhì)上也是以樹(shù)形結(jié)構(gòu)來(lái)表示數(shù)據(jù)的結(jié)構(gòu),因此,就可以通過(guò)使用XML文件綁定到樹(shù)形控件的方法,來(lái)動(dòng)態(tài)加載數(shù)據(jù)到控件中去,其中有兩種方法可以實(shí)現(xiàn):
1)另外寫(xiě)一個(gè)符合TREEVIEW格式的XML文件
2)通過(guò)XSL將XML進(jìn)行轉(zhuǎn)換。
先來(lái)看下第一種方法,建一個(gè)XML文件作為例子,命名為aspnetbooks.xml:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book price="34.95">
<title>Teach Yourself Active Server Pages 3.0 in 21 Days</title>
<authors>
<author>Mitchell</author>
<author>Atkinson</author>
</authors>
<year>1999</year>
</book>
<book price="29.95">
<title>Designing Active Server Pages</title>
<authors>
<author>Mitchell</author>
</authors>
<year>2000</year>
</book>
<book price="34.95">
<title>ASP.NET: Tips, Tutorials, and Code</title>
<authors>
<author>Mitchell</author>
<author>Mack</author>
<author>W(wǎng)alther</author>
<author>Seven</author>
<author>Anders</author>
<author>Nathan</author>
<author>W(wǎng)ahlin</author>
</authors>
<year>2001</year>
</book>
<book price="24.95">
<title>ASP Unleashed</title>
<authors>
<author>W(wǎng)alther</author>
</authors>
<year>1998</year>
</book>
</books>
如果我們使用第一種方法,必須對(duì)XML進(jìn)行重寫(xiě),用以下的形式表示,才能綁定到樹(shù)形控件中去:
<TREENODES> <treenode text="..." /> ... |
就是說(shuō),根結(jié)點(diǎn)必須是treenodes(大小寫(xiě)都無(wú)所謂),每個(gè)子結(jié)點(diǎn)必須以<treenode>的形式排列。于是,我們對(duì)原來(lái)的XML文件改寫(xiě)為如下的形式:
<?xml version="1.0" encoding="UTF-8"?>
<TREENODES>
<treenode text="Teach Yourself Active Server_u80 ?ages 3.0 in 21 Days">
<treenode text="Price - $34.95" />
<treenode text="Authors">
<treenode text="Mitchell" />
<treenode text="Atkinson" />
</treenode>
<treenode text="Year Published - 2000" />
</treenode>
<treenode text="Designing Active Server Pages">
<treenode text="Price - $29.95" />
<treenode text="Authors">
<treenode text="Mitchell" />
</treenode>
<treenode text="Year Published - 2000" />
</treenode>
〈/TREENODES>
這樣就將該xml文件綁定到樹(shù)形控件中去了,運(yùn)行后可以看到結(jié)果:
ASP.NET Books |
可以看到,使用第一種方法的確比較麻煩,不能對(duì)XML的結(jié)點(diǎn)進(jìn)行篩選或者其他操作。而如果使用第二種方法的XSL,則可以根據(jù)需要隨時(shí)對(duì)原來(lái)的XML進(jìn)行格式的控制,十分方便。
在使用XSL時(shí),可以用如下的方法對(duì)樹(shù)形控件進(jìn)行綁定:
<form runat="server"> |
其中,treenodexsltsrc的屬性中指定要轉(zhuǎn)換的XSL文件,我們?cè)O(shè)計(jì)的XSL文件如下:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'>
<xsl:template match="/books">
<TREENODES>
<xsl:for-each select="book">
<treenode>
<xsl:attribute name="text">
<xsl:value-of select="title" />
</xsl:attribute>
<treenode>
<xsl:attribute name="text">
Price - $<xsl:value-of select="@price" />
</xsl:attribute>
</treenode>
<treenode text="Authors">
<xsl:for-each select="authors/author">
<treenode>
<xsl:attribute name="text">
<xsl:value-of select="text()" />
</xsl:attribute>
</treenode>
</xsl:for-each>
</treenode>
<treenode>
<xsl:attribute name="text">
Year Published - <xsl:value-of select="year" />
</xsl:attribute>
</treenode>
</treenode>
</xsl:for-each>
</TREENODES>
</xsl:template>
</xsl:stylesheet>
在上面的XSL中,我們通過(guò)形如:
<xsl:attribute name="text"> |
的屬性設(shè)置,提取XML文件中每個(gè)結(jié)點(diǎn)的值,將其賦值給予treenode的text屬性中。當(dāng)然,也可以在XSL中使用XPATH等設(shè)置要顯示的結(jié)點(diǎn)。
運(yùn)行后,結(jié)果同樣與用第一種方法的一樣,能正確顯示樹(shù)形控件,而且靈活性比較高。
【編輯推薦】


















