03 April 2017

How To Enable Paging in ListView

I know it's a bit old, but there are newbies who still had difficulty in adding a pager in their ListView control. So I think that justifies why I should post this blog.

If for example you add a DataPager in you ListView's LayoutTemplate

<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="5">
        <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true" ShowNextPageButton="false" />                                       <asp:NumericPagerField ButtonType="Link" />
        <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false" ShowPreviousPageButton="false" />

First, you need to set the OnPagePropertiesChanging="OnPagePropertiesChanging" on your ListView properties like:

<asp:ListView ID="ListView1" runat="server" OnPagePropertiesChanging="OnPagePropertiesChanging">

Then, from code behind you can do the following:

protected void OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        (ListView1.FindControl("DataPager1") as DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false);

        //call your method in displaying the list

No comments:

Post a Comment