我知道这是一个老问题,但这就是我处理这个问题的方式。将 ObjectDataSource 添加到我的 aspx 页面:
<asp:ObjectDataSource ID="myDataSource" runat="server"
SelectMethod="GetSearchResults" EnablePaging="true"
StartRowIndexParameterName="startIndex"
MaximumRowsParameterName="pageSize"
SortParameterName="sortBy" SelectCountMethod="GetSearchCount" >
</asp:ObjectDataSource>
请注意,它缺少一个类型?我在代码中设置它,在我的 Page_Load
myDataSource.TypeName = this.GetType().AssemblyQualifiedName;
然后我在同一页面上(在同一类中)使用静态方法来更新gridview:
public static int GetSearchCount()
{
return _RowCount;//set elsewhere in code - this is the total number of rows for the query
}
public static DataTable GetSearchResults(string sortBy, int pageSize, int startIndex)
{
//Code to dynamically generate SQL statements based on supplied parameters
//then return a datatable containing only the data to be shown in the gridview
}