asp.net 使用jquery 和ajax 实现三级联动

  1. 前台: 
  2.  
  3. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
  4.  
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  6.  
  7. <html xmlns="http://www.w3.org/1999/xhtml"> 
  8. <head runat="server"> 
  9.     <title></title> 
  10.     <style type="text/css"> 
  11.         #Select1 
  12.         { 
  13.             height: 21px; 
  14.             width: 95px; 
  15.         } 
  16.         #Select2 
  17.         { 
  18.             height: 21px; 
  19.             width: 90px; 
  20.         } 
  21.         #Select3 
  22.         { 
  23.             height: 21px; 
  24.             width: 103px; 
  25.         } 
  26.     </style> 
  27.     <script src="Jquery1.7.js" type="text/javascript"></script> 
  28.     <script type="text/javascript"> 
  29.         $(function () { 
  30.             $.ajax({ 
  31.                 type: "post", 
  32.                 contentType: "application/json", 
  33.                 url: "WebService.asmx/province", 
  34.                 data: "{}", 
  35.                 success: function (result) { 
  36.                     var array = result.d.split(","); 
  37.                     for (var i = 0; i < array.length - 1; i++) { 
  38.                         var array1 = array[i].split("|"); 
  39.                         $("#DropDownList1").append("<option value=" + array1[0] + ">" + array1[1] + "</option>"); 
  40.                     } 
  41.                 } 
  42.  
  43.             }) 
  44.             $("#DropDownList1").change(function () { 
  45.                 document.getElementById('DropDownList2').length = 0
  46.                 $.ajax({ 
  47.                     type: "post", 
  48.                     contentType: "application/json", 
  49.                     url: "WebService.asmx/city", 
  50.                     data: "{drop1:" + $(this).val() + "}", 
  51.                     success: function (result) { 
  52.                         var array = result.d.split(","); 
  53.                         for (var i = 0; i < array.length - 1; i++) { 
  54.                             var array1 = array[i].split("|"); 
  55.                             $("#DropDownList2").append("<option value=" + array1[0] + ">" + array1[1] + "</option>"); 
  56.                         } 
  57.                     } 
  58.                 }) 
  59.             }) 
  60.             $("#DropDownList2").change(function () { 
  61.                 document.getElementById('DropDownList3').length = 0
  62.                 $.ajax({ 
  63.                     type: "post", 
  64.                     contentType: "application/json", 
  65.                     url: "WebService.asmx/area", 
  66.                     data: "{drop2:" + $(this).val() + "}", 
  67.                     success: function (result) { 
  68.                         var array = result.d.split(","); 
  69.                         for (var i = 0; i < array.length - 1; i++) { 
  70.                             var array1 = array[i].split("|"); 
  71.                             $("#DropDownList3").append("<option value=" + array1[0] + ">" + array1[1] + "</option>"); 
  72.                         } 
  73.                     } 
  74.                 }) 
  75.             }) 
  76.         }) 
  77.     </script> 
  78. </head> 
  79. <body> 
  80.     <form id="form1" runat="server"> 
  81.     <div> 
  82.         <asp:DropDownList ID="DropDownList1" runat="server"> 
  83.         </asp:DropDownList> 
  84.         <asp:DropDownList ID="DropDownList2" runat="server"> 
  85.         </asp:DropDownList> 
  86.         <asp:DropDownList ID="DropDownList3" runat="server"> 
  87.         </asp:DropDownList> 
  88.          
  89.      </div> 
  90.     </form> 
  91. </body> 
  92. </html> 
  93. webservice: 
  94.  
  95. using System; 
  96. using System.Collections.Generic; 
  97. using System.Linq; 
  98. using System.Web; 
  99. using System.Web.Services; 
  100. using System.Text; 
  101. using System.Data; 
  102. using System.Data.SqlClient; 
  103. using System.Configuration; 
  104.  
  105.  
  106. /// <summary> 
  107. ///WebService 的摘要说明 
  108. /// </summary> 
  109. [WebService(Namespace = "http://tempuri.org/")] 
  110. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
  111. //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。  
  112. [System.Web.Script.Services.ScriptService] 
  113. public class WebService : System.Web.Services.WebService { 
  114.     string sqlstr=ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString; 
  115.     public WebService () { 
  116.  
  117.         //如果使用设计的组件,请取消注释以下行  
  118.         //InitializeComponent();  
  119.     } 
  120.  
  121.     [WebMethod] 
  122.     public string HelloWorld() { 
  123.         return "Hello World"; 
  124.     } 
  125.     [WebMethod] 
  126.     public string province() 
  127.     { 
  128.         using (SqlConnection sqlcnn = new SqlConnection(sqlstr)) 
  129.         { 
  130.             using (SqlCommand sqlcmm=sqlcnn.CreateCommand()) 
  131.             { 
  132.                 sqlcmm.CommandText = "select * from province"
  133.                 DataTable dt = new DataTable(); 
  134.                 SqlDataAdapter adapt = new SqlDataAdapter(sqlcmm); 
  135.                 adapt.Fill(dt); 
  136.                 StringBuilder sb1 = new StringBuilder(); 
  137.                 for (int i = 0; i < dt.Rows.Count; i++) 
  138.                 { 
  139.                     string proid = dt.Rows[i]["provinceID"].ToString(); 
  140.                     string proname = dt.Rows[i]["province"].ToString(); 
  141.                     sb1.Append(proid); 
  142.                     sb1.Append("|"); 
  143.                     sb1.Append(proname); 
  144.                     sb1.Append(","); 
  145.                 } 
  146.                 return sb1.ToString(); 
  147.             } 
  148.         } 
  149.     } 
  150.     [WebMethod] 
  151.     public string city(int drop1) 
  152.     { 
  153.         using (SqlConnection sqlcnn=new SqlConnection(sqlstr)) 
  154.         { 
  155.             using (SqlCommand sqlcmm=sqlcnn.CreateCommand()) 
  156.             { 
  157.                 sqlcmm.CommandText = "select * from city where father=@father"
  158.                 sqlcmm.Parameters.AddWithValue("@father", drop1); 
  159.                 DataTable dt = new DataTable(); 
  160.                 SqlDataAdapter adapt = new SqlDataAdapter(sqlcmm); 
  161.                 adapt.Fill(dt); 
  162.                 StringBuilder sb2 = new StringBuilder(); 
  163.                 for (int i = 0; i < dt.Rows.Count; i++) 
  164.                 { 
  165.                     string proid = dt.Rows[i]["cityID"].ToString(); 
  166.                     string proname = dt.Rows[i]["city"].ToString(); 
  167.                     sb2.Append(proid); 
  168.                     sb2.Append("|"); 
  169.                     sb2.Append(proname); 
  170.                     sb2.Append(","); 
  171.                 } 
  172.                 return sb2.ToString(); 
  173.             } 
  174.         } 
  175.     } 
  176.     [WebMethod] 
  177.     public string area(int drop2) 
  178.     { 
  179.         using (SqlConnection sqlcnn = new SqlConnection(sqlstr)) 
  180.         { 
  181.             using (SqlCommand sqlcmm = sqlcnn.CreateCommand()) 
  182.             { 
  183.                 sqlcmm.CommandText = "select * from area where father=@father"
  184.                 sqlcmm.Parameters.AddWithValue("@father", drop2); 
  185.                 DataTable dt = new DataTable(); 
  186.                 SqlDataAdapter adapt = new SqlDataAdapter(sqlcmm); 
  187.                 adapt.Fill(dt); 
  188.                 StringBuilder sb3 = new StringBuilder(); 
  189.                 for (int i = 0; i < dt.Rows.Count; i++) 
  190.                 { 
  191.                     string proid = dt.Rows[i]["areaID"].ToString(); 
  192.                     string proname = dt.Rows[i]["area"].ToString(); 
  193.                     sb3.Append(proid); 
  194.                     sb3.Append("|"); 
  195.                     sb3.Append(proname); 
  196.                     sb3.Append(","); 
  197.                 } 
  198.                 return sb3.ToString(); 
  199.             } 
  200.         } 
  201.     } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值