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; } } }
感谢各位的阅读!看完上述内容,你们对Asp.Net实现省市二级联动功能的方法大概了解了吗?希望文章内容对大家有所帮助。如果想了解更多相关文章内容,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。