数据库表结构:
SQL语句:
CREATE TABLE [Cascade]
(
ID int,
Name nvarchar(50),
ParentID int,
[Type] int
)
//aspx代码
<script type="text/javascript">
var Province =<%=GetProvince()%>;
</script>
省:
<select class="easyui-combobox" id="Province" name="Province" style="width: 90px;" data-options="editable: false">
</select>
市:
<select class="easyui-combobox" id="City" name="City" style="width: 90px;" data-options="editable: false">
</select>
区:
<select class="easyui-combobox" id="Area" name="Area" style="width: 90px;" data-options="editable: false">
</select>
//js代码
$(document).ready(function () {
//省
$('#Province').combobox({
data: Province,
valueField: 'id',
textField: 'text',
editable: true,
onChange: function (n, o) {
if (n > 0) {
getCity(n);
}
else {
$('#City').combobox({
data: [{ "id": -1, "text": "所有市" }],
valueField: 'id',
textField: 'text',
editable: true
})
$("#City").combobox("setValue", -1)
}
}
})
$("#Province").combobox("setValue", -1)
//市
$('#City').combobox({
data: [{ "id": -1, "text": "所有市" }],
valueField: 'id',
textField: 'text',
editable: true,
onChange: function (n, o) {
if (n > 0) {
getArea(n);
}
else {
$('#Area').combobox({
data: [{ "id": -1, "text": "所有区" }],
valueField: 'id',
textField: 'text',
editable: true
})
$("#Area").combobox("setValue", -1)
}
}
})
$("#City").combobox("setValue", -1)
//区
$('#Area').combobox({
data: [{ "id": -1, "text": "所有区" }],
valueField: 'id',
textField: 'text',
editable: true
})
$("#Area").combobox("setValue", -1);
});//end ready
//绑定市
function GetCity(ParentID) {
$.ajax({
url: "Cascade.aspx?Action=GetCity&ParentID=" + ParentID,
async: false,
dataType: "json",
success: function (data) {
$('#City').combobox({
data: data,
valueField: 'id',
textField: 'text',
editable: true
});
$("#City").combobox("setValue", -1)
GetArea(ParentID);
},
error: function () { }
})
}
//绑定区
function GetArea(ParentID) {
$.ajax({
url: "Cascade.aspx?Action=GetArea&ParentID=" + ParentID,
async: false,
dataType: "json",
success: function (data) {
$('#Area').combobox({
data: data,
valueField: 'id',
textField: 'text',
editable: true
});
$("#Area").combobox("setValue", -1);
},
error: function () { }
})
}
//aspx.cs代码
protected void Page_Load(object sender, EventArgs e)
{
string Action = Utils.ReqStrParams("Action", "");
switch (Action)
{
//获取市
case "GetCity":
GetCity();
break;
//获取区
case "GetArea":
GetArea();
break;
}
Response.End();
}
/// <summary>
/// 查省
/// </summary>
/// <returns></returns>
public string GetProvince()
{
DataTable dt = CascadeDB.GetProvince(1);
List<dynamic> lst = new List<dynamic>();
lst.Add(new
{
id = -1,
text = "所有省"
});
lst.AddRange(dt.AsEnumerable().Select(p => new
{
id = p.Field<int>("ID"),
text = p.Field<string>("Name")
}).ToList<dynamic>());
return JsonConvert.SerializeObject(lst);
}
/// <summary>
/// 查市
/// </summary>
private void GetCity()
{
int ParentID = Utils.ReqIntParams("ParentID", -1);
DataTable dt = CascadeDB.GetCityArea(2, ParentID);
List<dynamic> lst = new List<dynamic>();
lst.Add(new
{
id = -1,
text = "所有市"
});
lst.AddRange(dt.AsEnumerable().Select(p => new
{
id = p.Field<int>("ID"),
text = p.Field<string>("Name")
}).ToList<dynamic>());
Response.Write(JsonConvert.SerializeObject(lst));
}
/// <summary>
/// 查区
/// </summary>
/// <returns></returns>
public void GetArea()
{
int ParentID = Utils.ReqIntParams("ParentID", -1);
DataTable dt = CascadeDB.GetCityArea(3, ParentID);
List<dynamic> lst = new List<dynamic>();
lst.Add(new
{
id = -1,
text = "所有区"
});
lst.AddRange(dt.AsEnumerable().Select(p => new
{
id = p.Field<int>("ID"),
text = p.Field<string>("Name")
}).ToList<dynamic>());
Response.Write(JsonConvert.SerializeObject(lst));
}
//CascadeDB.cs代码
//连接字符串
static string strConn = ConfigurationManager.ConnectionStrings["CnnhoRechargePlatformConnectionString"].ToString();
/// <summary>
/// 执行查询,返回DataTable对象
/// </summary>
/// <param name="strSQL">sql语句</param>
/// <returns>DataTable对象</returns>
public static DataTable GetTable(string strSQL)
{
DataTable dt = new DataTable(); ;
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
da.Fill(dt);
}
return dt;
}
/// <summary>
/// 查省
/// </summary>
/// <param name="Type">类型</param>
/// <returns></returns>
public static DataTable GetProvince(int Type)
{
string sql = @"select * from [Cascade] where [Type]= " + Type;
return GetTable(sql);
}
/// <summary>
/// 查市/区
/// </summary>
/// <param name="Type">类型</param>
/// <param name="ParentID">父级ID</param>
/// <returns></returns>
public static DataTable GetCityArea(int Type, int ParentID)
{
string sql = String.Format(@"select * from [Cascade] where [Type]={0} and ParentID={1} ", Type, ParentID);
return GetTable(sql);
}