分頁
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()
}