Introduction
In this article I will explain how to implement paging in Listview
Control. Paging is very much helpful in case of large data.
Implementation:
Design page:
·
Create
an application and add new webpage to the application. Now add Listview on this page.
·
Now
fire onpagepropertieschanging event of the ListView.
·
Now
Add DataPager to this page
·
By
Default DataPager displays 10 records
per page, but we can change no of records by setting PageSize property of DataPager .
<fieldset style="width:300px"><legend
><strong>Pagination in ListView Using DataPager</strong></legend><br />
<asp:ListView ID="ListView1" runat="server"
onpagepropertieschanging="ListView1_PagePropertiesChanging">
<LayoutTemplate>
<table><tr><th>Employee ID</th>
<th>Employee Name</th>
<th>Age</th>
</tr>
<tr id="ItemPlaceHolder" runat="server"></tr>
</LayoutTemplate>
<ItemTemplate>
<tr><td><%#Eval("EmpPer_ID")
%></td>
<td><%#Eval("EmpName")
%></td>
<td><%#Eval("Age")
%></td>
</tr>
</ItemTemplate>
</asp:ListView></table><br />
<asp:DataPager ID="DataPager1" runat="server" PageSize="3" PagedControlID="Listview1">
<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>
</fieldset>
ASP.NET Code(Using C#)
·
Create a function to get data data from database and bind it to ListView.
·
Call this function on page load after checking IsPostBack Property.
·
Now in ListView’s onpagepropertieschanging event write the code given below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class
Listviewpaging : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
protected void
Page_Load(object sender, EventArgs e)
{
if
(!IsPostBack)
{
bindListView();
}
}
public void
bindListView()
{
DataTable
dt = new DataTable();
SqlDataAdapter
adp = new SqlDataAdapter("select * from Emp_Personal", con);
adp.Fill(dt);
ListView1.DataSource = dt;
ListView1.DataBind();
}
protected void
ListView1_PagePropertiesChanging(object sender,
PagePropertiesChangingEventArgs e)
{
DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
bindListView();
}
}
0 comments:
Post a Comment