zoukankan      html  css  js  c++  java
  • 用DropDownList控件绑定XML数据实现省市区三级联动

    Citys.xml文件格式:

    <?xml version="1.0" encoding="utf-8"?>
    <PCitys>
    <Province pname="安徽省">
        <City cname="蚌埠市">
          <County name="蚌埠市"/>
          <County name="固镇县"/>
          <County name="怀远县"/>
          <County name="五河县"/>
        </City>
        <City cname="巢湖市">
          <County name="巢湖市"/>
          <County name="含山县"/>
          <County name="和县"/>
          <County name="庐江县"/>
          <County name="无为县"/>
        </City>
        <City cname="池州市">
          <County name="池州市"/>
          <County name="东至县"/>
          <County name="青阳县"/>
          <County name="石台县"/>
        </City>
    </Province>
    </PCitys>
    前台显示代码(VS2005的AJAX1.0实现无刷新调数据):

    <asp:ScriptManager id="ScriptManager1" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="upPCC" runat="server">
                    <ContentTemplate>
    <asp:DropDownList ID="ddlProvince" runat="server" AppendDataBoundItems="True" AutoPostBack="True"
                DataSourceID="XmlDataSource1" DataTextField="pname" DataValueField="pname" OnSelectedIndexChanged="ddlProvince_SelectedIndexChanged">
                <asp:ListItem Selected="True">省份</asp:ListItem>
            </asp:DropDownList><asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/Citys.xml"
                XPath="//Province"></asp:XmlDataSource>
            &nbsp;
            <asp:DropDownList ID="ddlCity" runat="server" AppendDataBoundItems="True" AutoPostBack="True"
                DataSourceID="XmlDataSource2" DataTextField="cname" DataValueField="cname" OnSelectedIndexChanged="ddlCity_SelectedIndexChanged">
                <asp:ListItem Selected="True">区市</asp:ListItem>
            </asp:DropDownList><asp:XmlDataSource ID="XmlDataSource2" runat="server" DataFile="~/App_Data/Citys.xml"
                XPath="//Province[@pname='']/City"></asp:XmlDataSource>
            <asp:DropDownList ID="ddlCounty" runat="server" AppendDataBoundItems="True" DataSourceID="XmlDataSource3"
                DataTextField="name" DataValueField="name">
                <asp:ListItem Selected="True">县市</asp:ListItem>
            </asp:DropDownList><asp:XmlDataSource ID="XmlDataSource3" runat="server" DataFile="~/App_Data/Citys.xml"
                XPath="//Province[@pname='']/City[@cname='']/County"></asp:XmlDataSource>
    </ContentTemplate></asp:UpdatePanel>

    后台代码:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class EmployeeAdd : System.Web.UI.Page
    ...{
        protected void Page_Load(object sender, EventArgs e)
        ...{
        }
        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        ...{
            ddlCity.Items.Clear();
            ddlCity.Items.Add("地市");
            ddlCity.DataTextField = "cname";
            ddlCity.DataValueField = "cname";
            XmlDataSource2.XPath = "//Province[@pname='" + ddlProvince.SelectedValue + "']/City";
            ddlCounty.Items.Clear();
            ddlCounty.Items.Add("县市");
        }
        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        ...{
            ddlCounty.Items.Clear();
            ddlCounty.Items.Add("县市");
            ddlCounty.DataTextField = "name";
            ddlCounty.DataValueField = "name";
            XmlDataSource3.XPath = "//Province[@pname='" + ddlProvince.SelectedValue + "']/City[@cname='" + ddlCity.SelectedValue + "']/County";
        }
    }

  • 相关阅读:
    [Python自学] day-10 (多进程、数据交互、进程锁、__main__、进程池、协程、gevent、简单爬虫、事件驱动异步IO、select poll epoll)
    [Python自学] day-9 (paramiko、SSH RSA、线程、GIL、互斥锁、信号量、事件、队列)
    [Python自学] day-8 (SocketServer)
    [Python自学] day-7 (静态方法、类方法、属性方法、类的其他、类的来源、反射、异常处理、socket)
    MyBatis学习 之 一、MyBatis简介与配置MyBatis+Spring+MySql
    深入浅出MyBatis-快速入门
    spring整合myBatis
    MyBatis入门学习(一)
    Mybatis中几个重要类
    ibatis源码分析
  • 原文地址:https://www.cnblogs.com/qfcndtt/p/2012426.html
Copyright © 2011-2022 走看看