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>
Before we do that we need to add a reference to the connection string that is stored in the web.config file. I have added mine in above the Page_Load event.
- private string connectionString =
- WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
- string queryString = "select * from dbo.TestTable";
- SqlConnection con = new SqlConnection(connectionString);
- SqlCommand cmd = new SqlCommand(queryString, con);
- SqlDataAdapter adp = new SqlDataAdapter(cmd);
- Con.Open();
- DataSet ds = new DataSet();
- adp.Fill(ds, "TestTable");
- ListView1.DataSource = ds;
- ListView1.DataBind();
- con.Close();
- protected void getData()
- {
- string queryString = "select * from dbo.TestTable";
- SqlConnection con = new SqlConnection(connectionString);
- SqlCommand cmd = new SqlCommand(queryString, con);
- SqlDataAdapter adp = new SqlDataAdapter(cmd);
- try
- {
- con.Open();
- DataSet ds = new DataSet();
- adp.Fill(ds, "TestTable");
- ListView1.DataSource = ds;
- ListView1.DataBind();
- } catch (Exception er)
- {
- lblError.Text = "Error retrieving data: " + er.Message;
- } finally
- {
- con.Close();
- }
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!this.IsPostBack)
- {
- getData();
- }
- }
In order to correct this you will need to implement the following steps:
First add the ‘OnPagePropertiesChanging’ event to the listview. It is highlighted in yellow for convenience.
- <asp:ListView ID="ListView1" runat="server" OnPagePropertiesChanging="ListView1_PagePropertiesChanging" GroupItemCount="3">
- protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e) {}
- DataPager dp = (DataPager)ListView1.FindControl("DataPager1");
- dp.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
- getData();
- protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
- {
- DataPager dp = (DataPager) ListView1.FindControl("DataPager1");
- dp.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
- getData();
- }
I have included a downloadable project for you to examine and play with at your leisure.I hope you found this tutorial helpful and easy to follow if you have any questions or comments please add them below in the comments section.
If you know of anyone who may find this article interesting or helpful then please share it around.

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