Now, we can configure the ObjectDataSource control with
these methods.
<asp:ObjectDataSource ID="ObjectDataSource1"
EnablePaging="true"
runat="server"
SelectCountMethod="GetUserCount"
SelectMethod="BindUsers" TypeName="UserDAO"
StartRowIndexParameterName="startRowIndex"
MaximumRowsParameterName="maximumRows">
</asp:ObjectDataSource>
Next, we can implement the stored procedure that fetches
the records only for that page by accepting startRowIndex and maximumRows as
parameters.
CREATE PROC [dbo].[GetUsers]
(@RowIndex INT ,
@MaxRows INT)
AS
DECLARE @StartRow INT
DECLARE @EndRow INT
SET @StartRow = (@RowIndex+1)
SET @EndRow = @StartRow + @MaxRows
SELECT * FROM (
SELECT FirstName, LastName, ROW_NUMBER() OVER (ORDER BY
UserID) AS ROW
FROM CD_Users) As NumberedUsers
WHERE ROW BETWEEN @StartRow AND @EndRow
The start row index parameter that will be returned from
GridView control will be 0. Hence, we need to increment the row index parameter
by 1 before querying the database.
Finally, we will configure the GridView control to
accept the datasource control.
<asp:GridView ID="gvUsers" runat="server"
AllowPaging="True" DataSourceID="ObjectDataSource1" PageSize="2">
</asp:GridView>
Thus, we have implemented the custom paging with the
help of ObjectDataSource control and Sql Server 2005.
Practically, when we implement the custom paging there
are chances that we should restrict the result set based on some filter
condition. In other words, we can restrict the result set by some parameters.
For example,
public DataTable BindUsers(string FirstName, string
LastName, int startRowIndex, int maximumRows)
{
}
public int GetUserCount(string FirstName, string
LastName)
{
}
These extra parameters can be configured from
ObjectDataSource control SelectParameter collection.
<asp:ObjectDataSource ID="ObjectDataSource1"
EnablePaging="true"
runat="server" SelectCountMethod="GetUserCount"
SelectMethod="BindUsers" TypeName="UserDAO"
StartRowIndexParameterName="startRowIndex"
MaximumRowsParameterName="maximumRows">
<SelectParameters>
<asp:ControlParameter ControlID="txtFirstName"
Name="FirstName" Type="string" />
<asp:ControlParameter ControlID="txtLastName"
Name="LastName" Type="string" />
</SelectParameters>
</asp:ObjectDataSource>
Finally, the stored procedure should be changed to
accept the filter parameters.
|