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.