1

DataPager has some strange behavior.

So to situate the problem, I have a DataPagerReapeater with information. And I have a DataPager, which I made to work together. I have 3 pages, but DataPager has some strange behavior.

When I'm on the first page and I click next it goes to 2nd, everything is fine. When I click next again it does a postback but doesnt move to the 3rd page. Last and first also works fine.

But when I'm on the second page and I click next it will not move to the third page, but stays on the second. Same if I manually click on the third page and click previous, it goes to the first page.

I really dont understand why.

Here is the DataPager:

<asp:DataPager ID="DataPager1" PagedControlID="ReapeaterCSGator" PageSize="5" 
        runat="server" onprerender="DataPager1_PreRender">
    <fields>
            <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="True"  FirstPageText="<< First"
            ShowNextPageButton="False" ShowPreviousPageButton="False" />
        <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="False"  FirstPageText="< Previous"
            ShowNextPageButton="False" ShowPreviousPageButton="True" />
        <asp:NumericPagerField />
        <asp:NextPreviousPagerField ButtonType="Link" ShowLastPageButton="False"  LastPageText="Next >"
            ShowNextPageButton="True" ShowPreviousPageButton="False" />
        <asp:NextPreviousPagerField ButtonType="Link" ShowLastPageButton="True"  LastPageText="Last >>"
            ShowNextPageButton="False" ShowPreviousPageButton="False" />
    </fields>
</asp:DataPager>

Here is the code which I execute on PreRender:

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);

    IEnumerable<CollaborativeSpace> listCS = LoadCollaborativeSpaces();
    // Binding the repeater with the list of documents
    ReapeaterCSGator.DataSource = listCS;
    ReapeaterCSGator.DataBind();

}

So, the behavior is really strange and I have no idea what the problem could be.

Anyone else confronted such an issue?

UPDATE: Here is on load method and what i have in there:

        ResultPerPages = GetResultsPerPage();
        DataPager2.PageSize = ResultPerPages;
        DataPager1.PageSize = ResultPerPages;
        //We initialize the pager repeater with the same value
        ReapeaterCSGator.SetPageProperties(0, ResultPerPages, false);
        //We add an handler on item data bound event for sub repeater
        ReapeaterCSGator.ItemDataBound += ReapeaterCSGator_ItemDataBound;
        //If the user is not post backing
        if (!IsPostBack)
        {
            //We add choices on drop down list "Results per page"
            foreach (int choice in NbResultsChoices)
            {
                NbResultsPerPage.Items.Add(new ListItem(choice + " results per page", choice.ToString(CultureInfo.InvariantCulture)));
            }
            //We get collaborative spaces from Sharepoint list
            //IEnumerable<CollaborativeSpace> listCS = LoadCollaborativeSpaces();
            //// Binding the repeater with the list of documents
            //ReapeaterCSGator.DataSource = listCS;

UPDATE 2: Here is the code behind SetPageProperties()

 public void SetPageProperties(int startRowIndex, int maximumRows, bool databind)
    {
        ViewState["_startRowIndex"] =startRowIndex;
        ViewState["_maximumRows"] = maximumRows;
        if (TotalRows > -1)
        {
            if (TotalRowCountAvailable != null)
            {
                TotalRowCountAvailable(this, new PageEventArgs((int)ViewState["_startRowIndex"], (int)ViewState["_maximumRows"], TotalRows));
            }
        }
    }

That component was used form here: http://www.codeproject.com/Articles/45163/Extend-Repeater-to-support-DataPager

PROBLEM SOLVED, it seems that the datapagerrepeater wasnt implemented the right way, now that i found the sources i could fix it. Thanks anyways for the help

Alnedru
  • 2,573
  • 9
  • 50
  • 88

1 Answers1

1

If I read your load method right, you're resetting the repeater to page 1 on every Page_Load.

What happens is:

  • In Page_Load, you reset the repeater to page 1 in the SetPagerProperties() call
  • During the control event dispatch phase, the datapager is advanced to the next page relative to page 1:
    • if you use "first" and "last" and specific pages, everything works, because they're not relative changes
    • "next" goes to the page after page 1, which is why you're stuck on page 2,
    • "previous" tries to go to the page before 1, since there is none, it stays on 1.

To fix this, stop initialising the pager on every page load. Either get rid of the call – I'm not sure why it's there, I only use it to "reset" the repeater, for example after a user clicks a column header to sort a list view. Or move it into the if (!IsPostback) block.

millimoose
  • 39,073
  • 9
  • 82
  • 134
  • Yes, you are right, but now im lost here a bit even if i set it in (!postback) i still have the same problem. The datapagerrepeater is custom control, made not by me ... but the thing is it is always been given 0 with setPagerProperties, but i suppose i need to give tehre index that increases .. or something – Alnedru Jan 31 '12 at 10:24
  • @user1002583 Not sure about the repeater, but you shouldn't need to manually change pages with the standard `asp:DataPager` using `SetPagerProperties()`. When you click on one of the data pager buttons, the pager will determine how to increase the start row index, and call the method on the paged control correctly. – millimoose Jan 31 '12 at 13:50
  • @user1002583 Also, if the problem is in the `DataPagerRepeater` from CodeProject, why use it at all? It seems like you could just use a standard [ListView](http://msdn.microsoft.com/en-us/library/ie/bb398790.aspx) which also supports paging. – millimoose Jan 31 '12 at 13:51
  • @user1002583 As you can see in the example in that documentation, no server side code should be necessary for paging to work if you use a data source control. (If using an explicit data source, I'm not really sure, I always use an `ObjectDataSource` when I need paging or sorting.) – millimoose Jan 31 '12 at 14:10