分页
ObjectDataSource 控件
如果使用 ObjectDataSource,几乎所有内容都已经为你处理,只需简单地告诉 GridView AllowPaging
并给它一个 PageSize
。
<asp:GridView ID="gvColors"
runat="server"
DataSourceID="sdsColors"
AllowPaging="True"
PageSize="5">
</asp:GridView>
<asp:SqlDataSource ID="sdsColors"
runat="server"
ConnectionString="<%$ MyConnectionString %>"
SelectCommand="SELECT Color_ID, Color_Name FROM Colors">
</asp:SqlDataSource>
手动绑定
如果手动绑定,则必须处理 PageIndexChanging
事件。只需设置 DataSource
和 PageIndex
并重新绑定 GridView。
<asp:GridView ID="gvColors"
runat="server"
AllowPaging="True"
PageSize="5"
OnPageIndexChanging="gvColors_PageIndexChanging">
</asp:GridView>
C#
protected void gvColors_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvColors.DataSource = // Method to retrieve DataSource
gvColors.PageIndex = e.NewPageIndex;
gvColors.DataBind();
}
VB.NET
Protected Sub gvColors_PageIndexChanging(sender As Object, e As GridViewPageEventArgs)
{
gvColors.DataSource = // Method to retrieve DataSource
gvColors.PageIndex = e.NewPageIndex
gvColors.DataBind()
}