Kris B
Kris B

Reputation: 3578

Multiple DataPagers on same page

I'm having an issue where I have 2 DataPagers on the same page, linked to the same ListView. Everything works fine, except the "bottom" or 2nd pager doesn't seem to be working. The page numbers are generated, but clicking on them does nothing. If I copy the "bottom" pager above the "top" pager, then that pager will work, but the one below it doesn't. Seems a only the pager that comes first seems to work:

<asp:DataPager ID="dpPagerTop" runat="server" PagedControlID="lvOutput" QueryStringField="pageNumber">
    <Fields>
        <asp:NumericPagerField NextPageText="Next" PreviousPageText="Previous" />
    </Fields>
</asp:DataPager>

<asp:DataPager ID="dpPagerBottom" runat="server" PagedControlID="lvOutput" QueryStringField="pageNumber">
    <Fields>
        <asp:NumericPagerField NextPageText="Next" PreviousPageText="Previous" />
    </Fields>
</asp:DataPager>

<asp:ListView ID="lvOutput" runat="server" OnPagePropertiesChanged="lvOutput_PagePropertiesChanged">
    <LayoutTemplate>
        <asp:PlaceHolder id="itemPlaceholder" runat="server" />
    </LayoutTemplate>
    <ItemTemplate>
        <a href="<%# Eval("Link") %>" title="<%# Eval("Title") %>"><%# Eval("Title") %></a>
    </ItemTemplate>
</asp:ListView>


protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        dpPagerTop.SetPageProperties(Request.QueryString["pageNumber"].ToString(), 25, false);
        dpPagerBottom.SetPageProperties(Request.QueryString["pageNumber"].ToString(), 25, false);

        lvOutput.DataSource = [datasource];
        lvOutput.DataBind();
    }
}


protected void lvOutput_PagePropertiesChanged(object sender, EventArgs e)
{
    lvOutput.DataBind();
}

UPDATE:

After fooling around with this some more, I've determined that both pagers will work if SetPageProperties has the correct parameters. The first parameter should be the number to start the results and the second should be number of results to show. However, I am getting the wrong numbers to display. I have exactly 100 records and I want to display 25 results per page. If I hardcode:

dpPagerTop.SetPageProperties(25, 25, true);
dpPagerBottom.SetPageProperties(25, 25, true);

This should be the 2nd page of the results and the results show 26-50. However, the bottom pager doesn't work.

Now, if I hardcode:

dpPagerTop.SetPageProperties(26, 25, true);
dpPagerBottom.SetPageProperties(26, 25, true);

Both pagers work like the should, but the number of results go from 27-51.

Can anyone recreate this, it's driving me nuts?!?!?

UPDATE 2:

I think I got it to work by setting the page properties BEFORE binding to the ListView.

Upvotes: 0

Views: 4101

Answers (2)

FiveTools
FiveTools

Reputation: 6030

I had a similar problem with two datapagers on a page bound to one listview. the datapagers weren't synchronized with each - so changes to the top then bottom pagers would have the appearance of the pager not working. This method put them back on track:

 protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
    {
        DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
        DataPager2.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
    }

Upvotes: 2

cptScarlet
cptScarlet

Reputation: 6136

I think I have this figured out.

First from what I can tell you need to databind the listview before you set the page properties.

Secondly, I think you are misunderstanding the first parameter to the SetPageProperties method. It does not set the current page, it sets the first record on this page of data.

Here is the HTML I am using

<asp:DataPager ID="dpPagerTop" runat="server" PagedControlID="lvOutput" QueryStringField="pageNumber"
    PageSize="2">
    <Fields>
        <asp:NumericPagerField NextPageText="Next" PreviousPageText="Previous" />
    </Fields>
</asp:DataPager>
<asp:DataPager ID="dpPagerBottom" runat="server" PagedControlID="lvOutput" QueryStringField="pageNumber"
    PageSize="2">
    <Fields>
        <asp:NumericPagerField NextPageText="Next" PreviousPageText="Previous" />
    </Fields>
</asp:DataPager>

<asp:ListView ID="lvOutput" runat="server" OnPagePropertiesChanged="lvOutput_PagePropertiesChanged">
    <LayoutTemplate>
        <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
    </LayoutTemplate>
    <ItemTemplate>
        <a href="Donation.aspx" title="<%# Eval("Type") %>">
            <%# Eval("id")%></a>
    </ItemTemplate>
</asp:ListView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:LutheranAssistanceConnectionString %>" 
    SelectCommand="SELECT [Id], [RecipientId], [Type], [Reason] FROM [Donations]">
</asp:SqlDataSource>

Here is the code in the code behind

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //bind the list view first
            lvOutput.DataSource = SqlDataSource1;
            lvOutput.DataBind();

            //the first parameter of SetPageProperties is not the page number 
            //it is index of the first record on the page
            //So we need to calculate the index based on the passed in page number.
            int pageNumber = Convert.ToInt32(Request["pageNumber"]);
            int recordNumber = pageNumber * dpPagerTop.PageSize;

            //now set first record
            dpPagerTop.SetPageProperties(recordNumber , 25, false); 
            dpPagerBottom.SetPageProperties(recordNumber , 25, false);
        }
    }

    protected void lvOutput_PagePropertiesChanged(object sender, EventArgs e)
    {
        lvOutput.DataBind();
    }

Hope this helps

Upvotes: 0

Related Questions