关闭

.NET2.0四级联下拉框

599人阅读 评论(0) 收藏 举报
test.aspx.cs file:

   protected void DdlistProvince_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (IsPostBack == true)
            {
                this.DdlistCity.Enabled = true;
                this.DdlistCountry.Enabled = false;
                this.DdlistCountry.Items.Insert(0, "请选择");
                this.XmlDataSourceCity.XPath = "Gov/State/Province[@PostalCode=" + this.DdlistProvince.SelectedValue + "]/City";
                this.DdlistCity.DataSourceID = "XmlDataSourceCity";
                this.DdlistCity.DataTextField = "Name";
                this.DdlistCity.DataValueField = "PostalCode";
            }
            else
            {
                this.DdlistCountry.Items.Clear();
            }
          
        }

        protected void DdlistCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (IsPostBack == true)
            {
               
                this.DdlistCountry.Enabled = true;
                this.XmlDataSourceCountry.XPath = "Gov/State/Province/City[@PostalCode=" + this.DdlistCity.SelectedValue + "]/County";
                this.DdlistCountry.DataSourceID = "XmlDataSourceCountry";
                this.DdlistCountry.DataTextField = "Name";
                this.DdlistCountry.DataValueField = "PostalCode";
            }

        }

        protected void DdlistState_SelectedIndexChanged(object sender, EventArgs e)
        {
            //[@PostalCode=" + this.DdlistState.SelectedValue + "]

            if (IsPostBack == true)
            {
                this.DdlistCountry.Enabled = false;
                this.DdlistCountry.Items.Insert(0, "请选择");
                this.DdlistCity.Enabled = false;
                this.XmlDataSourceProvince.XPath = "Gov/State[@PostalCode=" + this.DdlistState.SelectedValue + "]/Province";
                this.DdlistProvince.DataSourceID = "XmlDataSourceProvince";
                this.DdlistProvince.DataTextField = "Name";
                this.DdlistProvince.DataValueField = "PostalCode";
            }

        }

 test.aspx file:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>区域代码测试</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<asp:Label ID="Label1" runat="server" Text="国家"></asp:Label>
        <asp:DropDownList ID="DdlistState" runat="server" AutoPostBack="True" DataSourceID="XmlDataSourceState" DataTextField="Name" DataValueField="PostalCode" OnSelectedIndexChanged="DdlistState_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:Label ID="Label2" runat="server" Text="省"></asp:Label>
        <asp:DropDownList ID="DdlistProvince" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DdlistProvince_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:Label ID="Label3" runat="server" Text="市"></asp:Label>
        <asp:DropDownList ID="DdlistCity" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DdlistCity_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:Label ID="Label4" runat="server" Text="区"></asp:Label>
        <asp:DropDownList ID="DdlistCountry" runat="server" AutoPostBack="True">
        </asp:DropDownList>
        <asp:XmlDataSource ID="XmlDataSourceState" runat="server" DataFile="~/state.xml"
            XPath="Gov/State"></asp:XmlDataSource>
   
    </div>
        <asp:XmlDataSource ID="XmlDataSourceProvince" runat="server" DataFile="~/state.xml"
            XPath="Gov/State/Province"></asp:XmlDataSource>
        <asp:XmlDataSource ID="XmlDataSourceCity" runat="server" DataFile="~/state.xml" XPath="Gov/State/Province/City">
        </asp:XmlDataSource>
        <asp:XmlDataSource ID="XmlDataSourceCountry" runat="server" DataFile="~/state.xml"
             XPath="Gov/State/Province/City/County">
        </asp:XmlDataSource>
    </form>
</body>
</html>

xml file:
   <Gov>
<State Name="中国" PostalCode="100">

        <Province Name="北京市" PostalCode="110000">

             <City Name="市辖区" PostalCode="110100">

                   <County Name="东城区" PostalCode="110101"/>

                   <County Name="西城区" PostalCode="110102"/>

                   <County Name="崇文区" PostalCode="110103"/>

                   <County Name="宣武区" PostalCode="110104"/>

                   <County Name="朝阳区" PostalCode="110105"/>

                   <County Name="丰台区" PostalCode="110106"/>

                   <County Name="石景山区" PostalCode="110107"/>

                   <County Name="海淀区" PostalCode="110108"/>

                   <County Name="门头沟区" PostalCode="110109"/>

                   <County Name="房山区" PostalCode="110111"/>

                   <County Name="通州区" PostalCode="110112"/>

                   <County Name="顺义区" PostalCode="110113"/>

                   <County Name="昌平区" PostalCode="110114"/>

                   <County Name="大兴区" PostalCode="110115"/>

                   <County Name="怀柔区" PostalCode="110116"/>

                   <County Name="平谷区" PostalCode="110117"/>
            </City>


              <City Name="县" PostalCode="110200">

                   <County Name="密云县" PostalCode="110228"/>

                   <County Name="延庆县" PostalCode="110229"/>
            </City>


        </Province>

        <Province Name="天津市" PostalCode="120000">

              <City Name="市辖区" PostalCode="120100">

                   <County Name="和平区" PostalCode="120101"/>

                   <County Name="河东区" PostalCode="120102"/>

                   <County Name="河西区" PostalCode="120103"/>

                   <County Name="南开区" PostalCode="120104"/>

                   <County Name="河北区" PostalCode="120105"/>

                   <County Name="红桥区" PostalCode="120106"/>

                   <County Name="塘沽区" PostalCode="120107"/>

                   <County Name="汉沽区" PostalCode="120108"/>

                   <County Name="大港区" PostalCode="120109"/>

                   <County Name="东丽区" PostalCode="120110"/>

                   <County Name="西青区" PostalCode="120111"/>

                   <County Name="津南区" PostalCode="120112"/>

                   <County Name="北辰区" PostalCode="120113"/>

                   <County Name="武清区" PostalCode="120114"/>

                   <County Name="宝坻区" PostalCode="120115"/>
            </City>


              <City Name="县" PostalCode="120200">

                   <County Name="宁河县" PostalCode="120221"/>

                   <County Name="静海县" PostalCode="120223"/>

                   <County Name="蓟 县" PostalCode="120225"/>

            </City>

        </Province>
</State>
    <State Name="美国" PostalCode="200">
        <Province  Name="纽约州" PostalCode="1">
            <City   Name="纽约市" PostalCode="11">
                <County  Name="通用区" PostalCode="111"/>
                <County  Name="HP区" PostalCode="112"/>
            </City>
            <City   Name="洛杉矶市" PostalCode="12">
                <County  Name="谷歌区" PostalCode="121"/>
                <County  Name="IBM" PostalCode="122"/>
            </City>
        </Province>
       
        <Province  Name="华盛顿特区" PostalCode="12">
            <City   Name="金山市" PostalCode="121">
                <County  Name="唐人街" PostalCode="1211"/>
                <County  Name="华人区" PostalCode="1212"/>
            </City>
            <City   Name="卡拉奇市" PostalCode="122">
                <County  Name="硅谷区" PostalCode="1221"/>
                <County  Name="雅虎区" PostalCode="1222"/>
            </City>
        </Province>
    </State>
   
    <State Name="英国" PostalCode="300">
        <Province  Name="英格兰" PostalCode="2">
            <City   Name="剑桥市" PostalCode="21">
                <County  Name="东区" PostalCode="211"/>
                <County  Name="西区" PostalCode="212"/>
            </City>
            <City   Name="牛津市" PostalCode="22">
                <County  Name="南区" PostalCode="221"/>
                <County  Name="北区" PostalCode="222"/>
            </City>
        </Province>
       
        <Province  Name="苏格兰" PostalCode="22">
            <City   Name="伦敦市" PostalCode="221">
                <County  Name="A区" PostalCode="2211"/>
                <County  Name="B区" PostalCode="2212"/>
            </City>
            <City   Name="爱丁堡市" PostalCode="222">
                <County  Name="C区" PostalCode="2221"/>
                <County  Name="D区" PostalCode="2222"/>
            </City>
        </Province>
    </State>
</Gov>
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:211983次
    • 积分:3193
    • 等级:
    • 排名:第11125名
    • 原创:133篇
    • 转载:25篇
    • 译文:3篇
    • 评论:1条
    文章分类
    最新评论