<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
select
{
width:150px;
}
</style>
<script src="Jquery1.7.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$.ajax({
type: "post",
contentType: "application/json",
url: "WebService2.asmx/Loadprovince",
data: "{}",
success: function (result) {
for (var i = 0; i < result.d.length; i++) {
var stroption = '<option value=' + result.d[i].provinceID + '>';
stroption += result.d[i].provincename;
stroption += '</option>';
$('#seprovince').append(stroption);
}
}
})
$('#seprovince').change(function () {
$('#secity option:gt(0)').remove();
$.ajax
({
type: "post",
url: "WebService2.asmx/Getcity",
data: "{provinceid:'" + $(this).val() + "'}",
success: function (result) {
var strcity = '';
for (var i = 0; i < result.d.length; i++) {
strcity += '<option value=' + result.d[i].cityID + '>';
strcity += result.d[i].cityname;
strcity += '</option>';
}
$('#secity').append(strcity);
}
})
})
$('#secity').change(function () {
$('#searea option:gt(0)').remove();
$.ajax({
type: "post",
url: "WebService2.asmx/Getarea",
data: "{cityid:'" + $(this).val() + "'}",
success: function (result) {
var strarea = '';
for (var i = 0; i < result.d.length; i++) {
strarea += '<option value=' + result.d[i].areaID + '>';
strarea += result.d[i].areaname;
strarea += '</option>';
}
$('#searea').append(strarea);
}
})
})
})
</script>
</head>
<body>
<div>
<table>
<tr>
<td>省</td>
<td>
<select id="seprovince">
<option>--请选择--</option>
</select>
</td>
</tr>
<tr>
<td>市</td>
<td>
<select id="secity">
<option>--请选择--</option>
</select>
</td>
</tr>
<tr>
<td>县</td>
<td>
<select id="searea">
<option>--请选择--</option>
</select>
</td>
</tr>
</table>
</div>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace 三联动
{
/// <summary>
/// WebService2 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolBoxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService2 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public List<WebAJAX.Model.province> Loadprovince()
{
WebAJAX.BLL.province bprovince = new WebAJAX.BLL.province();
List<WebAJAX.Model.province> list = bprovince.GetListModel();
return list;
}
[WebMethod]
public List<WebAJAX.Model.city> Getcity(string provinceid)
{
WebAJAX.BLL.city bcity = new WebAJAX.BLL.city();
List<WebAJAX.Model.city> list = bcity.GetListModel("father='"+provinceid+"'");
return list;
}
[WebMethod]
public List<WebAJAX.Model.area> Getarea(string cityid)
{
WebAJAX.BLL.area bllarea = new WebAJAX.BLL.area();
List<WebAJAX.Model.area> list= bllarea.GetListModel("father='" + cityid + "'");
return list;
}
}
}
三层BLL代码
省
public List<Model.province> GetListModel()
{
return dal.GetListModel();
}
市
public List<Model.city> GetListModel(string strsql)
{
return dal.GetListModel(strsql);
}
县
public List<Model.area> GetListModel(string strsql)
{
return dal.GetListModel(strsql);
}
三层DAL代码
省
public System.Collections.Generic.List<Model.province> GetListModel()
{
System.Collections.Generic.List<Model.province> list = new System.Collections.Generic.List<Model.province>();
DataTable dt= GetList("").Tables[0];
foreach (DataRow row in dt.Rows)
{
Model.province mpro = new Model.province();
mpro.id = Convert.ToInt32(row["id"]);
mpro.provinceID = row["provinceID"].ToString();
mpro.provincename = row["provincename"].ToString();
list.Add(mpro);
}
return list;
}
市
public System.Collections.Generic.List<Model.city> GetListModel(string strsql)
{
System.Collections.Generic.List<Model.city> list = new System.Collections.Generic.List<Model.city>();
DataTable dt = GetList(strsql).Tables[0];
foreach (DataRow row in dt.Rows)
{
Model.city mcity = new Model.city();
mcity.id = Convert.ToInt32(row["id"]);
mcity.cityID = row["cityID"].ToString();
mcity.cityname = row["cityname"].ToString();
list.Add(mcity);
}
return list;
}
县
public System.Collections.Generic.List<Model.area> GetListModer(string strsql) { System.Collections.Generic.List<Model.area> list = new System.Collections.Generic.List<Model.area>(); DataTable dt= GetList(strsql).Tables[0]; foreach (DataRow row in dt.Rows) { Model.area marea = new Model.area(); marea.id = Convert.ToInt32(row["id"]); marea.areaID = row["areaID"].ToString(); marea.areaname = row["areaname"].ToString(); list.Add(marea); } return list; }
原文链接:https://www.f2er.com/ajax/166327.html