logo

DropDownList 绑定 XML 和 DropDownList 绑定 XML 数据并进行联动的例子

作者:孟宪会 阅读:2066 发表于:2010-10-21 14:08:24

默认情况下,绑定 XML 数据格式,是属性,要绑定节点,需要编写 XLST 进行转换,或者使用 XPath() 方法进行选择。下面就是绑定 XML 文件的例子。

aa.xml 代码
<countrys>
  
<country id="China" value="China">
    
<city id="hb" value="Hubei" />
    
<city id="gz" value="Guangzhou" />
    
<city id="hn" value="Hunan" />
    
<city id="jx" value="jiangxi" />
  
</country>

  
<country id="USA" value="USA">
    
<city id="ny" value="New Yory" />
    
<city id="hsd" value=" Washington " />
  
</country>
</countrys>
a.xml 代码
<?xml version="1.0" encoding="utf-8" ?>
<root>
  
<item>
    
<value>HeNan</value>
    
<text>河南</text>
  
</item>
  
<item>
    
<value>Beijing</value>
    
<text>北京</text>
  
</item>
  
<item>
    
<value>Chongqing</value>
    
<text>重庆</text>
  
</item>
</root>
c.xsl 代码
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" />
  
<xsl:template match="/node()"><!--先找根节点-->
    
<xsl:element name="{local-name()}">
      
<xsl:for-each select="child::node()">
        
<xsl:element name="{local-name()}">
          
<xsl:for-each select="child::node()">
            
<xsl:attribute name="{local-name()}">
              
<xsl:value-of select="."/>
            
</xsl:attribute>
          
</xsl:for-each>          
        
</xsl:element>        
      
</xsl:for-each>
    
</xsl:element>
  
</xsl:template>
</xsl:stylesheet>
ASPX 代码
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<script runat="server">
  protected
void Page_Load(object sender, EventArgs e)
  {
    
//直接绑定的例子
    XmlDataSource XmlDataSource1 = new XmlDataSource();
    XmlDataSource1.DataFile
= "~/aa.xml";
    XmlDataSource1.ID
= "XmlDataSource1";
    form1.Controls.Add(XmlDataSource1);
    DropDownList1.DataSourceID
= "XmlDataSource1";
    DropDownList1.DataTextField
= "id";
    DropDownList1.DataValueField
= "value";

    
//数据过滤的例子
    XmlDataSource XmlDataSource2 = new XmlDataSource();
    XmlDataSource2.ID
= "XmlDataSource2";
    XmlDataSource2.DataFile
= "~/aa.xml";
    XmlDataSource2.XPath
= "countrys/country[@id='China']/city";
    form1.Controls.Add(XmlDataSource2);
    DropDownList2.DataSourceID
= "XmlDataSource2";
    DropDownList2.DataTextField
= "id";
    DropDownList2.DataValueField
= "value";

    
//绑定节点而不是属性的例子
    XmlDataSource XmlDataSource3 = new XmlDataSource();
    XmlDataSource3.ID
= "XmlDataSource3";
    XmlDataSource3.DataFile
= "~/a.xml";
    XmlDataSource3.TransformFile
= "~/c.xsl";
    form1.Controls.Add(XmlDataSource3);
    DropDownList3.DataSourceID
= "XmlDataSource3";
    DropDownList3.DataTextField
= "text";
    DropDownList3.DataValueField
= "value";
  }  
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  
<title></title>
</head>
<body>
  
<form id="form1" runat="server">
  
<asp:DropDownList ID="DropDownList1" runat="server">
  
</asp:DropDownList>
  
<asp:DropDownList ID="DropDownList2" runat="server">
  
</asp:DropDownList>
  
<asp:DropDownList ID="DropDownList3" runat="server">
  
</asp:DropDownList>
  
</form>
</body>
</html>
下面这个例子是进行联动的例子:
City.xml 代码
<?xml version="1.0" encoding="utf-8"?>
<Cities>
  
<City text="北京" value="bj">
    
<District value="haidian" text="海淀区"></District>
    
<District value="changping" text="昌平区"></District>
    
<District value="chaoyang" text="朝阳区"></District>
    
<District value="pinggu" text="平谷区"></District>
  
</City>
  
<City text="上海" value="sh">
    
<District value="pudong" text="浦东区"></District>
    
<District value="xuhui" text="徐汇区"></District>
  
</City>
  
<City text="郑州" value="zz">
    
<District value="erqi" text="二七区"></District>
    
<District value="huiji" text="惠济区"></District>
    
<District value="jinshui" text="金水区"></District>
  
</City>
</Cities>
ASPX 代码
<%@ Page Language="C#" EnableViewState="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  protected
void City_SelectedIndexChanged(object sender, EventArgs e)
  {
    
if (this.City.SelectedValue == "")
    {
      District.Items.Clear();
    }
    
else
    {
      District.DataSourceID
= "XmlDistrict";
      XmlDistrict.XPath
= "Cities/City[@value='" + this.City.SelectedValue + "']/District";
      District.DataTextField
= "text";
      District.DataValueField
= "value";
    }
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  
<title></title>
</head>
<body>
  
<form id="form1" runat="server">
  
<asp:DropDownList ID="City" runat="server" OnSelectedIndexChanged="City_SelectedIndexChanged"
    DataTextField
="text" DataValueField="value" DataSourceID="XmlCity" AutoPostBack="true"
    AppendDataBoundItems
="true">
    
<asp:ListItem Value="">-- 请选择城市 --</asp:ListItem>
  
</asp:DropDownList>
  
<asp:DropDownList ID="District" runat="server">
  
</asp:DropDownList>
  
<asp:XmlDataSource ID="XmlCity" runat="server" DataFile="~/City.xml"></asp:XmlDataSource>
  
<asp:XmlDataSource ID="XmlDistrict" runat="server" DataFile="~/City.xml"></asp:XmlDataSource>
  
</form>
</body>
</html>