jQuery+Asp.Net实现省市二级联动功能的方法
程序员文章站
2024-03-30 21:12:03
本文实例讲述了jquery+asp.net实现省市二级联动功能的方法。分享给大家供大家参考,具体如下:
页面html:
<%@ page language...
本文实例讲述了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"> <div> 三级联动:<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}\""; //构造格式字符串 {"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; } } }
更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net优化技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作xml技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。
希望本文所述对大家asp.net程序设计有所帮助。