Today I would like to discuss paging a listview control. First I will show you paging a list view that uses a SqlDataSource control then I will show you paging a list view that does not use a data source control.
The DataPager control makes paging almost effortless when used in conjunction with a DataSource control as you will see.
You will need a database to query. Fortunately in the downloadable project there is a database included along with all the code examples we will be discussing.
On your .aspxpage add the following markup:
- <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString='<%$ ConnectionStrings:DefaultConnection %>' SelectCommand="SELECT [TestCol1], [TestCol2], [TestCol3] FROM [TestTable]"></asp:SqlDataSource>
- <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="TestCol1" GroupItemCount="3">
- <AlternatingItemTemplate>
- <td runat="server" style="background:#808080;color:#ffffff;">TestCol1:
- <asp:Label Text='<%# Eval("TestCol1") %>' runat="server" ID="TestCol1Label" /><br /> TestCol2:
- <asp:Label Text='<%# Eval("TestCol2") %>' runat="server" ID="TestCol2Label" /><br /> TestCol3:
- <asp:Label Text='<%# Eval("TestCol3") %>' runat="server" ID="TestCol3Label" /><br />
- </td>
- </AlternatingItemTemplate>
- <EmptyDataTemplate>
- <table runat="server">
- <tr>
- <td>No data was returned.</td>
- </tr>
- </table>
- </EmptyDataTemplate>
- <EmptyItemTemplate>
- <td runat="server" />
- </EmptyItemTemplate>
- <GroupTemplate>
- <tr runat="server" id="itemPlaceholderContainer">
- <td runat="server" id="itemPlaceholder"></td>
- </tr>
- </GroupTemplate>
- <ItemTemplate>
- <td runat="server">TestCol1:
- <asp:Label Text='<%# Eval("TestCol1") %>' runat="server" ID="TestCol1Label" /><br /> TestCol2:
- <asp:Label Text='<%# Eval("TestCol2") %>' runat="server" ID="TestCol2Label" /><br /> TestCol3:
- <asp:Label Text='<%# Eval("TestCol3") %>' runat="server" ID="TestCol3Label" /><br />
- </td>
- </ItemTemplate>
- <LayoutTemplate>
- <table runat="server">
- <tr runat="server">
- <td runat="server">
- <table runat="server" id="groupPlaceholderContainer" border="0">
- <tr runat="server" id="groupPlaceholder"></tr>
- </table>
- </td>
- </tr>
- <tr runat="server">
- <td runat="server">
- <asp:DataPager runat="server" PageSize="5" ID="DataPager1">
- <Fields>
- <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False"></asp:NextPreviousPagerField>
- <asp:NumericPagerField></asp:NumericPagerField>
- <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False"></asp:NextPreviousPagerField>
- </Fields>
- </asp:DataPager>
- </td>
- </tr>
- </table>
- </LayoutTemplate>
- </asp:ListView>
We have also defined an ItemTemplate which defines the layout of each individual item,A GroupTemplate which contains a placeholder for the item and a LayoutTemplate which defines the layout of the items and also contains our DataPager control.
If you run the application you’ll see that it displays the first 5 items and when you click the little number 2 you’ll see the final 2 items. And that’s it you now have a ListView that pages through data without writing a single line of C# code. Wonderful isn’t it? It is so easy that anyone could do it. A group of ASP.NET controls that work together in complete harmony straight out of the box.
However if you have ever tied this without a data source control you will know that some strange behavior occurs. What was so easy only moments ago, in the previous example, suddenly becomes a bit of a nightmare. To demonstrate thisI will show you what happens when you try to page a list view without a Sql Data Source. In the downloadable project included with this post there is a webform called Default2.aspx. On that webform is two list views, the next example is taken from the list view on the top half of the page.
On your .aspx page add the following markup:
- <asp:Label ID="lblError" runat="server"></asp:Label>
- <asp:ListView ID="ListView1" runat="server" GroupItemCount="3">
- <EmptyDataTemplate>
- <table runat="server">
- <tr>
- <td>No data was returned.</td>
- </tr>
- </table>
- </EmptyDataTemplate>
- <EmptyItemTemplate>
- <td runat="server" />
- </EmptyItemTemplate>
- <GroupTemplate>
- <tr runat="server" id="itemPlaceholderContainer">
- <td runat="server" id="itemPlaceholder"></td>
- </tr>
- </GroupTemplate>
- <ItemTemplate>
- <td runat="server">TestCol1:
- <asp:Label Text='<%# Eval("TestCol1") %>' runat="server" ID="TestCol1Label" /><br /> TestCol2:
- <asp:Label Text='<%# Eval("TestCol2") %>' runat="server" ID="TestCol2Label" /><br /> TestCol3:
- <asp:Label Text='<%# Eval("TestCol3") %>' runat="server" ID="TestCol3Label" /><br />
- </td>
- </ItemTemplate>
- <LayoutTemplate>
- <table runat="server">
- <tr runat="server">
- <td runat="server">
- <table runat="server" id="groupPlaceholderContainer" border="0">
- <tr runat="server" id="groupPlaceholder"></tr>
- </table>
- </td>
- </tr>
- <tr runat="server">
- <td runat="server">
- <asp:DataPager runat="server" PageSize="5" ID="DataPager1">
- <Fields>
- <asp:NumericPagerField></asp:NumericPagerField>
- </Fields>
- </asp:DataPager>
- </td>
- </tr>
- </table>
- </LayoutTemplate>
- </asp:ListView>

Santhakumar MunuswamyPosted Feb 12, 2016, 11:16 PM
Thanks for nice share