Search code examples
asp.netlistviewquery-stringdatapager

Determine current page without using the DataPager's QueryString (ListView ASP.NET)


I have a Tab Control that contains a ListView, there are three Tabs, each tab contains the same ListView but the Data Source is changed whenever the tab chagnes.

Lets say there are 3 Tabs: Tab1: Customers Tab2: Items Tab3: Orders

The problem comes when I use a DataPager. (Previous 1 2 3 4 5 6 ... Next) The DataPager uses a QueryString:

<asp:DataPager runat="server" ID="DataPager1" PagedControlID="ListView1" PageSize="5"     QueryStringField="page"  OnPreRender="Pager_PreRender" >
    <Fields>
        <asp:NextPreviousPagerField ButtonType="Button" PreviousPageText="Previous" />
        <asp:NumericPagerField ButtonType="Link" ButtonCount="10" />
        <asp:NextPreviousPagerField ButtonType="Button" NextPageText="Next" />
    </Fields>
</asp:DataPager>

This produces the following URL:

http:// localhost /MyProject/view/Customers?page=1

Lets say I'm in Tab3: Orders, if I navigate to the next Page a postback occurs and Tab1: Customers is displayed instead and that is because the URL indicates view/Customers?page=1 Is there anyway to achieve paging without using the QueryString?

Here is where I use the QueryString:

    protected void Pager_PreRender(object sender, EventArgs e)
    {
        int CurrentPage = 0;
        Int32.TryParse(Request.QueryString["page"], out CurrentPage);
        CurrentPage = CurrentPage.Equals(0) ? 1 : CurrentPage;

        HyperLink PreviousLink = DataPager1.Controls[0].Controls[0] as HyperLink;
        HyperLink NextLink = DataPager1.Controls[2].Controls[0] as HyperLink;
        if (PreviousLink != null)
        {
            if (CurrentPage.Equals(1))
            {
                PreviousLink.Visible = false;
            }
            else if (CurrentPage > 1)
            {
                PreviousLink.Visible = true;
            }
        }
        if (NextLink != null)
        {
            if ((CurrentPage * DataPager1.PageSize) >= DataPager1.TotalRowCount)
            {
                NextLink.Visible = false;
            }
            else
            {
                NextLink.Visible = true;
            }
        }
    }

Solution

  • You could use a PagingDataSet and use a HiddenField to store the page index. Just explained in another answer...

    Custom datatable with paging

    Make sense?