Asp.Net+jQuery如何?省市二級聯動功能的方法詳解

來源:互聯網
上載者:User
這篇文章主要介紹了jQuery+Asp.Net實現省市二級聯動功能的方法,涉及asp.net資料庫讀取與字串轉換相關操作技巧,需要的朋友可以參考下

本文執行個體講述了jQuery+Asp.Net實現省市二級聯動功能的方法。分享給大家供大家參考,具體如下:

頁面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三級聯動</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() {//載入完成後綁定省份資料$.getJSON("Default.aspx", function(data) { //data的資料格式[{"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; //先清掉縣下拉框的的資料document.getElementById("selCity").options.length=1; //先清掉城市下拉框的的資料$.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; //先清掉縣下拉框的的資料$.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>三級聯動:<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}\""; //構造格式字串 {"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();   //城市或地區的父ID      string sql = "select * from " + type + " where father='" + fid + "'";      //構造資料的類型[{"text":"南昌","value":"0001"},{"text":"上饒","value":"0002"}]      //string strTemp = "{\"text\":\"{0}\",\"value\":\"{1}\"}";//這裡犯了個錯誤:直接這樣構造會出錯,因為大括弧裡又有格式大括弧,解析會出錯      string strTemp = "\"text\":\"{0}\",\"value\":\"{1}\""; //構造格式字串 {"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;    }  }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.