RadDataPager TotalRowCountRequest is firing PageIndexChanged ... how to cancel

4 posts, 0 answers
  1. Daniel
    Daniel avatar
    4 posts
    Member since:
    Aug 2011

    Posted 31 Jan 2012 Link to this post

    Now, I know by design RadDataPager is implimented to work the way it is for my scenario (described below), but I am wondering how to work around it.

    I am using ASP.NET AJAX... I saw a suggestion for Silverlihgt RadDataPager to use some sort of RadDataPager.PagedSourse.PageIndexChanged or something maybe.

    My Scenario:
    • I am binding a RadDataPager using the TotalRowCountRequest event.
    • I start by binding the RadDataPager (PageSize=5) to have a TotalRowCount of 10 (item count of a collection I'm keeping in ViewState).
    • User clicks to view Page #2 (which means PageIndex 1)   <-------------- so far so good
    • User deletes one of the items in my ViewState item collection.
      • Now the TotalRowCountRequest gets the updated count (which is now 9 because the User removed 1 from the original 10)
        • PageIndexChanged is now fired with a NewPageIndex of 0        <------------------ this is what I want to prevent

    I understand that this is the way it's suppose to work, as it's really meant for a User to do something like going to last page, and then filtering items (since the item collection would chage Telerik assumes the RadDataPager should take the User to the first Page).

    So is there a good way to do what I want and prevent TotalRowRequest from firing PageIndexChanged with a PageIndex of zero?

    Thanks for your time to read and reply!
    -Dan
  2. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 01 Feb 2012 Link to this post

    Hi Daniel,

    Indeed this is the default behavior in RadDataPager. It is expected that, whenver your total row count changes, the pager should reset its index and go to the first index. Otherwise, the pager may end up in an non-existent page index.

    This behavior can be prevent, though. Suppose we have a button that does some action in the code-behind Click handler and, as a result, the total row count is reduced. What you can do is save the current page index of RadDataPager in a private field on the page. Then, in the PreRenderComplete event handler in your page, you can check if there is a saved page index and fire a pager command  that will restore the current index. Here is a sample:

    int currentPageIndex = -1;
    protected void Button1_Click(object sender, EventArgs e)
    {
        //RowCount is the total item count
        RowCount--;
        //we save the current data pager index to a private field
        currentPageIndex = RadDataPager1.CurrentPageIndex;
    }
     
    protected void Page_PreRenderComplete(object sender, EventArgs e)
    {
        //fire a page command if there is a saved page index
        //Note: you can do this only in Page_PreRenderComplete
        if (currentPageIndex > -1)
        {
            RadDataPager1.FireCommand(RadDataPager.PageCommandName, (currentPageIndex + 1).ToString());
        }
    }

    Using this approach we can keep RadDataPager from resetting the page index. Note, however, that, by keeping the page index unchanged, you have the responsibility to ensure the pager does not go to a non-existing page index.

    Veli
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Daniel
    Daniel avatar
    4 posts
    Member since:
    Aug 2011

    Posted 01 Feb 2012 Link to this post

    Veli,

    While I do understand your approach for keeping the PageIndex the same, I am actually looking for a way to prevent PageIndexChanged from firing, because it is making a request to the DB for more information.

    Here is code for a simple example, although I am unsure how to integrate RadDataPager correctly for my case:

    Is there a way for me to use the RadDataPager for a case involving deleting items in an item collection (while not tieing it to a ListView or GridView)?

    Thanks!
    -Dan
    ______________________________________________________________________

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TelerikRadDataPager.aspx.cs" Inherits="Test.TelerikRadDataPager" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

        <title></title>

        <telerik:RadCodeBlock runat="server">

        <script type="text/javascript">

          

        </script>

        </telerik:RadCodeBlock>

    </head>

    <body>

        <form id="form1" runat="server">

        <telerik:RadScriptManager runat="server"></telerik:RadScriptManager>

        <div>

            <asp:Label ID="lblDisplay" runat="server" EnableViewState="false"></asp:Label>

            <telerik:RadDataPager ID="RadDataPager1" runat="server"

                OnPageIndexChanged="RadDataPager1_PageIndexChanged"

                OnTotalRowCountRequest="RadDataPager1_TotalRowCountRequest"

                PageSize="5">

            <Fields>

                <telerik:RadDataPagerButtonField FieldType="FirstPrev" />

                <telerik:RadDataPagerButtonField FieldType="Numeric" />

                <telerik:RadDataPagerButtonField FieldType="NextLast" />

            </Fields>

            </telerik:RadDataPager>

            <asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" Text="delete 1" />

        </div>

        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">

            <AjaxSettings>

                <telerik:AjaxSetting AjaxControlID="btnDelete">

                    <UpdatedControls>

                        <telerik:AjaxUpdatedControl ControlID="RadDataPager1" />

                        <telerik:AjaxUpdatedControl ControlID="lblDisplay" />

                    </UpdatedControls>

                </telerik:AjaxSetting>

                <telerik:AjaxSetting AjaxControlID="RadDataPager1">

                    <UpdatedControls>

                        <telerik:AjaxUpdatedControl ControlID="RadDataPager1" />

                        <telerik:AjaxUpdatedControl ControlID="lblDisplay" />

                    </UpdatedControls>

                </telerik:AjaxSetting>

            </AjaxSettings>

        </telerik:RadAjaxManager>

        </form>

    </body>

    </html>

     

    _______________________________________________________________

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using Telerik.Web.UI;

    namespace Test

    {

        public partial class TelerikRadDataPager : System.Web.UI.Page

        {

            List<string> _items = null;

            protected void Page_Load(object sender, EventArgs e)

            {

                this.lblDisplay.Text += "Page_Load<br />";

                if (!IsPostBack)

                {

                    _items = new List<string>(){

                        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"

                    };

                   

                    this.FillDataForPage(0);

                }

                else

                {

                    _items = ViewState["_items"] as List<string>;

                }

            }

            protected void Page_PreRender(object sender, EventArgs e)

            {

                this.lblDisplay.Text += "Page_PreRender<br />";

                ViewState["_items"] = _items;

            }

            protected void RadDataPager1_TotalRowCountRequest(object sender, RadDataPagerTotalRowCountRequestEventArgs e)

            {

                this.lblDisplay.Text += "RadDataPager1_TotalRowCountRequest(_items==null ? " + ((_items == null) ? "true" : "false") + ", so e.TotalRowCount=" + ((_items == null) ? (ViewState["_items"] as List<string>).Count.ToString() : _items.Count.ToString()) + ")<br />";

                if (_items != null)

                {

                    e.TotalRowCount = _items.Count;

                }

                else

                {

                    e.TotalRowCount = (ViewState["_items"] as List<string>).Count;

                }

            }

            protected void RadDataPager1_PageIndexChanged(object sender, RadDataPagerPageIndexChangeEventArgs e)

            {

                if (_items != null)

                {

                    this.lblDisplay.Text += "RadDataPager1_PageIndexChanged(NewPageIndex=" + e.NewPageIndex.ToString() + ")<br />";

                    this.FillDataForPage(e.NewPageIndex);

                }

            }

            private void FillDataForPage(int pageIndex)

            {

                this.lblDisplay.Text += "FillDataForPage(pageIndex=" + pageIndex.ToString() + ")<br />";

               

                //Fill some control with data

                for (int i=0; i < _items.Count; i++)

                {

                    if (pageIndex * this.RadDataPager1.PageSize <= i

                        && i < (pageIndex + 1) * this.RadDataPager1.PageSize)

                    this.lblDisplay.Text += _items[i];

                }

                this.lblDisplay.Text += "<br />";

            }

            protected void btnDelete_Click(object sender, EventArgs e)

            {

                this.lblDisplay.Text += "btnDelete_Click<br />";

                if (_items.Count > 0)

                {

                    _items.RemoveAt(0);

                    int iActualPageCount = (_items.Count / this.RadDataPager1.PageSize);

                    if(_items.Count % this.RadDataPager1.PageSize > 0)

                    {

                        iActualPageCount++;

                    }

                    if (this.RadDataPager1.CurrentPageIndex + 1 <= iActualPageCount)//number of displaying pages has NOT changed

                    {//fill data for the current page index

                        this.FillDataForPage(this.RadDataPager1.CurrentPageIndex);

                    }

                    else//number of displaying pages has changed

                    {

                        if (this.RadDataPager1.CurrentPageIndex > iActualPageCount - 1)//we were on the last page before we deleted an item

                        {//fire PageCommand for ('what the last page was' - 1)

                            RadDataPager1.FireCommand(RadDataPager.PageCommandName, (this.RadDataPager1.CurrentPageIndex - 1).ToString());

                        }

                        else//we were not on the last page

                        {//since our page index hasn't changed, fill data for the current page index

                            this.FillDataForPage(this.RadDataPager1.CurrentPageIndex);

                        }

                    }

                   

                }

            }

        }

    }

     

  5. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 02 Feb 2012 Link to this post

    Hi Daniel,

    You cannot prevent the PageIndexChanged event from firing, but you can customize  the code in the PageIndexChanged event handler to not query your database if the total row count has changed.

    On your second question, RadDataPager can be used without binding to any particular pageable control. As you are already doing, once you provide correct row count in the OnTotalRowCounRequest event, the data pager will properly update its state and generate the necessary UI for supporting paging. Then you can use the PageIndexChanged event to retrieve the new page index and display the appropriate data on your page.

    Veli
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Back to Top