Ajax 实现分页 【无刷新】三层 | GridView 实现分页【有刷新】

前端之家收集整理的这篇文章主要介绍了Ajax 实现分页 【无刷新】三层 | GridView 实现分页【有刷新】前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

sql


动软生成简单的三层


  1. DECLARE @pagesize int;
  2. declare @pageindex int;
  3. select * from(select ROW_NUMBER() over(order by Id) as rownumber,* from T_News1)T
  4. WHERE rownumber>(@pageindex-1)*@pagesize AND rownumber<=@pageindex*@pagesize
  5.  
  6. GO
  7. create proc pro_fenye
  8. @pagesize int,@pageindex int
  9. as
  10. select * from(select ROW_NUMBER() over(order by Id) as rownumber,* from T_News1)T
  11. WHERE rownumber>(@pageindex-1)*@pagesize AND rownumber<=@pageindex*@pagesize
  12. GO
  13.  
  14. EXEC pro_fenye 5,2


HTMI------------ajax生成表格

  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head>
  3. <title></title>
  4. <style type="text/css">
  5. table{ border:solid 1px #444; background-color:Aqua;}
  6. table td{border:solid 1px #444;}
  7. </style>
  8. <script src="js/Jquery1.7.js" type="text/javascript"></script>
  9. <script type="text/javascript">
  10. $(function () {
  11. var pageindex = 1;
  12. var pagesize = 10;
  13. var lastpageindex = 1;
  14. /*如果将代码封装成一个函数,那么除非显示调用(loaddata()),否则函数中的代码不会执行
  15. 根据传递的页码和每页显示的记录数量获取数据
  16. */
  17. function loaddata() {
  18. $.ajax({
  19. type: "post",contentType: "application/json",url: "WebService1.asmx/GetListAjax",data: "{pagesize:" + pagesize + ",pageindex:" + pageindex + "}",success: function (result) {
  20. var strtable = '<table>';
  21. strtable += '<tr><td>编号</td><td>标题</td><td>内容</td><td>创建时间</td></tr>';
  22. for (var i = 0; i < result.d.length; i++) {
  23. strtable += '<tr>';
  24. strtable += '<td>' + result.d[i].Id + '</td>';
  25.  
  26.  
  27. strtable += '<td>' + result.d[i].NewsTitle + '</td>';
  28.  
  29.  
  30. strtable += '<td>' + result.d[i].NewsContent + '</td>';
  31.  
  32.  
  33. strtable += '<td>' + result.d[i].CreateTime + '</td>';
  34. strtable += '</tr>';
  35. }
  36. strtable += '</table>';
  37. $('#mydiv').html(strtable);
  38. }
  39. })
  40. }
  41.  
  42.  
  43. //根据传递到后台的每页显示的记录数量获取最大的页码(就是一共有多少页)
  44. $.ajax({
  45. type: "post",url: "WebService1.asmx/GetLastPageindex",data: "{pagesize:" + pagesize + "}",success: function (result) {
  46. lastpageindex = result.d;
  47. }
  48. })
  49.  
  50.  
  51. //显式调用函数,在页面初次加载时加载第一页数据
  52. loaddata();
  53. //下一页
  54. $('a:eq(2)').click(function () {
  55. if (pageindex < lastpageindex) {
  56. pageindex++;
  57. loaddata();
  58. }
  59.  
  60.  
  61. })
  62. //上一页
  63. $('a:eq(1)').click(function () {
  64. if (pageindex > 1) {
  65. pageindex--;
  66. loaddata();
  67. }
  68. })
  69. //第一页
  70. $('a:first').click(function () {
  71. pageindex = 1;
  72. loaddata();
  73. })
  74. //最后一页
  75. $('a:eq(3)').click(function () {
  76. pageindex = lastpageindex;
  77. loaddata();
  78. })
  79. $('a:last').click(function () {
  80. pageindex = $('#txtPageindex').val();
  81. loaddata();
  82. })
  83. })
  84. </script>
  85. </head>
  86. <body>
  87. <div id="mydiv">
  88. </div>
  89. <div><a href="#">第一页</a><a href="#">上一页</a><a href="#">下一页</a><a href="#">最后一页</a><input
  90. id="txtPageindex" type="text" /><a href="#">Go</a></div>
  91. </body>
  92. </html>
Web.config
  1. <connectionStrings>
  2. <add name="sqlservercon" connectionString="Data Source=Lan-PC;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=478950"/>
  3. </connectionStrings>

DbHelpersql.cs
  1. public static DataTable RunProcedureDataTable(string storedProcName,IDataParameter[] parameters)
  2. {
  3. using (sqlConnection connection = new sqlConnection(connectionString))
  4. {
  5. DataTable dt = new DataTable();
  6. connection.Open();
  7. sqlDataAdapter sqlDA = new sqlDataAdapter();
  8. sqlDA.SelectCommand = BuildQueryCommand(connection,storedProcName,parameters);
  9. sqlDA.Fill(dt);
  10. connection.Close();
  11. return dt;
  12. }
  13. }

WebService1.asmx
  1.  
  1. [WebMethod]
  2. public List<Model.T_News1> GetListAjax(int pagesize,int pageindex)
  3. {
  4. BLL.T_News1 bnews = new BLL.T_News1();
  5. DataTable dt= bnews.GetListDataTable(pagesize,pageindex);
  6.  
  7. List<Model.T_News1> list = new List<Model.T_News1>();
  8.  
  9. int Id;
  10. string newstitle = "";
  11. string newscontent = "";
  12. DateTime createtime;
  13. for (int i = 0; i < dt.Rows.Count; i++)
  14. {
  15. Id = Convert.ToInt32(dt.Rows[i]["Id"]);
  16. newstitle = dt.Rows[i]["NewsTitle"].ToString();
  17. newscontent = dt.Rows[i]["NewsContent"].ToString();
  18. createtime = Convert.ToDateTime(dt.Rows[i]["CreateTime"]);
  19.  
  20. Model.T_News1 news = new Model.T_News1()
  21. {
  22. Id = Id,NewsTitle = newstitle,NewsContent = newscontent,CreateTime = createtime
  23. };
  24. list.Add(news);
  25. }
  26. return list;
  27. }
  28. [WebMethod]
  29. public int GetLastPageindex(int pagesize)
  30. {
  31. BLL.T_News1 bnews = new BLL.T_News1();
  32. int totalcount= bnews.GetRecordCount("");
  33. if (totalcount % pagesize == 0)
  34. {
  35. return totalcount / pagesize;
  36. }
  37. else
  38. {
  39. return totalcount / pagesize + 1;
  40. }
  41. }


=======================================================================================

GridView 实现分页——【有刷新】

WebForm1.aspx

前台

  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head runat="server">
  3. <title></title>
  4. <script src="js/Jquery1.7.js" type="text/javascript"></script>
  5. <script type="text/javascript">
  6. $(function () {
  7. $('#txtPageindex').focus(function () {
  8. $(this).val("");
  9. })
  10. })
  11. </script>
  12. </head>
  13. <body>
  14. <form id="form1" runat="server">
  15. <div>
  16. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="230px"
  17. Width="859px">
  18. <Columns>
  19. <asp:BoundField DataField="Id" HeaderText="编号" />
  20. <asp:BoundField DataField="NewsTitle" HeaderText="标题" />
  21. <asp:BoundField DataField="NewsContent" HeaderText="内容" />
  22. <asp:BoundField DataField="CreateTime" DataFormatString="{0:yyyy-MM-dd hh:mm:ss}"
  23. HeaderText="创建时间" />
  24. </Columns>
  25. </asp:GridView>
  26. </div>
  27. <div>
  28. <asp:LinkButton ID="btnFirst" runat="server" onclick="btnFirst_Click">第一页</asp:LinkButton>
  29. <asp:LinkButton
  30. ID="btnPre" runat="server" onclick="btnPre_Click">上一页</asp:LinkButton>
  31. <asp:LinkButton ID="btnNext"
  32. runat="server" onclick="btnNext_Click">下一页</asp:LinkButton>
  33. <asp:LinkButton ID="btnLast" runat="server" onclick="btnLast_Click">最后一页</asp:LinkButton><asp:TextBox
  34. ID="txtPageindex" runat="server"></asp:TextBox>
  35. <asp:LinkButton ID="LinkButton5" runat="server" onclick="LinkButton5_Click">Go</asp:LinkButton>
  36. </div>
  37. </form>
  38. </body>
  39. </html>

后台
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8.  
  9. namespace WebApplication1
  10. {
  11. public partial class WebForm1 : System.Web.UI.Page
  12. {
  13. int pagesize = 10;
  14. int pageindex = 1;
  15. protected void Page_Load(object sender,EventArgs e)
  16. {
  17. if (!IsPostBack)
  18. {
  19. ViewState["pageindex"] = 1;
  20. GetLastPageindex();
  21. LoadData();
  22. }
  23. }
  24. private void GetLastPageindex()
  25. {
  26. BLL.T_News1 bnews = new BLL.T_News1();
  27. int totalcount= bnews.GetRecordCount("");
  28. if (totalcount % pagesize == 0)
  29. {
  30. ViewState["lastpageindex"] = totalcount / pagesize;
  31. }
  32. else
  33. {
  34. ViewState["lastpageindex"] = totalcount / pagesize+1;
  35. }
  36. }
  37. private void LoadData()
  38. {
  39. BLL.T_News1 bnews = new BLL.T_News1();
  40. DataTable dt = bnews.GetListDataTable(pagesize,Convert.ToInt32(ViewState["pageindex"]));
  41. this.GridView1.DataSource = dt;
  42. this.GridView1.DataBind();
  43. }
  44.  
  45. protected void btnFirst_Click(object sender,EventArgs e)
  46. {
  47. ViewState["pageindex"] = 1;
  48. LoadData();
  49. }
  50.  
  51. protected void btnPre_Click(object sender,EventArgs e)
  52. {
  53. int pageindex = Convert.ToInt32(ViewState["pageindex"]);
  54. if (pageindex>1)
  55. {
  56. pageindex--;
  57. ViewState["pageindex"] = pageindex;
  58. LoadData();
  59. }
  60. }
  61.  
  62. protected void btnNext_Click(object sender,EventArgs e)
  63. {
  64. int pageindex = Convert.ToInt32(ViewState["pageindex"]);
  65. if (pageindex<Convert.ToInt32(ViewState["lastpageindex"]))
  66. {
  67. pageindex++;
  68. ViewState["pageindex"] = pageindex;
  69. LoadData();
  70. }
  71. }
  72.  
  73. protected void btnLast_Click(object sender,EventArgs e)
  74. {
  75. ViewState["pageindex"] = ViewState["lastpageindex"];
  76. LoadData();
  77. }
  78.  
  79. protected void LinkButton5_Click(object sender,EventArgs e)
  80. {
  81. int result;
  82. if (int.TryParse(txtPageindex.Text,out result) == true)
  83. {
  84. ViewState["pageindex"] = txtPageindex.Text.Trim();
  85. LoadData();
  86. }
  87. else
  88. {
  89. txtPageindex.Text = "请输入合法的数字";
  90. }
  91. }
  92. }
  93. }

猜你在找的Ajax相关文章