views:

526

answers:

1

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.

A: 

I think I have this figured out.

First from what I can tell you need to databind the listviwe 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

cptScarlet
Thanks for the reply. The problem isn't that the results are not paging correctly, I'm getting the right results if I use the "top" pager only. It's just the the 2nd pager doesn't work at all. Like I said, it will work, if I move the 2nd pager above the 1st pager (then the 1st pager will stop working). I also moved the binding to before the SetPageProperties, but that didn't fix it.
Did you change the SetPageProperties Code in the code behind? THe paging did not work for me until I modified it to what I have above. Otherwise I got the page numbers but I couldn't click on them.
cptScarlet
I updated my answer to show all the HTML and C# code I am using in hopes you will notice an important difference between my code and yours. Right now this works for me and I can page with both pagers.
cptScarlet
I tried one more test and one thing I noticed was that if I did not set the page size explicitly in the .aspx page my paging did not work, even though I set a small enough page size in the SetPageProperies call to force paging. I know you said one is working for you but it may be worth explicitly setting the page size to something very small in both pagers to see if that gets both pagers working
cptScarlet
Hey, checkout my comments to the original post. I'm able to get the pagers working, but with strange results.
Hey, I think I got it to work. I set the page properties BEFORE binding the to the ListView. Not sure why, but I'm not complaining. Thanks for your help.