ashx一般处理程序---ajax异步加载---省市级联

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.12.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                     $(#t1).append(<option value=" + item.AreaId + "> + item.AreaName + </option>);
15                     $(#t1).change();
16                 });
17 
18 
19                 $(#t1).change(function () {
20                     $.getJSON(ajax_ssjl.ashx, {
21                         pid: $(#t1).val()
22                     }, function (msg) {
23                         $(#t2).empty();
24                         $.each(msg, function (index, item) {
25                             $(#t2).append(<option value=" + item.AreaId + "> + item.AreaName + </option>);
26                             $(#t2).change();
27                         });
28                     });
29                 });
30 
31                 $(#t2).change(function () {
32                     $.getJSON(ajax_ssjl.ashx, {
33                         pid: $(#t2).val()
34                     }, function (msg) {
35                         $(#t3).empty();
36                         $.each(msg, function (index, item) {
37                             $(#t3).append(<option value=" + item.AreaId + "> + item.AreaName + </option>);
38                             $(#t3).change();
39                         });
40                     });
41                 });
42 
43 
44             });
45         });
46     </script>
47 </head>
48 <body>
49<select id="t1"></select>
50<select id="t2"></select>
51<select id="t3"></select>
52 </body>
53 </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 [email protected]";
 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         }

 

以上是关于ashx一般处理程序---ajax异步加载---省市级联的主要内容,如果未能解决你的问题,请参考以下文章

AJAX 基本结构 数据加载

Ajax 调用(传值)一般处理程序(.ashx)

AJAX基础

.net Ajax与后台一般处理程序(ashx) 交互

AJAX提交到Handler.ashx一般处理程序返回json数据-转

HTML C# ajax结合ashx处理程序实现文件上传