![](https://i-blog.csdnimg.cn/blog_migrate/8afa2291424c18a406483527bd684b11.jpeg)
网页前台代码:
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Default.aspx.cs
"
Inherits
=
"
_Default
"
EnableEventValidation
=
"
false
"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<%
@ Register assembly
=
"
AjaxControlToolkit
"
namespace
=
"
AjaxControlToolkit
"
tagprefix
=
"
cc1
"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML 1.0 Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head runat
=
"
server
"
>
<
title
>
无标题页
</
title
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
div align
=
"
center
"
>
<
asp:ScriptManager ID
=
"
ScriptManager1
"
runat
=
"
server
"
>
</
asp:ScriptManager
>
<
fieldset
>
<
legend
>
全国省市县
</
legend
>
<
fieldset style
=
"
width: 265px
"
>
<
legend
>
省
</
legend
>
<
asp:DropDownList ID
=
"
DropDownList1
"
runat
=
"
server
"
AutoPostBack
=
"
True
"
>
</
asp:DropDownList
>
<
cc1:CascadingDropDown ID
=
"
DropDownList1_CascadingDropDown
"
runat
=
"
server
"
Category
=
"
provice
"
Enabled
=
"
True
"
LoadingText
=
"
读取数据中
..
"
PromptText
=
"
请选择省
"
TargetControlID
=
"
DropDownList1
"
ServicePath
=
"
WebService.asmx
"
ServiceMethod
=
"
GetproviceNames
"
>
</
cc1:CascadingDropDown
>
</
fieldset
>
<
fieldset style
=
"
width: 266px
"
>
<
legend
>
市
</
legend
>
<
asp:DropDownList ID
=
"
DropDownList2
"
runat
=
"
server
"
AutoPostBack
=
"
True
"
>
</
asp:DropDownList
>
<
cc1:CascadingDropDown ID
=
"
DropDownList2_CascadingDropDown
"
runat
=
"
server
"
Category
=
"
city
"
Enabled
=
"
True
"
TargetControlID
=
"
DropDownList2
"
LoadingText
=
"
读取数据中
..
"
PromptText
=
"
请选择市
"
ParentControlID
=
"
DropDownList1
"
ServicePath
=
"
WebService.asmx
"
ServiceMethod
=
"
GetCityNames
"
>
</
cc1:CascadingDropDown
>
</
fieldset
>
<
fieldset style
=
"
width: 266px
"
>
<
legend
>
县
</
legend
>
<
asp:DropDownList ID
=
"
DropDownList3
"
runat
=
"
server
"
>
</
asp:DropDownList
>
<
cc1:CascadingDropDown ID
=
"
DropDownList3_CascadingDropDown
"
runat
=
"
server
"
Category
=
"
District
"
TargetControlID
=
"
DropDownList3
"
Enabled
=
"
True
"
LoadingText
=
"
读取数据中
..
"
ParentControlID
=
"
DropDownList2
"
ServicePath
=
"
WebService.asmx
"
ServiceMethod
=
"
GetDistrictNames
"
>
</
cc1:CascadingDropDown
>
</
fieldset
>
</
fieldset
>
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
</
div
>
</
form
>
</
body
>
</
html
>
WebService代码
using
System;
using
System.Collections;
using
System.Linq;
using
System.Web;
using
System.Web.Services;
using
System.Web.Services.Protocols;
using
System.Xml.Linq;
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
//访问数据库命名空间
using
System.Data;
using
System.Data.SqlClient;
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
//访问web.config命名空间
using
System.Web.Configuration;
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
//导入ScriptServiceAttribute类的命名空间
using
System.Web.Script.Services;
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
//返回CascadingDropDownNameValue数组所需的命名空间
using
AjaxControlToolkit;
using
System.Collections.Generic;
using
System.Collections.Specialized;
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
/**/
/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/"
)]
[WebServiceBinding(ConformsTo =
WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class
WebService : System.Web.Services.WebService
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0be121fa5b8988fbabbbc526af3b0fc0.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
public WebService()
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
[WebMethod]
public CascadingDropDownNameValue[] GetproviceNames(string knownCategoryValues, string category)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select [ProvinceID],[ProvinceName] from [S_Province] order by 1";
conn.Open();
try
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
SqlDataReader dr = comm.ExecuteReader();
while (dr.Read())
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
values.Add(new CascadingDropDownNameValue(dr[1].ToString(), dr[0].ToString()));
}
return values.ToArray();
}
finally
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
conn.Close();
}
}
[WebMethod]
public CascadingDropDownNameValue[] GetCityNames(string knownCategoryValues, string category)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
StringDictionary kcv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
if (!kcv.ContainsKey("provice"))
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
return null;
}
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select [CityID],[CityName] from [S_City] where [ProvinceID]=@ProvinceID order by 1";
comm.Parameters.Add("@ProvinceID", SqlDbType.Int).Value = kcv["provice"];
conn.Open();
try
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
SqlDataReader dr = comm.ExecuteReader();
while (dr.Read())
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
values.Add(new CascadingDropDownNameValue(dr[1].ToString(), dr[0].ToString()));
}
return values.ToArray();
}
finally
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
conn.Close();
}
}
[WebMethod]
public CascadingDropDownNameValue[] GetDistrictNames(string knownCategoryValues, string category)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
StringDictionary kcv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
if (!kcv.ContainsKey("city"))
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
return null;
}
List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select [DistrictID],[DistrictName] from [S_District] where [CityID]=@CityID order by 1";
comm.Parameters.Add("@CityID", SqlDbType.Int).Value = kcv["city"];
conn.Open();
try
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
SqlDataReader dr = comm.ExecuteReader();
while (dr.Read())
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
values.Add(new CascadingDropDownNameValue(dr[1].ToString(), dr[0].ToString()));
}
return values.ToArray();
}
finally
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
{
conn.Close();
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
数据库脚本下载链接:
http://files.cnblogs.com/dushouke/PCD_AllData_V1.rar