自定义分页/按钮

  使用GridView绑定了数据之后,设置allowpage=true之后,自动套用样式。下面的翻页按钮只有1 2 3 4 5 6等数字用于翻页,

如果想自己定义按钮,可用以下方法。

aspx文件
  1. "DG" runat="server" AutoGenerateColumns="False">  //这里用的是datagrid 原理是一样的
  2. "USERCODE" HeaderText="登录帐户">
  3. "USERNAME" HeaderText="用户姓名">
  4. "所属部门" DataField="ORGNAME">
  5. "用户角色" DataField="ROLENAME">
  6. "使用状态" DataField="ISUSE">
  7. "修改" Text="修改">
  8. "删除" Text="删除">
  9. "l_CurrentPage" runat="server" oreColor="Red">
  10. "l_PageCount" runat="server">页
  11. "First" runat="server" CommandArgument="first" OnClick="PagerButton_Click">首 页
  12. "Prev" runat="server" CommandArgument="prev" OnClick="PagerButton_Click">上一页
  13. "Next" runat="server" CommandArgument="next" OnClick="PagerButton_Click">下一页                      
  14.  "Last" runat="server" CommandArgument="last" OnClick="PagerButton_Click">尾 页

 

aspx.cs文件

  1. private int PageSize;    //每页显示条目数量
  2. private int PageCount;   //总页数
  3. private int CurrentPage;
  4. private int RecordCount;  //总记录数
  5. protected void Page_Load(object sender, EventArgs e)
  6. {
  7.        User user = new User();
            Hashtable ht = new Hashtable();
            DataTable dt = user.QueryUserView(ht);
            RecordCount = dt.Rows.Count;//计算总记录数
       
            PageSize = 8; //初始化每页条目数量
            DG.AllowPaging = true;
            DG.PagerStyle.Visible = false;
            DG.PageSize = PageSize;
            PageCount = (RecordCount % PageSize) > 0 ? (RecordCount / PageSize) + 1 : (RecordCount / PageSize); //计算总页数
            this.l_PageCount.Text = PageCount.ToString();
            if (!this.IsPostBack)
            {
                pageIndx = 1;
                l_CurrentPage.Text = "1";
            }
            InitData();
  8. }
  1.     /// 
  2.     /// 绑定DG
  3.     /// 
  4.     private void InitData()
  5.     {
  6.         User user = new User();
  7.         Hashtable ht = new Hashtable();
  8.         DataTable dt = user.QueryUserView(ht);
  9.         DG.DataSource = dt;
  10.         DG.DataBind();
  11.         DataBindUser();        
  12.     }
  13.     /// 
  14.     /// 按钮的可见属性
  15.     /// 
  16.     private void DataBindUser()
  17.     {
  18.         pageIndx = DG.CurrentPageIndex+1;
  19.         l_CurrentPage.Text =pageIndx.ToString();
  20.         if (pageIndx == 1)
  21.         {
  22.             this.lb_First.Enabled = false;
  23.             this.lb_Prv.Enabled = false;
  24.             this.lb_Next.Enabled = true;
  25.             this.lb_Last.Enabled = true;
  26.         }
  27.         else if (pageIndx == PageCount)
  28.         {
  29.             this.lb_Next.Enabled = false;
  30.             this.lb_Last.Enabled = false;
  31.             this.lb_First.Enabled = true;
  32.             this.lb_Prv.Enabled = true;
  33.         }
  34.         else 
  35.         {
  36.             this.lb_Next.Enabled = true;
  37.             this.lb_Last.Enabled = true;
  38.             this.lb_First.Enabled = true;
  39.             this.lb_Prv.Enabled = true;
  40.         }
  41.     }
  42.     /// 
  43.     /// 翻页按钮的功能
  44.     /// 
  45.     /// 
  46.     /// 
  47.     protected void PagerButton_Click(object sender, EventArgs e)
  48.     {
  49.      int pageIndx = Convert.ToInt32(DG.CurrentPageIndex);
  50.      string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();
  51.         switch (arg)
  52.         {
  53.             case "prev":
  54.             if (pageIndx > 0)
  55.             {
  56.               pageIndx -= 1;
  57.             }
  58.             break;
  59.             case "next":
  60.             if (pageIndx < PageCount - 1)
  61.             {
  62.                pageIndx += 1;
  63.             }
  64.             break;
  65.             case "last":
  66.             pageIndx = PageCount - 1;
  67.             break;
  68.             default:
  69.             pageIndx = 0;
  70.             break;
  71.         }
  72.         DG.CurrentPageIndex = pageIndx;
  73.         InitData();
  74.     }

你可能感兴趣的:(点滴细节)