New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Persist current PageIndex or stay with the first item after PageSize changed

DESCRIPTION

By default RadGrid will jump to the first page when the PageSize is changing. Sometimes, you might want to persist the current PageIndex or stay on the same page with the first item of the current page after the PageSize is changed in RadGrid. This article will show you the way you can achieve that.

Example showing the persisting of current page index after PageSize changed

Example showing that the user stays on the same page with the first item after PageSize has changed

SOLUTION

DOWNLOAD the sample: radgrid_radfilter_rendering_problem.zip

OR

Copy the code

The following Markup works with both scenarios. Subscribe the Grid to PageIndexChanged and PageSizeChanged events.

XML
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" PageSize="3"
    OnPageIndexChanged="RadGrid1_PageIndexChanged"
    OnPageSizeChanged="RadGrid1_PageSizeChanged">
</telerik:RadGrid>

Persist CurrentPageIndex on PageSizeChange

SessionPageIndex property, PageIndexChanged and PageSizeChanged event handlers

C#
public int SessionPageIndex
{
    get
    {
        if (Session["CurrentPageIndex"] == null)
        {
            Session["CurrentPageIndex"] = RadGrid1.CurrentPageIndex;
        }
 
        return (int)Session["CurrentPageIndex"];
    }
    set
    {
        Session["CurrentPageIndex"] = value;
    }
}
 
protected void RadGrid1_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
    if (RadCheckBox1.Checked == true)
    {
        RadGrid1.CurrentPageIndex = SessionPageIndex;
    }
}
 
protected void RadGrid1_PageIndexChanged(object sender, GridPageChangedEventArgs e)
{
    SessionPageIndex = e.NewPageIndex;
}

Stay with the first item on the same page after PageSizeChange

OldPageIndex and OldPageSize properties +  PageIndexChanged and PageSizeChanged event handlers

C#
public int OldPageIndex
{
    get
    {
        if (Session["OldPageIndex"] == null)
        {
            Session["OldPageIndex"] = RadGrid1.CurrentPageIndex;
        }
 
        return (int)Session["OldPageIndex"];
    }
    set
    {
        Session["OldPageIndex"] = value;
    }
}
 
public int OldPageSize
{
    get
    {
        if (Session["OldPageSize"] == null)
        {
            Session["OldPageSize"] = RadGrid1.PageSize;
        }
 
        return (int)Session["OldPageSize"];
    }
    set
    {
        Session["OldPageSize"] = value;
    }
}
 
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        OldPageIndex = RadGrid1.CurrentPageIndex;
        OldPageSize = RadGrid1.PageSize;
    }
}
 
protected void RadGrid1_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
{
     
    if (OldPageSize != e.NewPageSize)
    {
        // var newPageIndex = (int)Math.Floor((OldPageIndex * OldPageSize + 1) / (decimal)e.NewPageSize);
        int newPageIndex = (OldPageIndex * OldPageSize + 1) / e.NewPageSize;
 
        if(RadCheckBox2.Checked == true)
        {
            RadGrid1.CurrentPageIndex = newPageIndex;
        }
         
        OldPageSize = e.NewPageSize;
        OldPageIndex = newPageIndex;
    }
}
 
protected void RadGrid1_PageIndexChanged(object sender, GridPageChangedEventArgs e)
{
    OldPageIndex = e.NewPageIndex;
}