jQuery+Asp.Net實(shí)現(xiàn)省市二級(jí)聯(lián)動(dòng)功能的方法
本文實(shí)例講述了jQuery+Asp.Net實(shí)現(xiàn)省市二級(jí)聯(lián)動(dòng)功能的方法。分享給大家供大家參考,具體如下:
頁(yè)面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三級(jí)聯(lián)動(dòng)</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"> <div> 三級(jí)聯(lián)動(dòng):<select id="selProvince"> <option value="選擇省份">==選擇省份==</option> </select> <select id="selCity"><option>==選擇城市==</option></select>& amp;nbsp; <select id="selArea"><option>==選擇縣==</option></select> </div> </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(); //主要用于識(shí)別是查詢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è)錯(cuò)誤:直接這樣構(gòu)造會(huì)出錯(cuò),因?yàn)榇罄ㄌ?hào)里又有格式大括號(hào),解析會(huì)出錯(cuò) 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 + "},"); //兩邊的大括號(hào)格式化后加上 } reader.Close(); string json = sb.ToString(); context.Response.Write("[" + json.Substring(0, json.Length - 1) + "]"); //Substring的作用是去掉最后一個(gè)'逗號(hào)' } } public bool IsReusable { get { return false; } } }
更多關(guān)于asp.net相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《asp.net優(yōu)化技巧總結(jié)》、《asp.net字符串操作技巧匯總》、《asp.net操作XML技巧總結(jié)》、《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結(jié)專題》及《asp.net緩存操作技巧總結(jié)》。
希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。
- asp.net省市三級(jí)聯(lián)動(dòng)的DropDownList+Ajax的三種框架(aspnet/Jquery/ExtJs)示例
- ASP.NET MVC下拉框聯(lián)動(dòng)實(shí)例解析
- asp.net DropDownList實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)效果
- ASP.NET中DropDownList和ListBox實(shí)現(xiàn)兩級(jí)聯(lián)動(dòng)功能
- asp.net下使用AjaxPro實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)代碼
- asp.net DropDownList 三級(jí)聯(lián)動(dòng)下拉菜單實(shí)現(xiàn)代碼
- asp.net兩級(jí)聯(lián)動(dòng)(包含添加和修改)
- 適用與firefox ASP.NET無(wú)刷新二級(jí)聯(lián)動(dòng)下拉列表
- ASP.NET實(shí)現(xiàn)級(jí)聯(lián)下拉框效果實(shí)例講解
- ASP.NET Ajax級(jí)聯(lián)DropDownList實(shí)現(xiàn)代碼
相關(guān)文章
MVC4制作網(wǎng)站教程第二章 部分用戶功能實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了MVC4制作網(wǎng)站教程,部分用戶功能實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08頁(yè)面導(dǎo)出為Excel的時(shí)間格式的問(wèn)題
今天在網(wǎng)上搞了半天,終于搞定了這個(gè)問(wèn)題。發(fā)文慶祝。2008-11-11C# .Net動(dòng)態(tài)調(diào)用webService實(shí)現(xiàn)思路及代碼
動(dòng)態(tài)調(diào)用web服務(wù)將執(zhí)行以下步驟:獲取WSDL/生成客戶端代理類代碼/設(shè)定編譯參數(shù)/編譯代理類/生成代理實(shí)例,并調(diào)用方法,很詳細(xì)的,感興趣的你可不要錯(cuò)過(guò)了哈2013-02-02.net中如何以純二進(jìn)制的形式在內(nèi)存中繪制一個(gè)對(duì)象
這篇文章主要介紹了如何以純二進(jìn)制的形式在內(nèi)存中繪制一個(gè)對(duì)象,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07AJAX使用post發(fā)送數(shù)據(jù)xml格式接受數(shù)據(jù)
AJAX使用post發(fā)送數(shù)據(jù)xml格式接受數(shù)據(jù),需要的朋友可以參考一下2013-03-03ASP.NET中實(shí)現(xiàn)獲取調(diào)用方法名
這篇文章主要介紹了ASP.NET中實(shí)現(xiàn)獲取調(diào)用方法名的技巧,較為詳細(xì)的講述了相關(guān)的命名空間的調(diào)用及語(yǔ)句執(zhí)行順序,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12ASP.NET搭配Ajax實(shí)現(xiàn)搜索提示功能
為了更好的用戶體驗(yàn),不論是桌面軟件還是網(wǎng)站,在搜索查詢的輸入中都會(huì)加入提示功能,就像百度搜索一樣!今天筆者就ASP.NET編程介紹一下如何利用Ajax來(lái)實(shí)現(xiàn)搜索信息提示功能。2015-09-09Asp.net FCKEditor 2.6.3 上傳文件沒(méi)有權(quán)限解決方法
到Fckeditor官方網(wǎng)站下載FredCK.FCKeditorV2.vs2005 (asp.net)2009-02-02使用Visual Studio編寫(xiě)單元測(cè)試
本文詳細(xì)講解了使用Visual Studio編寫(xiě)單元測(cè)試的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03