生成xml文件,并且实现省,市,县无刷新联动

首先是生成xml文件,生成xml文件前去网上DOWNload一个最新中国省市行政数据库
public  void createxml(string filename)
    {
        XmlTextWriter mywrite = new XmlTextWriter(filename, Encoding.UTF8);
        try
        {
            mywrite.Formatting = Formatting.Indented;
            mywrite.WriteStartDocument();
            //mywrite.WriteProcessingInstruction("xml"
            mywrite.WriteStartElement("local");
            mywrite.WriteAttributeString("country", "中国");
            pdt = getprovincelist();
            if (pdt.Rows.Count > 0)
            {
                for (int i = 0; i < pdt.Rows.Count; i++)
                {
                    mywrite.WriteStartElement("province");
                    mywrite.WriteAttributeString("province", pdt.Rows[i]["province"].ToString());
                    mywrite.WriteAttributeString("provinceID", pdt.Rows[i]["provinceID"].ToString());
                    cdt = getcitylist(pdt.Rows[i]["provinceID"].ToString());
                    if (cdt.Rows.Count > 0)
                    {
                        for (int j = 0; j < cdt.Rows.Count; j++)
                        {
                            mywrite.WriteStartElement("city");
                            mywrite.WriteAttributeString("city", cdt.Rows[j]["city"].ToString());
                            mywrite.WriteAttributeString("cityID", cdt.Rows[j]["cityID"].ToString());
                            adt = getarealist(cdt.Rows[j]["cityID"].ToString());
                            if (adt.Rows.Count > 0)
                            {
                                for (int k = 0; k < adt.Rows.Count; k++)
                                {
                                    mywrite.WriteStartElement("area");
                                    mywrite.WriteAttributeString("area", adt.Rows[k]["area"].ToString());
                                    mywrite.WriteAttributeString("areaID", adt.Rows[k]["areaID"].ToString());
                                    mywrite.WriteEndElement();
                                }
                            }
                            mywrite.WriteEndElement();
                        }
                    }
                    mywrite.WriteEndElement();
                }
            }
            mywrite.WriteEndElement();
            mywrite.WriteEndDocument();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            mywrite.Close();
        }
    }
/*****/
接下来是aspx页面,需要引用jquery.js和jquery.xml2json.js(这是一个把xml转换成json的插件)
以下是代码

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="test_Default" %>

<!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>
    <script language="javascript" type="text/javascript" src="../jquery.js"></script>
    <script language="javascript" type="text/javascript" src="../opselect.js"></script>
    <script language="javascript" type="text/javascript" src="../jquery.xml2json.js"></script>
    <script language="javascript" type="text/javascript">
        $(function()
        {
            var local
            $.get("20090107.xml",function(xml){
            local = $.xml2json(xml, true);
            for(index in local.province)
            {
                $("#Select1").addOption(local.province[index].province,local.province[index].provinceID);
            }
            }
            );
            $("#Select1").bind("change",function()
            {//选清空城市,县城的值;
                $("#Select2").clearAll();
                $("#Select2").addOption("--请选择--",0);
                $("#Select3").clearAll();
                $("#Select3").addOption("--请选择--",0);
                var provinceID=$("#Select1").getSelectedValue();
                for(provinceindex in local.province)
                {
                    if(local.province[provinceindex].provinceID==provinceID)
                    {
                        for(cityindex in local.province[provinceindex].city)
                        {
                            $("#Select2").addOption(local.province[provinceindex].city[cityindex].city,local.province[provinceindex].city[cityindex].cityID);
                        }
                        break;
                    }
                }
            }
            );
             $("#Select2").bind("change",function()
            {//选清空城市,县城的值;
                $("#Select3").clearAll();
                $("#Select3").addOption("--请选择--",0);
                var provinceID=$("#Select1").getSelectedValue();
                var cityid=$("#Select2").getSelectedValue();
                for(provinceindex in local.province)
                {
                     if(local.province[provinceindex].provinceID==provinceID)
                    {
                        for(cityindex in local.province[provinceindex].city)
                        {
                            if(local.province[provinceindex].city[cityindex].cityID==cityid)
                            {
                                for(townindex in local.province[provinceindex].city[cityindex].area)
                                {
                                    $("#Select3").addOption(local.province[provinceindex].city[cityindex].area[townindex].area,local.province[provinceindex].city[cityindex].area[townindex].areaID);
                                }
                            }
                        }
                    }
                }
            }
            );
            //$("#Select2").clearother();
        }
        );
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <select id="Select1">
            <option value="0">--请选择--</option>
        </select>
        <select id="Select2">
            <option value="0">--请选择--</option>
        </select>
        <select id="Select3">
            <option value="0">--请选择--</option>
        </select>
    </div>
    </form>
</body>
</html>

posted @ 2009-01-07 17:01  火精灵  阅读(804)  评论(0编辑  收藏  举报