<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>省市县无刷新联动</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList ID="ddlProvince" runat="server">
</asp:DropDownList><asp:DropDownList ID="ddlCity" runat="server">
</asp:DropDownList><asp:DropDownList ID="ddlVilliage" runat="server">
</asp:DropDownList><ajax:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="ddlProvince" ServicePath="SNWebService.asmx" ServiceMethod="GetProvinceContents" Category="Province" PromptText="请选择省份" LoadingText="省份加载中...">
</ajax:CascadingDropDown>
<ajax:CascadingDropDown ID="CascadingDropDown1" runat="server" ParentControlID="ddlProvince" ServicePath="SNWebService.asmx" ServiceMethod="GetCityContents" Category="City" TargetControlID="ddlCity" PromptText="请选择城市" LoadingText="城市加载中...">
</ajax:CascadingDropDown>
<ajax:CascadingDropDown ID="CascadingDropDown3" runat="server" Category="Villiage" LoadingText="乡镇加载中..."
ParentControlID="ddlCity" PromptText="请选择乡镇" ServiceMethod="GetViliageContents"
ServicePath="SNWebService.asmx" TargetControlID="ddlVilliage">
</ajax:CascadingDropDown>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
SNWebServices.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 SafetyProduce.DBUtility;
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 strSql = "Select ProvCode,ProvName From dbo.T_Province Where IsActive='1'";
SqlDataReader dtrProvince = DbHelperSQL.ExecuteReader(strSql);
while (dtrProvince.Read())
{
provinceList.Add(new CascadingDropDownNameValue(dtrProvince["ProvName"].ToString(),dtrProvince["ProvCode"].ToString()));
}
dtrProvince.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 strSql = "Select CityCode,CityName From dbo.T_City Where ProvCode='" + provinceList["Province"] + "' And IsActive='1'";
List<CascadingDropDownNameValue> cityList = new List<CascadingDropDownNameValue>();
SqlDataReader dtrCity = DbHelperSQL.ExecuteReader(strSql);
while (dtrCity.Read())
{
cityList.Add(new CascadingDropDownNameValue(dtrCity["CityName"].ToString(), dtrCity["CityCode"].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 strSql = "Select CounCode,CounName From dbo.T_Country Where CityCode='"+ cityList["City"] +"' And IsActive='1'";
List<CascadingDropDownNameValue> viliageList = new List<CascadingDropDownNameValue>();
SqlDataReader dtrViliage = DbHelperSQL.ExecuteReader(strSql);
while (dtrViliage.Read())
{
viliageList.Add(new CascadingDropDownNameValue(dtrViliage["CounName"].ToString(), dtrViliage["CounCode"].ToString()));
}
dtrViliage.Close();
return viliageList.ToArray();
}
}