动态加载省市区

数据库表结构:
在这里插入图片描述
SQL语句:

CREATE TABLE [Cascade]
(
ID int,
Name nvarchar(50),
ParentID int,
[Type] int
)
//aspx代码

省:

市:

区:

//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();
}

/// 
/// 查省
/// 
/// 
public string GetProvince()
{
    DataTable dt = CascadeDB.GetProvince(1);
    List lst = new List();
    lst.Add(new
    {
        id = -1,
        text = "所有省"
    });
    lst.AddRange(dt.AsEnumerable().Select(p => new
    {
        id = p.Field("ID"),
        text = p.Field("Name")
    }).ToList());
    return JsonConvert.SerializeObject(lst);
}

/// 
/// 查市
/// 
private void GetCity()
{
    int ParentID = Utils.ReqIntParams("ParentID", -1);
    DataTable dt = CascadeDB.GetCityArea(2, ParentID);
    List lst = new List();
    lst.Add(new
    {
        id = -1,
        text = "所有市"
    });
    lst.AddRange(dt.AsEnumerable().Select(p => new
    {
        id = p.Field("ID"),
        text = p.Field("Name")
    }).ToList());
    Response.Write(JsonConvert.SerializeObject(lst));
}

/// 
/// 查区
/// 
/// 
public void GetArea()
{
    int ParentID = Utils.ReqIntParams("ParentID", -1);
    DataTable dt = CascadeDB.GetCityArea(3, ParentID);
    List lst = new List();
    lst.Add(new
    {
        id = -1,
        text = "所有区"
    });
    lst.AddRange(dt.AsEnumerable().Select(p => new
    {
        id = p.Field("ID"),
        text = p.Field("Name")
    }).ToList());
    Response.Write(JsonConvert.SerializeObject(lst));
}
        
//CascadeDB.cs代码
//连接字符串
static string strConn = ConfigurationManager.ConnectionStrings["CnnhoRechargePlatformConnectionString"].ToString();

/// 
/// 执行查询,返回DataTable对象
/// 
/// sql语句
/// DataTable对象
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;
}

/// 
/// 查省
/// 
/// 类型
/// 
public static DataTable GetProvince(int Type)
{
    string sql = @"select * from [Cascade] where [Type]= " + Type;
    return GetTable(sql);
}

/// 
/// 查市/区
/// 
/// 类型
/// 父级ID
/// 
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);
}

你可能感兴趣的:(Back-End)