asp.net中的省市区联动封装成用户控件

今天用ajax+xml+一般处理程序写了个省市区联动的用户控件,感觉有需要优化的地方,欢迎大家指正。

 

用户控件的前段页面代码
 1 <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Area.ascx.cs" Inherits="uc_Area" %>
 2 <script src="../js/jquery-1.5.1.min.js" type="text/javascript"></script>
 3 <script type="text/javascript">
 4     $(function () {
 5         GetData($("#province"), '');
 6         a();
 7         $("#province").bind("change", function () {
 8             $('#area').find("option:gt(0)").remove();
 9             if ($("#province").val() != '-1') {
10                 GetData($("#city"), $("#province").val() + ",c");
11             }
12         });
13 
14         $("#city").bind("change", function () {
15             if ($("#city").val() != '-1') {
16                 GetData($("#area"), $("#city").val() + ",a");
17             }
18 
19         });
20 
21         $("#div1 select").change(function () {
22             $(this).next('input[type=hidden]').val($(this).val());
23         });
24 
25     });
26 
27     function GetData(sel, param) {
28         sel.find("option:gt(0)").remove();
29         $.ajax({
30             type: "post",
31             url: "./Area.ashx?action=province",
32             contentType: "application/x-www-form-urlencoded;charset=UTF-8",
33             data: "param=" + param,
34             datatype: "json",
35             async: false,
36             success: function (data) {
37                 var json = eval(data);
38                 if (!json) return;
39                 $.each(json, function (i, n) {
40                     sel.append($("<option value='" + n.name + "'>" + n.name + "</option>"));
41                 });
42             },
43             error: function (e, x) {
44                 alert(e.responseText);
45             }
46         });
47     }
48     var a = function () {
49         if ($("#<%=hidProvince.ClientID %>").val() != '') {
50             setTimeout(function () { $("#province").val($("#<%=hidProvince.ClientID %>").val()); }, 1);
51             setTimeout(function () { $("#province").change(); }, 1);
52 
53         }
54         if ($("#<%=hidCity.ClientID %>").val() != '' && $("#city").val() == '-1') {
55             setTimeout(function () { $("#city").val($("#<%=hidCity.ClientID %>").val()); }, 1);
56             setTimeout(function () { $("#city").change(); }, 1);
57 
58         }
59 
60         if ($("#<%=hidArea.ClientID %>").val() != '' && $("#area").val() == '-1') {
61             setTimeout(function () { $("#area").val($("#<%=hidArea.ClientID %>").val()); }, 1);
62             setTimeout(function () { $("#area").change(); }, 1);
63         }
64 
65     };
66 </script>
67 <div id="div1">
68     <select name="province" id="province" style="width:100px">
69         <option value="-1">请选择省</option>
70     </select>
71     <asp:HiddenField ID="hidProvince" runat="server" />
72     <select name="city" id="city" style="width:100px">
73         <option value="-1">请选择市</option>
74     </select>
75     <asp:HiddenField ID="hidCity" runat="server" />
76     <select name="area" id="area" style="width:100px">
77         <option value="-1">请选择县/区</option>
78     </select>
79     <asp:HiddenField ID="hidArea" runat="server" />
80 </div>

一般处理程序的代码,感觉有地方需要优化:

一般处理程序读取xml
  1 <%@ WebHandler Language="C#" Class="Area" %>
  2 
  3 using System;
  4 using System.Web;
  5 using System.Xml;
  6 using System.Text;
  7 
  8 public class Area : IHttpHandler {
  9     
 10     public void ProcessRequest (HttpContext context) {
 11         context.Response.ContentType = "application/json";
 12         string action = context.Request.Params["action"];
 13         string param = context.Request.Params["param"];
 14         string[] paramss = { string.Empty, string.Empty };
 15         if (param != string.Empty)
 16         {
 17             paramss = param.Split(',');
 18         }
 19         string result = string.Empty;
 20         if (action == "province")
 21         {
 22             if (paramss[1] == string.Empty)
 23             {
 24                 result = GetProvince();
 25             }
 26             else if (paramss[1] == "c")
 27             {
 28                 result = GetCity(HttpUtility.UrlDecode(paramss[0], Encoding.GetEncoding("utf-8")));
 29             }
 30             else
 31             {
 32                 result = GetArea(HttpUtility.UrlDecode(paramss[0], Encoding.GetEncoding("utf-8")));
 33             }
 34         }
 35         context.Response.Write(result);
 36         context.Response.End();
 37     }
 38 
 39 
 40     private string GetProvince()
 41     {
 42         XmlDocument doc = new XmlDocument();
 43         GetXmlDocumnet(doc);
 44         XmlNodeList lists = doc.SelectNodes(@"/address/province");
 45         string provinceJson = GetXmlToJson(lists);
 46         return provinceJson;
 47     }
 48 
 49     private string GetCity(string provname)
 50     {
 51         XmlDocument doc = new XmlDocument();
 52         GetXmlDocumnet(doc);
 53         string xpath = string.Format("/address/province[@name='{0}']/city", provname);//要研究
 54         XmlNodeList lists = doc.SelectNodes(xpath);
 55         string cityJson = GetXmlToJson(lists);
 56         return cityJson;
 57     }
 58 
 59     private string GetArea(string cityname)
 60     {
 61         XmlDocument doc = new XmlDocument();
 62         GetXmlDocumnet(doc);
 63         string xpath = string.Format("/address/province/city[@name='{0}']/country", cityname);
 64         XmlNodeList lists = doc.SelectNodes(xpath);
 65         string cityJson = GetXmlToJson(lists);
 66         return cityJson;
 67     }
 68 
 69 
 70     private void GetXmlDocumnet(XmlDocument doc)
 71     {
 72         string path = HttpContext.Current.Server.MapPath("./xml/Area.xml");
 73         doc.Load(path);
 74     }
 75 
 76 
 77     private string GetXmlToJson(XmlNodeList nodeList)
 78     {
 79         try
 80         {
 81             System.Text.StringBuilder sb = new System.Text.StringBuilder();
 82             if (nodeList != null)
 83             {
 84                 sb.Append("[");
 85                 foreach (XmlNode node in nodeList)
 86                 {
 87                     sb.Append("{\"name\":\"" + node.Attributes["name"].InnerText + "\"},");
 88                 }
 89                 sb.Remove(sb.Length - 1, 1);
 90                 sb.Append("]");
 91             }
 92             return sb.ToString();
 93         }
 94         catch (Exception ex)
 95         {
 96             throw ex;
 97         }
 98 
 99     }
100  
101     public bool IsReusable {
102         get {
103             return false;
104         }
105     }
106 
107 }

 

xml的代码网上很多。

效果图1:

 

修改时的代码:

返回页面的代码
1  private void gettoHid()
2     {
3         ((HiddenField)this.area1.FindControl("hidProvince")).Value = "河南省";
4         ((HiddenField)this.area1.FindControl("hidCity")).Value = "鹤壁市";
5         ((HiddenField)this.area1.FindControl("hidArea")).Value = "淇县";
6     }

 

效果图2:

 

 

下载源码

转载于:https://www.cnblogs.com/zhaoguochen/archive/2012/07/11/javascript.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值