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(); } }