gate_s

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

当GridView中显示的记录很多的时候,可以通过GridView的分页功能来分页显示这些记录。如果GridView是直接绑定数据库,则很简单:只要点击GridView控件左上角的小三角形,再弹出的选项中,将"启动分页"打上勾即可。
如果是用代码实现,则需要这么做:
1、允许分页:设置AllowPaging=True;
2、设置GridView属性栏中PagerSetting里的一些属性中,定义分页的样式;
3、数据部署:将数据显示到GridView上;
4、加入相关事件:PageIndexChanged()、PageIndexChanging();
5、如果要添加分页码显示,即显示当前在第几页,还需添加DataBound()事件。

例子:
功能:GridView分页使用图片按钮并添加分页码显示。
      默认情况下GridView的分页按钮如果以图片来显示就无法显示文字,这样就无法知道当前所在的页数。于是,添加分页代码显示就可以显示所在分页的索引数字了。

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GridView_Page : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //设置分页的图片按钮,这些都可以在控件的属性表上的pagersetting里设置
        if (!IsPostBack)
        {
            GridView1.Caption = "这是一个GridView的小实验";  
                  //Caption属性类似于表名,显示在控件的正上方。
            GridView1.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast;
            GridView1.PagerSettings.NextPageImageUrl = "img/next.gif";
            GridView1.PagerSettings.PreviousPageImageUrl = "img/pre.gif";
            GridView1.PagerSettings.FirstPageImageUrl = "img/first.gif";
            GridView1.PagerSettings.LastPageImageUrl = "img/last.gif";
            GridView1.PageSize = 10;  //每页最多显示10条记录;
            BindData();
        }
    }
    private void BindData()
    {
        //将数据部署到GridView中
        string Constr = "server=localhost; uid=sa;pwd=123456;database=NorthWind";
        string sqlstr = "select * from products";
        SqlConnection con = new SqlConnection(Constr);
        SqlDataAdapter ad = new SqlDataAdapter(sqlstr, con);
        DataSet ds = new DataSet();
        ad.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        //进行分页之后,重新部署数据
        BindData();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //分页完成之前
        GridView1.PageIndex = e.NewPageIndex;
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        //添加分页码显示
        GridViewRow bottomPagerRow = GridView1.BottomPagerRow;
        Label bottomLabel = new Label();
        bottomLabel.Text = "目前所在分页:(" + (GridView1.PageIndex + 1) + "/" + GridView1.PageCount + ")";
        bottomPagerRow.Cells[0].Controls.Add(bottomLabel);
    }
}

启动分页:
 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
            AllowSorting="True" AutoGenerateColumns="False" PageSize="8">
            <PagerSettings Visible="False" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#EFF3FB" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="学号" />
                <asp:BoundField DataField="name" HeaderText="姓名" />
                <asp:BoundField DataField="sex" HeaderText="性别" />
                <asp:BoundField DataField="age" HeaderText="年龄" />
                <asp:BoundField DataField="department" HeaderText="专业" />
                <asp:BoundField DataField="grade" HeaderText="年级" />
            </Columns>
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:LinkButton ID="lnkbtnFirst" runat="server" onclick="PagerButtonClick">首页</asp:LinkButton>
        <asp:LinkButton ID="lnkbtnPre" runat="server" onclick="PagerButtonClick">上一页</asp:LinkButton>
        <asp:LinkButton ID="lnkbtnNext" runat="server" onclick="PagerButtonClick">下一页</asp:LinkButton>
        <asp:LinkButton ID="lnkbtnLast" runat="server" onclick="PagerButtonClick">尾页</asp:LinkButton>
        <asp:Label ID="lblCurrentIndex" runat="server" Text="第?页"></asp:Label>
        <asp:Label ID="lblPageCount" runat="server" Text="共?页"></asp:Label>
        <asp:Label ID="lblRecordCount" runat="server" Text="记录数"></asp:Label>
        <asp:TextBox ID="txtJumpPage" runat="server" Width="24px">1</asp:TextBox>
        <asp:LinkButton ID="lnkbtnJumpPage" runat="server" 
            onclick="lnkbtnJumpPage_Click">跳转</asp:LinkButton>
posted on 2013-06-07 17:17  gate_s  阅读(470)  评论(0编辑  收藏  举报