標籤:
本片文章為練習,項目中不會這樣寫:
一:涉及到的知識點:
jQuery Dom操作jQuery Ajax操作ASP.net中的json操作二:用了自動代碼產生器1.Dal層的代碼:
public static partial class BranchTwoService{ /// <summary> /// 根據一級機構的ID得二級機構列表 /// </summary> /// <param name="branchOneId">1級機構ID</param> /// <returns></returns> public static IList<BranchTwo> GetBranchTwoByBranchOneId(int branchOneId) { string strsql = "select * from BranchTwo where BranchOneNo=" + branchOneId; return GetBranchTwosBySql(strsql); }}public static partial class BranchThirdService{ /// <summary> /// 根據二級機構的ID得三級機構列表 /// </summary> /// <param name="branchTwoId">2級機構ID</param> /// <returns></returns> public static IList<BranchThird> GetBranchThirdByBranchTwoId(int branchTwoId) { string strsql = "select * from BranchThird where BranchTwoNo=" + branchTwoId; return GetBranchThirdsBySql(strsql); }}
2.BLL層的代碼:
public static partial class BranchTwoManager { /// <summary> /// 根據一級機構的ID得二級機構列表 /// </summary> /// <param name="branchOneId">1級機構ID</param> /// <returns></returns> public static IList<BranchTwo> GetBranchTwoByBranchOneId(int branchOneId) { return DAL.BranchTwoService.GetBranchTwoByBranchOneId(branchOneId); } } public static partial class BranchThirdManager { /// <summary> /// 根據二級機構的ID得三級機構列表 /// </summary> /// <param name="branchTwoId">2級機構ID</param> /// <returns></returns> public static IList<BranchThird> GetBranchThirdByBranchTwoId(int branchTwoId) { return DAL.BranchThirdService.GetBranchThirdByBranchTwoId(branchTwoId); } }
UI層的代碼:
protected void Page_Load(object sender, EventArgs e) { IList<Models.BranchOne> branchOnes = BLL.BranchOneManager.GetAllBranchOnes(); string json = JsonHelper.Serialize(branchOnes); Response.Write(json); } protected void Page_Load(object sender, EventArgs e) { if (Request["branchoneid"] != null) { int branchoneId = int.Parse(Request.QueryString["branchoneid"]); IList<Models.BranchTwo> branchTwos = BLL.BranchTwoManager.GetBranchTwoByBranchOneId(branchoneId); Response.Write(JsonHelper.Serialize(branchTwos)); } }public partial class BranchThirdHandler : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (Request["branchtwoid"] != null) { int branchtwoId = int.Parse(Request.QueryString["branchtwoid"]); IList<Models.BranchThird> branchThirds= BLL.BranchThirdManager.GetBranchThirdByBranchTwoId(branchtwoId); Response.Write(JsonHelper.Serialize(branchThirds)); } }}
JQuery代碼:
%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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>jQuery+Ajax實戰三級下拉式清單聯動</title> <script src="Scripts/jquery-1.2.6.js" type="text/javascript"></script><%-- <script src="Scripts/jquery-1.2.6-vsdoc-cn.js" type="text/javascript"></script>--%> <script type="text/javascript"> $(document).ready(function() { $("#divLoading").ajaxStart(function() { $(this).show(); }) .ajaxSuccess(function() { $(this).hide() }) .ajaxError(function() { $(this).html("資料載入失敗!"); }); $.getJSON("BranchOneHandler.aspx", function(data) { var $cbxBranchOne = $("#cbxBranchOne"); $.each(data, function(i, json) { var $option = $("<option/>").append(json.branchName).attr("value", json.id); $cbxBranchOne.append($option); }); $cbxBranchOne[0].selectedIndex = 0; $cbxBranchOne.change(function() { var $cbxBranchTwo = $("#cbxBranchTwo"); $cbxBranchTwo.empty(); //清空options var branchoneId = $(this).attr("value"); if (branchoneId) { $.getJSON("BranchTowHandler.aspx", { branchoneid: branchoneId }, function(data) { $.each(data, function(i, json) { var $option = $("<option/>").append(json.branchName).attr("value", json.id); $cbxBranchTwo.append($option); }); }); } $cbxBranchTwo.change(function() { var $cbxBranchThird = $("#cbxBranchThird"); $cbxBranchThird.empty(); var branchtowId = $(this).attr("value"); if (branchtowId) { $.getJSON("BranchThirdHandler.aspx", { branchtwoid: branchtowId }, function(data) { $.each(data, function(i, json) { var $option = $("<option/>").append(json.branchName).attr("value", json.id); $cbxBranchThird.append($option); }); }); } }); }); }); }); </script></head><body style="text-align:center;padding-top:0px;"><div id="divLoading" style="width:50px;height:20px;color;white;background-color:Green;text-align:center;padding-top:3px;position:absolute;top:0px;left:0px;display:none;">Loading...</div> <form id="form1" runat="server"> <div style="padding-top:30px"> <table id="tbBranch" style="width: 361px; border-left-color: #cc00ff; border-bottom-color: #cc00ff; border-top-style: solid; border-top-color: #cc00ff; border-right-style: solid; border-left-style: solid; border-right-color: #cc00ff; border-bottom-style: solid;" border="1"> <tr> <td colspan="3" style="height: 21px; background-color: #cc00ff" align="center"> <span style="color: #ffffff"><strong> jQuery+Ajax實戰三級下拉式清單聯動</strong></span></td> </tr> <tr> <td style="width: 74px; background-color: #cc00ff; height: 38px;"> 一級機構</td> <td colspan="2" style="width: 243px; height: 38px;"> <select id="cbxBranchOne" style="width: 171px"> <option selected="selected"></option> </select></td> </tr> <tr> <td style="width: 74px; background-color: #cc00ff; height: 31px;"> 二級機構</td> <td colspan="2" style="width: 243px; height: 31px;"> <select id="cbxBranchTwo" style="width: 171px"> <option selected="selected"></option> </select> </td> </tr> <tr> <td style="width: 74px; background-color: #cc00ff; height: 29px;"> 三級機構</td> <td colspan="2" style="width: 243px; height: 29px;"> <select id="cbxBranchThird" style="width: 171px"> <option selected="selected"></option> </select> </td> </tr> </table> </div> </form></body></html>
用到的協助類:
using System;using System.Collections.Generic;using System.Web;using System.IO;using System.Text;/// <summary>/// JSON序列化與還原序列化輔助類/// </summary>public class JsonHelper{ /// <summary> /// 用於把對象data序列化為json格式的字串 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <returns></returns> public static string Serialize<T>(T data) { System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(data.GetType()); using (MemoryStream ms = new MemoryStream()) { serializer.WriteObject(ms, data); return Encoding.UTF8.GetString(ms.ToArray()); //ms.Position = 0; //using (StreamReader sr = new StreamReader(ms)) //{ // return sr.ReadToEnd(); //} } } /// <summary> /// 用於把json格式的js對象還原序列化為C#中的類 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="json"></param> /// <returns></returns> public static T Deserialize<T>(string json) { T obj = Activator.CreateInstance<T>(); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) { System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType()); return (T)serializer.ReadObject(ms); } }}
JQuery+Ajax實戰三級下拉式清單聯動(八)