zoukankan      html  css  js  c++  java
  • jquery 绑定省份和城市

    前台代码:

    <asp:DropDownList runat="server" ID="ddlProvince">
    </asp:DropDownList>
    <asp:DropDownList runat="server" ID="ddlCity">
    </asp:DropDownList>


    后台绑定省份

    复制代码
    protected void Page_Load(object sender, EventArgs e)
    {
    BindDDL();
    ddlProvince.Attributes.Add("onchange", "getCitys(this)");
    }
    public void BindDDL()
    {
    //绑定省份、直辖市和特别行政区
    ddlProvince.Items.Clear();
    DataSet dsProvince = Province.GetAllCES();
    ddlProvince.DataSource = dsProvince;
    ddlProvince.DataTextField = "ProvinceName";
    ddlProvince.DataValueField = "ProvinceID";
    ddlProvince.DataBind();
    ddlProvince.Items.Add(new ListItem("其他", "0"));
    }
    复制代码

    js事件处理getCitys(),为了提高性能,下面不该创建一个个的option对象然后一个个的添加到ddlcity里面,应该放到一个字符串里面然后一起放到optionddlcity里面,这里就不改了。

    复制代码
    function getCitys(ddlProvince) {
    $.ajax({
    url: "SettingsHandler.ashx",
    data: { type: "getCitys", ProvinceID: $(ddlProvince).find("option:selected").val() },
    type: "get",
    cache: false,
    dataType: "json",
    error: function() {
    alert("occur error");
    },
    success: function(data) {
    $('#ddlCity').empty();
    var option0 = $("<option></option>");
    option0.text("不限");
    option0.val("0");
    $('#ddlCity').append(option0);
    if ($(ddlProvince).find("option:selected").val() != "0") {
    var objCitys = eval(data);
    $.each(objCitys.citys, function(i, city) {
    var option = $("<option></option>");
    option.text(city.CityName);
    option.val(city.CityID);
    $('#ddlCity').append(option);
    });
    }
    }
    });
    }
    复制代码

    SettingHandler.ashx文件

    复制代码
    public void ProcessRequest(HttpContext context)
    {
    context.Response.ContentType = "application/json";
    string strjson = "{"JDataList":[]}";
    string strType = context.Request.QueryString["type"].ToString();
    if (strType == "getCitys")//根据省份ID获取对应的城市
    {
    string strProvinceID = context.Request.Form["ProvinceID"].ToString();
    DataSet dsCitys = Province.GetCitysByProvinceID(ValidateHelper.GetInt(strProvinceID));
    if (DataHelper.DataIsNotNull(dsCitys))
    {
    strjson = JsonHelper.DataTableToJSON(dsCitys.Tables[0], "citys");
    }
    }
        return strjson;
    }
    复制代码

    JsonHelper.DataTableToJSON(DataTable dt,string dtName)方法:

    复制代码
    public static string DataTableToJSON(DataTable dt, string dtName)
    {
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);

    using (JsonWriter jw = new JsonTextWriter(sw))
    {
    JsonSerializer ser = new JsonSerializer();
    jw.WriteStartObject();
    jw.WritePropertyName(dtName);
    jw.WriteStartArray();
    foreach (DataRow dr in dt.Rows)
    {
    jw.WriteStartObject();

    foreach (DataColumn dc in dt.Columns)
    {
    jw.WritePropertyName(dc.ColumnName);
    ser.Serialize(jw, dr[dc].ToString());
    }

    jw.WriteEndObject();
    }
    jw.WriteEndArray();
    jw.WriteEndObject();

    sw.Close();
    jw.Close();

    }

    return sb.ToString();
    }
    复制代码

    以上使用了JSON、JQUERY来实现省市的无刷新联动。

  • 相关阅读:
    nginx高性能WEB服务器系列之九--nginx运维故障日常解决方案
    nginx高性能WEB服务器系列之一简介及安装
    nginx高性能WEB服务器系列之八--nginx日志分析与切割
    nginx高性能WEB服务器系列之七--nginx反向代理
    nginx高性能WEB服务器系列之六--nginx负载均衡配置+健康检查
    nginx高性能WEB服务器系列之五--实战项目线上nginx多站点配置
    nginx高性能WEB服务器系列之四配置文件详解
    nginx高性能WEB服务器系列之三版本升级
    nginx高性能WEB服务器系列之二命令管理
    阿里云平台微信告警(基于收费平台)
  • 原文地址:https://www.cnblogs.com/a757956132/p/3906544.html
Copyright © 2011-2022 走看看