使用GridView自带分页的代码

时间:2023-03-08 16:42:31

  1. 关于GridView分页页码的讨论

  2. 在GridView中实现分页的效果方法非常easy,仅仅须要在“GridView任务”对话框中进行设置就能够了。在“GridView任务”对话框中,选择“启用分页”命令,这样建立起简单的分页效果。
  3. 在使用“启用分页”命令的时候要注意两点。
  4. (1)  是否同意分页
  5. GridView的AllowPaging属性。AllowPaging:是否同意分页。假设AllowPaging=“true”就是同意分页。否则就是不同意使用分页。
  6. (2)       每页记录数
  7. GridView的PageSize属性。在GridView控件的属性中能够设置每页显示的数据记录的个数。默认情况下PageSize的值是10,也能够依据须要进行设置。
  8. 假设想要对分页编码进行设置的话,能够在HTML代码中为GridView控件加入分页导航条形式代码。也就是启用GridView的PagerSettings属性,在PagerSettings属性中能够设置依据须要设置Mode的值,来实现分页编码的显示效果。
  9. <PagerSettings
  10. Mode = "NextPreviousFirstLast"
  11. FirstPageText = "第一页"
  12. LastPageText = "末页">
  13. </PagerSettings>
  14. 注意:
  15. PagerSettings属性的Mode:Numeric,NextPrevious,NextPreviousFirstLast,NumericFirstLast。有这四种,能够依据不同须要进行不同的选择设置。
  16. 自己主动设置分页效果

  17. 如今想要在GridView控件上显演示样例如以下页码信息:总页数、当前页、首页、上一页、下一页、尾页。
  18. 创建总页数
  19. <asp:Label ID="Lab_PageCount" runat="server"  Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>
  20. 创建但前页
  21. <asp:Label  ID="Lab_CurrentPage" runat="server"  Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
  22. 创建首页
  23. <asp:LinkButton ID="LBtn_FirstPage" runat="server" CommandArgument="First" CommandName="Page"
  24. Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首页</asp:LinkButton>
  25. 创建上一页
  26. <asp:LinkButton ID="LBtn_PreviousPage" runat="server" CommandArgument="Prev" CommandName="Page"
  27. Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton>
  28. 创建下一页
  29. <asp:LinkButton ID="LBtn_NextPage" runat="server" CommandArgument="Next" CommandName="Page"
  30. Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页</asp:LinkButton>
  31. 创建尾页
  32. <asp:LinkButton ID="LBtn_LastPage" runat="server" CommandArgument="Last" CommandName="Page"
  33. Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页</asp:LinkButton>
  34. 相应后台代码
  35. public void GetDataSet()

  36. {

  37. string zhuangtmc;

  38. zhuangtmc = Convert.ToString(DropDownList1.Text);

  39. string sql;

  40. sql = "Select L.LunWBH,L.LunWBT,Z.ZhuangTMC,L.ZhuCYHM,B.BianJM,L.TouGRQ From T_LunWXX L ,T_LunWZhT Z,T_BianJPL B  ";

  41. sql += "Where L.ZhuangTBH=Z.ZhuangTBH AND B.LunWBH=L.LunWBH AND";

  42. sql += " B.BianJM='" + zhucyhm + "'";

  43. if (zhuangtmc != "")

  44. {

  45. sql += " And Z.ZhuangTMC='" + zhuangtmc + "'";

  46. }

  47. sql += " Order By TouGRQ";

  48. CommonDB = new Common();

  49. GridView1.DataSource = CommonDB.DataSource(sql);

  50. GridView1.DataBind();
  51. //用lblCurrentIndex来显示当前页的页数。

  52. LabelCurrentPage.Text = "第 " + (GridView1.PageIndex + 1).ToString() + " 页";

  53. //用LblPageCount来显示当前数据的总页数。

  54. LabelPageCount.Text = "共 " + GridView1.PageCount.ToString() + " 页";

  55. //用LblrecordCount来显示数据的总条数。

  56. LabelRecordCount.Text = "总共 " + CommonDB.DataSets(sql).Tables[0].Rows.Count.ToString() + " 条";
  57. // 计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页"

  58. //点击首页设定的值为1。

  59. LinkButtonFirstPage.CommandName = "1";

  60. //点击‘上一页’

  61. LinkButtonPreviousPage.CommandName = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());

  62. //点击‘下一页’

  63. LinkButtonNextPage.CommandName = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());

  64. //点击‘尾页’

  65. LinkButtonLastPage.CommandName = GridView1.PageCount.ToString();

  66. }