用ajax实现无刷新国家,省份,城市的联动效果,相当于实现dropdownlist联动效果

1.首先准备三个  dropdownlist

            < asp:DropDownList ID = " Drop1 "  runat = " server " >
            
</ asp:DropDownList >& nbsp;
            
< asp:DropDownList ID = " Drop2 "  runat = " server " >
            
</ asp:DropDownList >
            
< asp:DropDownList ID = " Drop3 "  runat = " server " >
            
</ asp:DropDownLis >

2.三个CascadingDropDown

       < cc1:CascadingDropDown ID = " CascadingDropDown1 "  runat = " server "  TargetControlID = " Drop1 "  PromptText = " 请选择国家 "  Category = " Country "  ServiceMethod = " GetPlace " >
            
</ cc1:CascadingDropDown >
        
< cc1:CascadingDropDown ID = " CascadingDropDown2 "  runat = " server "  TargetControlID = " Drop2 "  ParentControlID = " Drop1 "  Category = " Province "   PromptText = " 请选择省份 "  LoadingText = " 正在加载省(地区)信息 "  ServiceMethod = " GetPlace "   >
        
</ cc1:CascadingDropDown >
        
< cc1:CascadingDropDown ID = " CascadingDropDown3 "  runat = " server "  TargetControlID = " Drop3 "  Category = " City "  PromptText = " 请选择城市 "  LoadingText = " 正在加载城市信息 "  ParentControlID = " Drop2 "  ServiceMethod = " GetPlace "   >
        
</ cc1:CascadingDropDown >

3.后台的Getplace代码,实现更新

  [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    
public   static  CascadingDropDownNameValue[] GetPlace( string  knownCategoryValues,  string  category)
    
{
        
string[] categoryValues = knownCategoryValues.Split(':'';');
        
string selectCommand;
        
bool isCountry = false ;

        
switch (category)
        
{
            
case "Countries"
                selectCommand 
= "SELECT * FROM [dbo].[tbCountryRegion] WHERE [RegionType] = 1";
                isCountry 
= true;
                
break;
            
case "Provinces"
                selectCommand 
= "SELECT * FROM [dbo].[tbCountryRegion] WHERE [RegionType] = 2 AND [RegionID] LIKE '" + categoryValues[1+ "%'";
                
break;
            
case "City"
                selectCommand 
= "SELECT * FROM [dbo].[tbCountryRegion] WHERE [RegionType] = 3 AND [RegionID] LIKE '" + categoryValues[3+ "%'";
                
break;
            
default:
                
return null;
        }


        
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLConnectionString"].ConnectionString))
        
{
            SqlCommand sqlcmd 
= new SqlCommand(selectCommand, conn);

            conn.Open();

            SqlDataReader reader 
= sqlcmd.ExecuteReader();

            List
<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

            
while (reader.Read())
            
{
                values.Add(
new CascadingDropDownNameValue(reader[1].ToString(), reader[0].ToString()));
            }


            reader.Close();

            
//
            
// 当数据库里面国家没有符合条件的数据的时候,就采用默认的值
            
//
            if (values.Count == 0 && isCountry)
            
{
                values.Add(
new CascadingDropDownNameValue("中国""000"));
            }


            
return values.ToArray();

        }

    }

说明:

1.后台的方法一定要用Static方法,不然就会出问题。

2 . [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]

不能省去,必须要的

3.具体方法就是连接数据库查询了。

4.KnownGategoryValues的字符串形式为:

cascadingdrop1:drop1.value;Cascadingdrop2:drop2.vaue;的形式。说白了就是选择的catigory和具体的值

所以后面就用split('";" ,":")分割成数组,取值。这样就实现了联动的效果。

5.如果用StringDictionary来分离KnownGategoryValues的值的话,要用以下的namespace

using System.Collections;
using System.Web.Services;
using System.Collections.Specialized;

功能代码如下:

 StringDictionary kv  =  CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        
string  selectCommand;
        
string  a;
        
bool  isCountry  =   false ;
        
switch  (category)
        
{
            
case "Country":
                selectCommand 
= "SELECT * FROM [dbo].[tbCode] WHERE [ItemType] = 1";
                isCountry 
= true;
                
break;
            
case "Province":
                a
=kv["Country"];
                selectCommand 
= "SELECT * FROM [dbo].[tbCode] WHERE [ItemType] = 2 AND [ItemValue] LIKE '" + a+ "%'";
                
break;
            
case "City":
                a 
= kv["Province"];
                selectCommand 
= "SELECT * FROM [dbo].[tbCode] WHERE [ItemType] = 3 AND [ItemValue] LIKE '" +a+ "%'";
                
break;
            
default:
                
return null;
        }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值