Custom paging with custom datasource, CurrentPageIndex is always 0

3 posts, 0 answers
  1. Wayne
    Wayne avatar
    5 posts
    Member since:
    May 2016

    Posted 06 Jul Link to this post

    I have some code I'm troubleshooting from our offshore team.  We have a datagrid that we want to enable paging on, the problem is that the CurrentPageIndex of the RadGrid is always 0.  We aren't using a DataSource property but retrieving our data via a service layer in the code-behind that calls a stored proc that uses OFFSET and FETCH to return the subset of rows based on the page index and page size.

    Here's the ASPX code for the RadGrid:

    <telerik:RadGrid ID="EnrollmentPeriodCompanyElectionsGrid" runat="server" AllowSorting="true" AutoGenerateColumns="false" PageSize="20" AllowPaging="True" AllowCustomPaging="true" OnPageIndexChanged="EnrollmentPeriodCompanyElectionsGrid_PageIndexChanged"
        OnPageSizeChanged="EnrollmentPeriodCompanyElectionsGrid_PageSizeChanged" OnNeedDataSource="EnrollmentPeriodCompanyElectionsGrid_NeedDataSource" OnItemDataBound="EnrollmentPeriodCompanyElectionsGrid_ItemDataBound" EnableViewState="true">
     
        <MasterTableView NoMasterRecordsText="Nothing was found for this election period">
            <Columns>
                <telerik:GridBoundColumn DataField="HrpClientID" HeaderText="ID" SortExpression="HrpClientID" />
                <telerik:GridHyperLinkColumn HeaderText="COMPANY" UniqueName="Company" SortExpression="CompanyName" DataTextField="CompanyName" DataNavigateUrlFields="CompanyID,BenefitEnrollmentPeriodID" DataNavigateUrlFormatString="~/Admin/Companies/Company.aspx?companyID={0}&page=Exhibits&periodID={1}"
                    HeaderStyle-Width="300px" />
                <telerik:GridBoundColumn DataField="CompanyType" HeaderText="TYPE" SortExpression="CompanyType" />
                <telerik:GridBoundColumn DataField="HRP" HeaderText="HRP" SortExpression="HRP" />
                <telerik:GridBoundColumn DataField="HRC" HeaderText="HRC" SortExpression="HRC" />
                <telerik:GridBoundColumn DataField="EnrollmentStatus" SortExpression="EnrollmentStatus" HeaderText="STATUS" />
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    Here's the NeedDataSource method:

    protected void EnrollmentPeriodCompanyElectionsGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
            {
                try
                {
                    var enrollmentId = Request.QueryString["PeriodID"] != null ? int.Parse(Request.QueryString["PeriodID"]) : 0;
                    {
                        RadGrid enrollmentPeriodCompanyElectionsGrid = (RadGrid)sender;
     
                        int currentPageIndex = EnrollmentPeriodCompanyElectionsGrid.CurrentPageIndex;
                        var dataSource = GetBenefitEnrollmentCompanies(enrollmentId, currentPageIndex, pageSize);
     
                        enrollmentPeriodCompanyElectionsGrid.DataSource = dataSource.BenefitEnrollmentCompanyDetail;
     
                        // set this only once.
                        if (enrollmentPeriodCompanyElectionsGrid.VirtualItemCount == 0)
                            enrollmentPeriodCompanyElectionsGrid.VirtualItemCount = dataSource.TotalRecordsCount;
                    }
     
                }
                catch (Exception ex)
                {
                    ErrorHandler.HandleError<Exception>(ex, new { UserID = CurrentUser.UserID, CompanyID = CurrentCompany.CompanyID, ViewingUserID = CurrentUser.UserID, PageIndex = 1, PageSize = pageSize });
                }
            }

    and PageIndexChanged:

    protected void EnrollmentPeriodCompanyElectionsGrid_PageIndexChanged(object sender, GridPageChangedEventArgs e)
        {
            try
            {
                // reset this property to new page after.
                EnrollmentPeriodCompanyElectionsGrid.CurrentPageIndex = e.NewPageIndex + 1;
            }
            catch (Exception ex)
            {
                ErrorHandler.HandleError<Exception>(ex, new { UserID = CurrentUser.UserID, CompanyID = CurrentCompany.CompanyID, ViewingUserID = CurrentUser.UserID, PageIndex = e.NewPageIndex, PageSize = pageSize });
            }
        }

    However, no matter what the CurrentPageIndex is 0.  I've verified by manually changing the CurrentPageIndex in Visual Studio that, when set, paging works correctly, it just is always being set to 0.  I've tried everything, but I can't get the paging to recognize that I'm selecting a new page.

  2. Wayne
    Wayne avatar
    5 posts
    Member since:
    May 2016

    Posted 06 Jul in reply to Wayne Link to this post

    I have it partially working by removing the Offset/Fetch part from the stored procedure and just setting AllowPaging=true, and not worrying about anything else.  However, when I do this the "Next" button doesn't work after the second page (clicking it just refreshes Page 2), although clicking the page numbers works, and the "Previous" button always goes back to page 1 regardless of what page I am on.  
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kostadin
    Admin
    Kostadin avatar
    1711 posts

    Posted 11 Jul Link to this post

    Hello Wayne,

    In case you need to use the built-in paging of the grid you need to bind it to the entire set of data and the grid will manually handle the paging. Nevertheless, if you want to retrieve only the items of the current page you need to use a custom paging. I would recommend you to examine the following help article which elaborates more on this matter.
    If you experience some issue with paging I would recommend you to check for any client side exception. Also if you are using an Ajax you can disable it and check whether the paging is working without it.

    Regards,
    Kostadin
    Telerik by Progress
    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top