ashx一般处理程序---ajax异步加载省市级联
html页面
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title></title> 6 <meta charset="utf-8" /> 7 <script src="scripts/jquery-1.7.4.js"></script> 8 <script> 9 $(function () { 10 $.getJSON('ajax_ssjl.ashx', { 11 pid: 0 12 }, function (msg) { 13 $.each(msg, function (index, item) { 14 $('#province').append('<option value="' + item.AreaId + '">' + item.AreaName + '</option>'); 15 $('#province').change(); 16 }); 17 $('#province').change(function () { 18 $.getJSON('ajax_ssjl.ashx', { 19 pid: $('#province').val() 20 }, function (msg) { 21 $('#city').empty(); 22 $.each(msg, function (index, item) { 23 $('#city').append('<option value="' + item.AreaId + '">' + item.AreaName + '</option>'); 24 $('#city').change(); 25 }); 26 }); 27 }); 28 29 $('#city').change(function () { 30 $.getJSON('ajax_ssjl.ashx', { 31 pid: $('#city').val() 32 }, function (msg) { 33 $('#county').empty(); 34 $.each(msg, function (index, item) { 35 $('#county').append('<option value="' + item.AreaId + '">' + item.AreaName + '</option>'); 36 $('#county').change(); 37 }); 38 }); 39 }); 40 }); 41 }); 42 </script> 43 </head> 44 <body> 45 <select id="province"><option>省</option></select> 46 <select id="city"><option>市</option></select> 47 <select id="county"><option>县</option></select> 48 </body> 49 </html>
ashx页面
1 public void ProcessRequest(HttpContext context) 2 { 3 context.Response.ContentType = "text/plain"; 4 int pid = int.Parse(context.Request["pid"]); 5 string sql = "select * from AreaFull where AreaPId=@pid"; 6 List<NewsAreas> list = new List<NewsAreas>(); 7 #region DataTable实现方法 8 DataTable dt = SqlHelper.ExecuteDataTable(sql, new SqlParameter("@pid", pid)); 9 foreach (DataRow item in dt.Rows) 10 { 11 list.Add(new NewsAreas() 12 { 13 AreaId=Convert.ToInt32(item["AreaId"]), 14 AreaName = item["AreaName"].ToString(), 15 AreaPId = Convert.ToInt32(item["AreaPId"]) 16 }); 17 } 18 #endregion 19 20 #region SqlDataReader实现方法 21 //using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, new SqlParameter("@pid", pid))) 22 //{ 23 // if (reader.HasRows) 24 // { 25 // while (reader.Read()) 26 // { 27 // NewsAreas model = new NewsAreas(); 28 // model.AreaId = reader.GetInt32(0); 29 // model.AreaPId = reader.GetInt32(2); 30 // model.AreaName = reader.GetString(1); 31 // list.Add(model); 32 // } 33 // } 34 //} 35 36 #endregion 37 38 string json = JsonConvert.SerializeObject(list); 39 context.Response.Write(json); 40 }
浙公网安备 33010602011771号