旗下导航:搜·么
当前位置:网站首页 > .Net教程 > 正文

Asp.Net+jQuery怎样完成省市二级联动功用的要领详解【C#.Net教程】,jQuery,Asp.Net,二级联动

作者:搜教程发布时间:2019-11-27分类:.Net教程浏览:74评论:0


导读:这篇文章重要引见了jQuery+Asp.Net完成省市二级联动功用的要领,触及asp.net数据库读取与字符串转换相干操纵技能,须要的朋侪能够参考下本文实例报告了jQuer...
这篇文章重要引见了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;
    }
  }
}

以上就是Asp.Net+jQuery怎样完成省市二级联动功用的要领详解的细致内容,更多请关注ki4网别的相干文章!

标签:jQueryAsp.Net二级联动


欢迎 发表评论: