溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Asp.Net實現(xiàn)省市二級聯(lián)動功能的方法

發(fā)布時間:2020-10-16 15:31:26 來源:億速云 閱讀:148 作者:小新 欄目:編程語言

Asp.Net實現(xiàn)省市二級聯(lián)動功能的方法?這個問題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家?guī)淼膮⒖純?nèi)容,讓我們一起來看看吧!

頁面html:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ddlAjax.aspx.cs" Inherits="ThreeAjaxDrop_ddlAjax" %>
<!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>DropDownList三級聯(lián)動</title>
<style type="text/css">
*{margin:0; padding:0;}
body{font-size:12px; font-family:Arial @宋體;}
</style>
<script type="text/javascript" src="../js/jquery-1.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
//加載完成后綁定省份數(shù)據(jù)
$.getJSON("Default.aspx", function(data) { //data的數(shù)據(jù)格式[{"text":"北京","value":"0001"},{"text":"江西","value":"0031"}]
//alert(data[0].text+"|"+data[0].value);
$.each(data, function(index, value) {
//alert(value.text + "|" + value.value);
$("#selProvince").append("<option value='" + value.value + "'>" + value.text + "</option>");
});
});
//省份的值改變,則要綁定出城市下拉框
$("#selProvince").change(function(){
document.getElementById("selArea").options.length=1; //先清掉縣下拉框的的數(shù)據(jù)
document.getElementById("selCity").options.length=1; //先清掉城市下拉框的的數(shù)據(jù)
$.getJSON("HandlerDropDownAjax.ashx",{"type":"city","fid":$(this).val()},function(data){
$.each(data, function(index, value) {
$("#selCity").append("<option value='" + value.value + "'>" + value.text + "</option>");
});
});
});
//城市下拉框的值改變
$("#selCity").change(function(){
document.getElementById("selArea").options.length=1; //先清掉縣下拉框的的數(shù)據(jù)
$.getJSON("HandlerDropDownAjax.ashx",{"type":"area","fid":$(this).val()},function(data){
$.each(data, function(index, value) {
$("#selArea").append("<option value='" + value.value + "'>" + value.text + "</option>");
});
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<p>
三級聯(lián)動:<select id="selProvince">
<option value="選擇省份">==選擇省份==</option>
</select> <select id="selCity"><option>==選擇城市==</option></select>& amp;nbsp; <select id="selArea"><option>==選擇縣==</option></select>
</p>
</form>
</body>
</html>

asp.net部分:

(1)Default.aspx.cs

public partial class ThreeAjaxDrop_Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    string sql = "select * from province";
    string strTemp = "\"text\":\"{0}\",\"value\":\"{1}\""; //構(gòu)造格式字符串 {"text":"北京","value":"00001"}
    StringBuilder sb = new StringBuilder();
    OleDbDataReader reader = OleDBHelper.ExecuteReader(sql);
    while (reader.Read())
    {
      string str1 = string.Format(strTemp, reader["province"].ToString(), reader["provinceID"].ToString());
      sb.Append("{"+str1+"},");
    }
    reader.Close();
    string json = sb.ToString();
    Response.Write("["+json.Substring(0,json.Length-1)+"]");
  }
}

(2)HandlerDropDownAjax.ashx

public class HandlerDropDownAjax : IHttpHandler {
  public void ProcessRequest (HttpContext context) {
    if (context.Request.QueryString["type"] != null && context.Request.QueryString["fid"] != null)
    {
      string type = context.Request.QueryString["type"].ToString(); //主要用于識別是查詢city還是area表
      string fid = context.Request.QueryString["fid"].ToString();   //城市或區(qū)域的父ID
      string sql = "select * from " + type + " where father='" + fid + "'";
      //構(gòu)造數(shù)據(jù)的類型[{"text":"南昌","value":"0001"},{"text":"上饒","value":"0002"}]
      //string strTemp = "{\"text\":\"{0}\",\"value\":\"{1}\"}";//這里犯了個錯誤:直接這樣構(gòu)造會出錯,因為大括號里又有格式大括號,解析會出錯
      string strTemp = "\"text\":\"{0}\",\"value\":\"{1}\""; //構(gòu)造格式字符串 {"text":"北京","value":"00001"}
      StringBuilder sb = new StringBuilder();
      OleDbDataReader reader = OleDBHelper.ExecuteReader(sql);
      while (reader.Read())
      {
        string str1 = string.Format(strTemp, reader[2].ToString(), reader[1].ToString());
        sb.Append("{" + str1 + "},"); //兩邊的大括號格式化后加上
      }
      reader.Close();
      string json = sb.ToString();
      context.Response.Write("[" + json.Substring(0, json.Length - 1) + "]"); //Substring的作用是去掉最后一個'逗號'
    }
  }
  public bool IsReusable {
    get {
      return false;
    }
  }
}

感謝各位的閱讀!看完上述內(nèi)容,你們對Asp.Net實現(xiàn)省市二級聯(lián)動功能的方法大概了解了嗎?希望文章內(nèi)容對大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI