Introduction-
The ListView control does not have paging capabilities. DataPager control provides Paging capabilities by using
PagedControlID property. In ListView, we can use the DataPager
control and customize the number of items or rows that are displayed for each page of data by
changing the PageSize property. We can also change the way a page is submitted to the
server by setting the QueryStringField property.
Implementing DataPager in Asp.Net-
DataPager can be implemented by using below two points:-
1) The ID of the ListView control is set for the PagedControlID property of the DataPager
control.
2) define the event OnPagePropertiesChanging which will be
fired when the DataPager Page is changed or clicked.
We will also need to import the following namespaces.
using System.Data
using System.Configuration
using System.Data.SqlClient
.Aspx file-
<asp:ListView ID="lvCustomers" runat="server" groupPlaceholderID="groupPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1" OnPagePropertiesChanging="OnPagePropertiesChanging">
<LayoutTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<th>
CustomerId
</th>
<th>
ContactName
</th>
<th>
Country
</th>
</tr>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
<tr>
<td colspan = "3">
<asp:DataPager ID="DataPager1" runat="server"
PagedControlID="lvCustomers" PageSize="10">
<Fields>
<asp:NextPreviousPagerField ButtonType="Link"
ShowFirstPageButton="false" ShowPreviousPageButton="true"
ShowNextPageButton="false" />
<asp:NumericPagerField ButtonType="Link" />
<asp:NextPreviousPagerField ButtonType="Link"
ShowNextPageButton="true" ShowLastPageButton="false"ShowPreviousPageButton = "false" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server"
ID="itemPlaceHolder1"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<%# Eval("CustomerId") %>
</td>
<td>
<%# Eval("ContactName") %>
</td>
<td>
<%# Eval("Country") %>
</td>
</ItemTemplate>
</asp:ListView>
aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindListView()
}
}
private void BindListView()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT CustomerId, ContactName, Country FROM Customers"
cmd.Connection = con
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable()
sda.Fill(dt)
lvCustomers.DataSource = dt
lvCustomers.DataBind()
}
}
}
}
Handling Paging in ListView using DataPager control
When the page is changed or clicked inside the DataPager the following event hander of the
ListView is triggered. Here first the DataPager control is found and then it's SetPageProperties
method is executed so that the page numbers are recreated.
aspx.cs:
protected void OnPagePropertiesChanging(object sender,PagePropertiesChangingEventArgs e)
{
(lvCustomers.FindControl("DataPager1") as
DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, false)
this.BindListView()
}
0 Comment(s)