如果使用ObjectDataSource,几乎已经为您处理了所有事情,只需告诉GridViewAllowPaging并给它一个即可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= // 检索数据源的方法 gvColors.PageIndex= e.NewPageIndex; gvColors.DataBind(); }
VB.NET
Protected Sub gvColors_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) { gvColors.DataSource= // 检索数据源的方法 gvColors.PageIndex= e.NewPageIndex gvColors.DataBind() }