AJAX级联多个下拉

最近的项目有比较多的关联分类,一开始想偷懒所以选择了Discuz中的级联下拉框,效果如图:

2011030414575240.jpg,

后来觉得自己做起来是简单了,但用户体验不是太理想,所以最后还是改成分成多个下拉框关联选择,当然最好是无刷新。

第一种方式:我选择了Asp.Net AJAX ToolKit中的CascadingDropDown控件

ContractedBlock.gif ExpandedBlockStart.gif XHTML代码
 
   
1 < asp:ScriptManager ID ="ScriptManager1" runat ="server" > </ asp:ScriptManager >
2   < div >
3 < asp:UpdatePanel ID ="UpdatePanel1" runat ="server" >
4 < ContentTemplate >
5 < asp:DropDownList ID ="ddlProvince" runat ="server" >
6 </ asp:DropDownList >< asp:DropDownList ID ="ddlCity" runat ="server" >
7 </ asp:DropDownList >< asp:DropDownList ID ="ddlVilliage" runat ="server" >
8 </ asp:DropDownList >< ajax:CascadingDropDown ID ="CascadingDropDown2" runat ="server" TargetControlID ="ddlProvince" ServicePath ="SNWebService.asmx" ServiceMethod ="GetProvinceContents" Category ="Province" PromptText ="请选择省份" LoadingText ="省份加载中..." >
9 </ ajax:CascadingDropDown >
10 < ajax:CascadingDropDown ID ="CascadingDropDown1" runat ="server" ParentControlID ="ddlProvince" ServicePath ="SNWebService.asmx" ServiceMethod ="GetCityContents" Category ="City" TargetControlID ="ddlCity" PromptText ="请选择城市" LoadingText ="城市加载中..." >
11 </ ajax:CascadingDropDown >
12 < ajax:CascadingDropDown ID ="CascadingDropDown3" runat ="server" Category ="Villiage" LoadingText ="区县加载中..."
13 ParentControlID ="ddlCity" PromptText ="请选择区县" ServiceMethod ="GetViliageContents"
14 ServicePath ="SNWebService.asmx" TargetControlID ="ddlVilliage" >
15 </ ajax:CascadingDropDown >
16 </ ContentTemplate >
17 </ asp:UpdatePanel >
18   </ div >
ContractedBlock.gif ExpandedBlockStart.gif WebService代码
 
   
1 /// <summary>
2 /// 获取省份数据
3 /// </summary>
4 /// <param name="knownCategoryValues"></param>
5 /// <param name="category"></param>
6 /// <returns></returns>
7   [WebMethod]
8 public CascadingDropDownNameValue[] GetProvinceContents( string knownCategoryValues, string category)
9 {
10 List < CascadingDropDownNameValue > provinceList = new List < CascadingDropDownNameValue > ();
11
12
13 string strSql = " Select * From Province " ;
14
15 SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);
16
17 SqlDataReader dtrProvince = sqlCmd.ExecuteReader();
18
19 while (dtrProvince.Read())
20 {
21 provinceList.Add( new CascadingDropDownNameValue(dtrProvince[ " Name " ].ToString(),dtrProvince[ " Code " ].ToString()));
22 }
23
24 dtrProvince.Close();
25
26
27 return provinceList.ToArray();
28 }
29 /// <summary>
30 /// 获取市数据
31 /// </summary>
32 /// <param name="knownCategoryValues"></param>
33 /// <param name="category"></param>
34 /// <returns></returns>
35   [WebMethod]
36 public CascadingDropDownNameValue[] GetCityContents( string knownCategoryValues, string category)
37 {
38 StringDictionary provinceList = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
39
40 string strSql = " Select * From city Where ProvinceId=' " + provinceList[ " Province " ] + " ' " ;
41
42 SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);
43
44 SqlDataReader dtrCity = sqlCmd.ExecuteReader();
45
46 List < CascadingDropDownNameValue > cityList = new List < CascadingDropDownNameValue > ();
47
48 while (dtrCity.Read())
49 {
50 cityList.Add( new CascadingDropDownNameValue(dtrCity[ " Name " ].ToString(), dtrCity[ " code " ].ToString()));
51 }
52
53 dtrCity.Close();
54
55 return cityList.ToArray();
56 }
57 /// <summary>
58 /// 获取乡镇数据
59 /// </summary>
60 /// <param name="knownCategoryValues"></param>
61 /// <param name="category"></param>
62 /// <returns></returns>
63   [WebMethod]
64 public CascadingDropDownNameValue[] GetViliageContents( string knownCategoryValues, string category)
65 {
66 StringDictionary cityList = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
67
68
69 string strSql = " Select * From area Where CityId=' " + cityList[ " City " ] + " ' " ;
70
71 SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn);
72
73 SqlDataReader dtrViliage = sqlCmd.ExecuteReader();
74
75 List < CascadingDropDownNameValue > viliageList = new List < CascadingDropDownNameValue > ();
76
77 while (dtrViliage.Read())
78 {
79 viliageList.Add( new CascadingDropDownNameValue(dtrViliage[ " Name " ].ToString(), dtrViliage[ " id " ].ToString()));
80 }
81
82 dtrViliage.Close();
83
84 return viliageList.ToArray();
85 }

因为只是做个Demo,所以上面的代码随手很乱的写在一起,具体可以自己再封装和优化。

第二种方式:AJAX

ContractedBlock.gif ExpandedBlockStart.gif JS代码
 
   
1 function changetype(ptype) {
2 document.getElementById( " <%= ddlProduct.ClientID%> " ).length = 0 ;
3 $.getJSON( " Handler1.ashx?type= " + ptype, { " resultType " : " json " }, function (data) {
4 if (data != null ) {
5 var data = data.data;
6 for ( var i = 0 ; i < data.length; i ++ ) {
7 document.getElementById( " <%= ddlProduct.ClientID%> " ).options.add( new Option(data[i].TypeName, data[i].ID));
8 }
9 changetypelevel(document.getElementById( " <%= ddlProduct.ClientID%> " ).value);
10 }
11
12 });
13
14 }
15 function changetypelevel(ptypelv) {
16 document.getElementById( " <%= ddlProductLevel.ClientID%> " ).length = 0 ;
17 $.getJSON( " Handler1.ashx?typelv= " + ptypelv, { " resultType " : " json " }, function (data) {
18 if (data != null ) {
19 var data = data.data;
20 for ( var i = 0 ; i < data.length; i ++ ) {
21 document.getElementById( " <%= ddlProductLevel.ClientID%> " ).options.add( new Option(data[i].TypeName, data[i].ID));
22 }
23 }
24 });
25 }
ContractedBlock.gif ExpandedBlockStart.gif 处理程序代码
 
   
1 public void ProcessRequest(HttpContext context)
2 {
3 string ptype = context.Request.Params[ " type " ];
4 string ptypelv = context.Request.Params[ " typelv " ];
5 string result,result1;
6
7 if ( ! string .IsNullOrEmpty(ptype))
8 {
9 result = ConversionRateDetail(ptype);
10 context.Response.Write(result);
11 }
12 if ( ! string .IsNullOrEmpty(ptypelv))
13 {
14 result1 = ConversionRateDetail1(ptypelv);
15
16 context.Response.Write(result1);
17 }
18 }
19
20 private string ConversionRateDetail( string type1)
21 {
22 var sb = new StringBuilder();
23 type.ID = int .Parse(type1);
24 var testlist = Business.Product.GetProductTypeList(type);
25 return " {\"data\": " + Newtonsoft.Json.JsonConvert.SerializeObject(testlist) + " } " ;
26 }
27 private string ConversionRateDetail1( string type2)
28 {
29 var sb = new StringBuilder();
30 type.ID = int .Parse(type2);
31 var testlist1 = Product.GetProductTypeList(type);
32 return " {\"data\": " + Newtonsoft.Json.JsonConvert.SerializeObject(testlist1) + " } " ;
33 }

如果有更好的方式,比如JQuery AJAX的(自己能力所致,试过没实现)等,请不吝赐教,谢谢!

转载于:https://www.cnblogs.com/pfs1314/archive/2011/03/04/1970947.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值