当前位置:编程学习 > asp >>

一步步打造自己的分页控件--2

 

上一篇中我将分页的代码写在用户控件里面,仔细一想,发现分页无非是改变当前页码,获取不同的数据

所以点击分页事件都是同一个,因此想到了使用委托,绑定多个类似的事件,对上面的代码进行优化

前台代码

 

 

1 <div id="pager">

2        第<asp:Label ID="curPage" runat="server" Text="1"></asp:Label>页  

3         <asp:LinkButton ID="lbtfirst" runat="server" onclick="lbtfirst_Click">首页</asp:LinkButton>     

4         <asp:LinkButton ID="lbtPre" runat="server" onclick="lbtPre_Click">上页</asp:LinkButton>    

5         <asp:LinkButton

6             ID="lbtNext" runat="server" onclick="lbtNext_Click">下页</asp:LinkButton>    

7             <asp:LinkButton ID="lblLast"

8                 runat="server" onclick="lblLast_Click">末页</asp:LinkButton>    

9     </div> 

后台分页代码没有具体实现分页,没有设置数据源,数据绑定控件参数

 

 

 1 using System;

 2

 3 namespace Pager.controls

 4 {

 5     public delegate void pageChangedHandler(object sender, int nPageIndex);

 6     public partial class pager3 : System.Web.UI.UserControl

 7     {

 8         public event pageChangedHandler OnPageChange;

 9         protected void Page_Load(object sender, EventArgs e)

10         {

11             if (!IsPostBack)

12             {

13                 state();

14             }

15         }

16         #region 属性

17

18         int pageCount = 10;

19         /// <summary>

20         /// 每页数量

21         /// </summary>

22         public int PageCount

23         {

24             get { return pageCount; }

25             set { pageCount = value; }

26         }

27         int pagelist = 1;

28         /// <summary>

29         /// 页数

30         /// </summary>

31         public int Pagelist

32         {

33             get { return pagelist; }

34             set { pagelist = value; }

35         }    

36

37         private void state()

38         {

39             if ( this.curPage.Text=="1")

40             {

41                 lbtfirst.Enabled = false;

42                 lbtPre.Enabled = false;

43                 lbtNext.Enabled = true;

44                 lblLast.Enabled = true;

45             }

46             if (int.Parse(this.curPage.Text) > 1 && int.Parse(this.curPage.Text) < Pagelist)

47             {

48                 lbtfirst.Enabled = true;

49                 lbtPre.Enabled = true;

50                 lbtNext.Enabled = true;

51                 lblLast.Enabled = true;

52             }

53             if (int.Parse(this.curPage.Text) == Pagelist)

54             {

55                 lbtfirst.Enabled = true;

56                 lbtPre.Enabled = true;

57                 lbtNext.Enabled = false;

58                 lblLast.Enabled = false;

59             }

60         }

61

62         #endregion    

63

64         #region 点击分页事件

65         //第一页

66         protected void lbtfirst_Click(object sender, EventArgs e)

67         {

68             this.curPage.Text = "1";

69             OnPageChange(sender, 1);

70             state();

71       

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,