using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
using KingClub.DBUtility;
/// <summary>
/// ProvCityArea 的摘要说明
/// </summary>
public class AjaxMethod
{
public AjaxMethod()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region GetPovinceList
public static DataSet GetPovinceList()
{
string sql = "";
return GetDataSet(sql);
}
#endregion
#region GetCityList
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public DataSet GetCityList(int povinceid)
{
string sql = "select * from ;
return GetDataSet(sql);
}
#endregion
#region GetAreaList
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public DataSet GetAreaList(int cityid)
{
string sql = " father=" + cityid;
return GetDataSet(sql);
}
#endregion
#region GetDataSet
public static DataSet GetDataSet(string sql)
{
//string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
string ConnectionString = SqlHelper.ConnectionStringLocalTransaction;
SqlDataAdapter sda = new SqlDataAdapter(sql, ConnectionString);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
#endregion
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>省市县3级连动</title>
<script type="text/javascript">
//选择城市触发的脚本
function cityResult()
{
var city=document.getElementById("DDLProvince");
//调用服务器ajax方法绑定城市
AjaxMethod.GetCityList(city.value,get_city_Result_CallBack);
}
//省份选择后绑定城市的脚本
function get_city_Result_CallBack(response)
{
if (response.value != null)
{
document.all("DDLCity").length=0;
//获得ajax城市绑定方法返回的结果
var ds = response.value;
if(ds != null && typeof(ds) == "object" && ds.Tables != null)
{
for(var i=0; i<ds.Tables[0].Rows.length; i++)
{
var name=ds.Tables[0].Rows[i].city;
var id=ds.Tables[0].Rows[i].cityID;
document.all("DDLCity").options.add(new Option(name,id));
}
//如果有城市被选种调用服务器上的地区绑定方法
if(ds.Tables[0].Rows.length>0)
{
//调用服务器端的ajax方法返回地区的绑定
AjaxMethod.GetAreaList(ds.Tables[0].Rows[0].cityID,get_area_Result_CallBack);
}
}
document.all("DDLArea").length=0;
}
return
}
//选择市区要触发的脚本
function areaResult()
{
var area=document.getElementById("DDLCity");
//市区选择时调用服务器上的ajax方法绑定地区信息
AjaxMethod.GetAreaList(area.value,get_area_Result_CallBack);
}
function get_area_Result_CallBack(response)
{
if (response.value != null)
{
document.all("DDLArea").length=0;
var ds = response.value;
if(ds != null && typeof(ds) == "object" && ds.Tables != null)
{
for(var i=0; i<ds.Tables[0].Rows.length; i++)
{
var name=ds.Tables[0].Rows[i].area;
var id=ds.Tables[0].Rows[i].areaID;
document.all("DDLArea").options.add(new Option(name,id));
}
}
}
return
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="Table1" style="z-index: 101; left: 96px; position: absolute; top: 32px"
cellspacing="1" cellpadding="1" width="300" border="1" bgcolor="#ccff66">
<tr>
<td>
省市</td>
<td>
<asp:DropDownList ID="DDLProvince" runat="server" Width="192px">
</asp:DropDownList></td>
</tr>
<tr>
<td>
城市</td>
<td>
<asp:DropDownList ID="DDLCity" runat="server" Width="191px">
</asp:DropDownList></td>
</tr>
<tr>
<td>
市区</td>
<td>
<asp:DropDownList ID="DDLArea" runat="server" Width="191px">
</asp:DropDownList></td>
</tr>
</table>
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));
if (!Page.IsPostBack)
{
AjaxMethod objajax = new AjaxMethod();
DataView dvProvince = new DataView(AjaxMethod.GetPovinceList().Tables[0]);
this.DDLProvince.DataSource = dvProvince;
this.DDLProvince.DataTextField = "province";
this.DDLProvince.DataValueField = "provinceID";
this.DDLProvince.DataBind();
int provinceID = 0;
if(!string.IsNullOrEmpty(dvProvince[0]["provinceID"].ToString()))
{
provinceID = Convert.ToInt32(dvProvince[0]["provinceID"]);
}
DataView dvCity = new DataView(objajax.GetCityList(provinceID).Tables[0]);
this.DDLCity.DataSource = dvCity;
this.DDLCity.DataTextField = "city";
this.DDLCity.DataValueField = "cityID";
this.DDLCity.DataBind();
int areaID = 0;
if (!string.IsNullOrEmpty(dvCity[0]["cityID"].ToString()))
{
areaID =
}
DataView dvArea = new DataView(objajax.GetAreaList(areaID).Tables[0]);
this.DDLArea.DataSource =
this.DDLArea.DataTextField = "
this.DDLArea.DataValueField = "areaID";
this.DDLArea.DataBind();
this.DDLProvince.Attributes.Add("onclick", "cityResult();");
this.DDLCity.Attributes.Add("onclick", "areaResult();");
}
}