您的位置:首页 > 运维架构

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

2007-08-21 11:08 781 查看
1.首先准备三个 dropdownlist


<asp:DropDownList ID="Drop1" runat="server">


</asp:DropDownList> 


<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;


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: