首页 > 其他 > 详细

023-异步新闻分页增删改查

时间:2017-01-27 13:38:17      阅读:373      评论:0      收藏:0      [点我收藏+]

完成新闻分类表crud(page)

-》列表提示:$.parseJSON(‘‘)可以将字符串转成json对象
-》注意:使用$.getJSON()会有缓存,所以需要处理一下url,如加入随机数或时间

TypeList.html

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5     <title></title>
 6     <script src="js/jquery-1.7.1.min.js"></script>
 7     <script>
 8         pCurrentIndex = 1;
 9         $(function () {
10             LoadList(1);
11         });
12 
13         function LoadList(pIndex) {
14             pCurrentIndex = pIndex;
15             $.getJSON(
16                 TypeList.ashx,
17                 {
18                     pIndex: pIndex
19                 },
20                 function (data) {
21                     //{List:[{Id:,Title:}...],PageBar:...}
22                     var list = $(#list);
23                     list.empty();
24                     $.each(data.List, function (index, item) {
25                         list.append(<tr> +
26                             <td> + item.Id + </td> +
27                             <td> + item.Title + </td> +
28                             <td><a href="TypeEdit.aspx?id= + item.Id + ">修改</a></td> +
29                             <td><a href="javascript:RemoveConfirm( + item.Id + )">删除</a></td> +
30                             </tr>);
31                     });
32                     list.append(<tr> +
33                         <td colspan="4" align="center"> + data.PageBar + </td> +
34                         </tr>);
35                     //执行以下代码时,可以获取超链接,进行分页改写
36                     $(.pagebar).each(function () {
37                         var pIndex1 = $(this).attr(href);
38                         $(this).attr(href, javascript:LoadList( + pIndex1 + ));
39                     });
40                 }
41             );
42         }
43 
44         function RemoveConfirm(id) {
45             if (confirm(确定要删除吗?)) {
46                 $.post(
47                     TypeDelete.ashx,
48                     {
49                         id: id
50                     },
51                     function (msg) {
52                         if (msg == 1) {
53                             LoadList(pCurrentIndex);
54                         } else {
55                             alert(删除失败);
56                         }
57                     }
58                 );
59             }
60         }
61     </script>
62 </head>
63 <body>
64     <a href="TypeAdd.html">添加</a>
65     <hr />
66     <table border="1">
67         <tr>
68             <th width="80">编号</th>
69             <th width="100">标题</th>
70             <th width="100">修改</th>
71             <th width="100">删除</th>
72         </tr>
73         <tbody id="list"></tbody>
74     </table>
75 </body>
76 </html>

TypeList.ashx

  1     public class TypeList : IHttpHandler
  2     {
  3 
  4         public void ProcessRequest(HttpContext context)
  5         {
  6             context.Response.ContentType = "text/plain";
  7 
  8             #region 构造分页字符串
  9 
 10             int pageIndex = 1, pageSize = 2;
 11             if (!string.IsNullOrEmpty(context.Request["pIndex"]))
 12             {
 13                 pageIndex = int.Parse(context.Request["pIndex"]);
 14             }
 15             string pagerBar = GetPagerBar(ref pageIndex, pageSize);
 16             #endregion
 17 
 18             #region 得到当前页数据
 19             //构造目标集合
 20             List<ListItem> list = new List<ListItem>();
 21 
 22             //查询得到数据
 23             string sql = "select * from TypeInfoList where rowIndex between @rowStart and @rowEnd";
 24             SqlParameter[] ps =
 25             {
 26                 new SqlParameter("@rowStart",(pageIndex-1)*pageSize+1),
 27                 new SqlParameter("@rowEnd",pageIndex*pageSize)
 28             };
 29             DataTable dt = SqlHelper.GetList(sql, ps);
 30 
 31             //将数据存到目标集合
 32             foreach (DataRow row in dt.Rows)
 33             {
 34                 list.Add(new ListItem()
 35                 {
 36                     Id = Convert.ToInt32(row["TypeId"]),
 37                     Title = row["TypeTitle"].ToString()
 38                 });
 39             }
 40 
 41             #endregion
 42 
 43             //序列化
 44             //{List:[],PageBar:...}
 45             //匿名对象
 46             var result1 = new
 47             {
 48                 List = list,
 49                 PageBar = pagerBar
 50             };
 51 
 52             JavaScriptSerializer js = new JavaScriptSerializer();
 53             string result = js.Serialize(result1);
 54 
 55             context.Response.Write(result);
 56         }
 57 
 58         private string GetPagerBar(ref int pageIndex, int pageSize)
 59         {
 60             StringBuilder sb = new StringBuilder();
 61 
 62             if (pageIndex < 1)
 63             {
 64                 pageIndex = 1;
 65             }
 66 
 67             if (pageIndex == 1)
 68             {
 69                 sb.Append("首页 上一页 ");
 70             }
 71             else
 72             {
 73                 sb.Append("<a href=‘1‘ class=‘pagebar‘>首页</a> ");
 74                 sb.Append("<a href=‘" + (pageIndex - 1) + "‘  class=‘pagebar‘>上一页</a> ");
 75             }
 76 
 77             string sql = "select count(*) from typeinfo";
 78             int rowsCount = Convert.ToInt32(SqlHelper.ExecuteScalar(sql));
 79             int pageCount = Convert.ToInt32(Math.Ceiling(rowsCount * 1.0 / pageSize));
 80 
 81             if (pageIndex > pageCount)
 82             {
 83                 pageIndex = pageCount;
 84             }
 85 
 86             if (pageIndex == pageCount)
 87             {
 88                 sb.Append("下一页 末页");
 89             }
 90             else
 91             {
 92                 sb.Append("<a href=‘" + (pageIndex + 1) + "‘ class=‘pagebar‘>下一页</a> ");
 93                 sb.Append("<a href=‘" + pageCount + "‘ class=‘pagebar‘>末页</a> ");
 94             }
 95             return sb.ToString();
 96         }
 97 
 98         public bool IsReusable
 99         {
100             get
101             {
102                 return false;
103             }
104         }
105     }
106 
107     public class ListItem
108     {
109         public int Id { get; set; }
110         public string Title { get; set; }
111     }

 

023-异步新闻分页增删改查

原文:http://www.cnblogs.com/ninghongkun/p/6353801.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!