Ajax WebService 省级联动
AjaxControlToolkit 控件
新建服务 WebService
WebService.cs
using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; using System.Web.Script.Services; using System.Collections.Generic; using System.Collections.Specialized; using AjaxControlToolkit; using System.Data.SqlClient; using System.Data; /// <summary> /// SNWebService 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ScriptService] public class SNWebService : System.Web.Services.WebService { public SNWebService() { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } /// <summary> /// 获取省份数据 /// </summary> /// <param name="knownCategoryValues"></param> /// <param name="category"></param> /// <returns></returns> [WebMethod] public CascadingDropDownNameValue[] GetProvinceContents(string knownCategoryValues, string category) { List<CascadingDropDownNameValue> provinceList = new List<CascadingDropDownNameValue>(); string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]; SqlConnection sqlConn = new SqlConnection(connectionString); sqlConn.Open(); string strSql = "Select * From Province"; SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn); SqlDataReader dtrProvince = sqlCmd.ExecuteReader(); while (dtrProvince.Read()) { provinceList.Add(new CascadingDropDownNameValue(dtrProvince["Name"].ToString(),dtrProvince["Code"].ToString())); } dtrProvince.Close(); sqlConn.Close(); return provinceList.ToArray(); } /// <summary> /// 获取市数据 /// </summary> /// <param name="knownCategoryValues"></param> /// <param name="category"></param> /// <returns></returns> [WebMethod] public CascadingDropDownNameValue[] GetCityContents(string knownCategoryValues, string category) { StringDictionary provinceList = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]; SqlConnection sqlConn = new SqlConnection(connectionString); sqlConn.Open(); string strSql = "Select * From city Where ProvinceId='" + provinceList["Province"] + "'"; SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn); SqlDataReader dtrCity = sqlCmd.ExecuteReader(); List<CascadingDropDownNameValue> cityList = new List<CascadingDropDownNameValue>(); while (dtrCity.Read()) { cityList.Add(new CascadingDropDownNameValue(dtrCity["Name"].ToString(), dtrCity["code"].ToString())); } dtrCity.Close(); return cityList.ToArray(); } /// <summary> /// 获取乡镇数据 /// </summary> /// <param name="knownCategoryValues"></param> /// <param name="category"></param> /// <returns></returns> [WebMethod] public CascadingDropDownNameValue[] GetViliageContents(string knownCategoryValues, string category) { StringDictionary cityList = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); string connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]; SqlConnection sqlConn = new SqlConnection(connectionString); sqlConn.Open(); string strSql = "Select * From area Where CityId='" + cityList["City"] + "'"; SqlCommand sqlCmd = new SqlCommand(strSql, sqlConn); SqlDataReader dtrViliage = sqlCmd.ExecuteReader(); List<CascadingDropDownNameValue> viliageList = new List<CascadingDropDownNameValue>(); while (dtrViliage.Read()) { viliageList.Add(new CascadingDropDownNameValue(dtrViliage["Name"].ToString(), dtrViliage["id"].ToString())); } dtrViliage.Close(); return viliageList.ToArray(); } }