ASP.NET中GridView分页

方法一:

新建一个页面然后在页面拖一个GridView控件在属性框中设置GridView控件的AllowPaing属性为True 然后再设置GridView的PageSize属性,源文件如下:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames ="classId" CellPadding="5" OnRowDeleting="GridView1_RowDeleting" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize ="15">
            <Columns>
                <asp:BoundField DataField ="className" HeaderText ="名称" />
                <asp:BoundField DataField="classDes" HeaderText="描述" SortExpression="classDes" />
         </Columns>
          <PagerTemplate>
               <table style="font-size :12px;">
               <tr>
           
                 <td> 总共<asp:Label ID="Label1" runat="server" Text="<%#((GridView)Container.NamingContainer).PageCount %>"></asp:Label>页 &nbsp; </td>
               
                    <td> 第<asp:Label ID="Label2" runat="server" Text="<%#((GridView)Container.NamingContainer).PageIndex+1 %>"></asp:Label>页 &nbsp;</td>
                
                          <td> <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument ="1" CommandName ="Page" Enabled ="<%#((GridView)Container.NamingContainer).PageIndex!=0 %>">首页</asp:LinkButton> &nbsp;</td>
                          <td>
                              <asp:LinkButton ID="LinkButton2" runat="server" CommandArgument ="Prev" CommandName ="Page" Enabled ="<%#((GridView)Container.NamingContainer).PageIndex!=0 %>">上一页</asp:LinkButton> &nbsp;</td>
                              <td>
                                  <asp:LinkButton ID="LinkButton3" runat="server" CommandArgument ="Next" CommandName ="Page" Enabled ="<%#((GridView)Container.NamingContainer).PageIndex!=((GridView)Container.NamingContainer).PageCount-1 %>">下一页 </asp:LinkButton> &nbsp;</td>
                            <td>
                                <asp:LinkButton ID="LinkButton4" runat="server" CommandArgument ="Last" CommandName ="Page" Enabled ="<%#((GridView)Container.NamingContainer).PageIndex!=((GridView)Container.NamingContainer).PageCount-1 %>">尾页</asp:LinkButton> &nbsp;</td>
                                <td>
                                    <asp:LinkButton ID="LinkButton5" runat="server" CommandArgument ="-1" CommandName ="Page" ValidationGroup="1" >GO</asp:LinkButton> </td>
                                    <td>
                   <asp:TextBox ID="txtNum" runat="server" Width="30px" Text ="<%#((GridView)Container.NamingContainer).PageIndex+1 %>" ValidationGroup="1"></asp:TextBox>
                                        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="" ValidationExpression ="^/d+$" ControlToValidate ="txtNum" ValidationGroup="1"></asp:RegularExpressionValidator>
                   </td>
               </tr>
               </table>
            </PagerTemplate>
           
        </asp:GridView>  

cs代码为:
/// <summary>
   ///为GridView控件绑定数据源  
    /// </summary>
    private void bindClass()
   {
        string sql = "SELECT * FROM Class ORDER BY classId DESC";
        DataExecute data = new DataExecute();
        DataSet ds = data.returnDataSet(sql, CommandType.Text);
        if (ds.Tables[0].Rows.Count != 0)
        {
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
            //this.Count = ds.Tables[0].Rows.Count;           
        }
    }

在GridView控件的PageIndexChanging事件中写相应的代码即可代码如下:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        GridView gv = (GridView)sender;
        int newPageIndex = 0;
        if (-2 == e.NewPageIndex)
        {
            TextBox tb = null;
            GridViewRow gvr = gv.BottomPagerRow;
            if (gvr != null)
            {
                tb = (TextBox)gvr.FindControl("txtNum");

            }
            if (tb != null)
            {
                newPageIndex = int.Parse(tb.Text) - 1;
            }
        }
        else
        {
            newPageIndex = e.NewPageIndex;
        }
        newPageIndex = newPageIndex <= 0 ? 0 : newPageIndex;   
        this.GridView1.PageIndex = newPageIndex;
         //分页后重新绑定数据源
        this.bindClass();       
       
    }

方法二:

protected void Page_Load(object sender, EventArgs e)
     {
         btnFirst.Text = "最首页";
         btnPrev.Text = "前一页";
         btnNext.Text = "下一页";
         btnLast.Text = "最后页";
         GridView1.DataBind();
         ShowPageChangedStatus();
     }
   private void ShowPageChangedStatus()
     {
         lblCurrentIndex.Text = "第" + (GridView1.PageIndex + 1).ToString() + "页";
         lblPageCount.Text = "总共 " + GridView1.PageCount.ToString() + " 页";
     }
     public void PagerButtonClick(object sender, EventArgs e)
     {
         string arg = ((LinkButton)sender).CommandArgument.ToString();
         switch (arg)
         {
             case "next":
                 if (GridView1.PageIndex < (GridView1.PageCount - 1))
                 {
                     GridView1.PageIndex += 1;
                 }
                 break;
             case "prev":
                 if (GridView1.PageIndex > 0)
                 {
                     GridView1.PageIndex -= 1;
                 }
                 break;
             case "last":
                 GridView1.PageIndex = (GridView1.PageCount - 1);
                 break;
             default:
                 GridView1.PageIndex = System.Convert.ToInt32(arg);
                 break;
         }
         GridView1.DataBind();
         ShowPageChangedStatus();
     }
     protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
     {
         int startIndex;
         startIndex = GridView1.PageIndex * GridView1.PageSize;
         GridView1.PageIndex = e.NewPageIndex;
         GridView1.DataBind();
         ShowPageChangedStatus();
     }

   <asp:label id="lblPageCount" runat="server"></asp:label>&nbsp;
   <asp:label id="lblCurrentIndex" runat="server"></asp:label>
   <asp:linkbutton id="btnFirst" οnclick="PagerButtonClick" runat="server"
   Font-size="8pt" ForeColor="navy" CommandArgument="0"></asp:linkbutton>&nbsp;
   <asp:linkbutton id="btnPrev" οnclick="PagerButtonClick" runat="server"
   Font-size="8pt" ForeColor="navy" CommandArgument="prev"></asp:linkbutton>&nbsp;
   <asp:linkbutton id="btnNext" οnclick="PagerButtonClick" runat="server"
   Font-size="8pt" ForeColor="navy" CommandArgument="next"></asp:linkbutton>&nbsp;
   <asp:linkbutton id="btnLast" οnclick="PagerButtonClick" runat="server"
   Font-size="8pt" ForeColor="navy" CommandArgument="last"></asp:linkbutton>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值