ASP.NET控件Gridview自带的分页功能实现

ps:假分页,只适合数据量较小时使用

一.设置GridView控件属性

1、更改GrdView控件的AllowPaging属性为true。
2、更改GrdView控件的PageSize属性为 任意数值(可以不用更改,这个表示每页显示的记录条数,默认为10条)
3、更改GrdView控件的PageSetting->Mode为Numeric等(默认为Numeric)该属性为分页样式。

设置好后前台样式基本就完成了

二.前台代码

<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#3366CC"
                        BorderStyle="None" BorderWidth="1px" CellPadding="4" AllowPaging="True" 
                        onpageindexchanging="GridView1_PageIndexChanging" PageSize="10">
                        <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                        <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
                        <PagerSettings FirstPageText="首頁" LastPageText="尾頁" 
                            Mode="NextPreviousFirstLast" NextPageText="下一頁" PreviousPageText="上一頁" />
                        <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                        <RowStyle BackColor="White" ForeColor="#003399" />
                        <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                        <SortedAscendingCellStyle BackColor="#EDF6F6" />
                        <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                        <SortedDescendingCellStyle BackColor="#D6DFDF" />
                        <SortedDescendingHeaderStyle BackColor="#002876" />
                    </asp:GridView>

三.后台代码

添加事件GridView1_PageIndexChanging,并在事件中写下如下代码:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataSource = (DataTable)Session["dsData"];
            GridView1.DataBind();
        }

四.效果图如下

猜你喜欢

转载自blog.csdn.net/qq_42450386/article/details/100661044