http://www.codedigest.com/Articles/ASPNET/100_Paging_in_ListView_in_ASPNet_35.aspx
Paging in ListView in ASP.Net 3.5
My previous article, ListView Control in ASP.Net 3.5, provides a good understanding on the ListView control with a sample databinding. Our next step in exploring the ListView control is to add the paging feature to it. Like GridView, Paging in ListView control is not an integral part of the control itself. It can be provided through a new control in ASP.Net 3.5 called DataPager control. The main advantage of providing this feature with a new control are, Paging can be separated from ListView Control and can be displayed at any part of the page. We can attach the paging feature with the ListView control by placing the DataPager control in the LayoutTemplate of the ListView control which resembles GridView control.
In this article, we will implement paging feature with DataPager control in 3 different ways,
Ø Paging in ListView through Datasource control databind.
Ø Paging in ListView without using DataSource Controls.
Ø Providing Custom interface for the Paging in ListView.
Paging in ListView through Datasource control databind
Before moving to paging implementation with DataPager we will bind the ListView by configuring a SqlDataSource control.
Configuring SqlDataSource Control
Drag a SqlDataSource control from the data tab of Visual Studio 2008. Configure it with the Database Server; I have used a database in App_Data folder.
Configuring ListView Control
Drag a ListView control from the data tab of Visual Studio 2008. Click the Smart tag of ListView control and select the datasource control; Refer the below figure, SqlDataSource1 in our example.
Click “Configure ListView...” option as seen in the above figure to choose the display Layout for the ListView. Choose a display layout, I have selected “Flow” in our example, as seen in the below figure. Click Ok.You can check “Enable Paging” in the above figure, which will add a DataPager control in the LayoutTemplate of the ListView control but we will add the DataPager control explicitly in this article. Executing the page will display the records without paging. Now, we will provide paging feature through the DataPager control.
Configure Paging through DataPager
Drag a DataPager control from the data tab of Visual Studio 2008. Assign the PagedControlID property to the ListView control’s ID. Set PageSize property if required, default is 10. I have set as 2 in our example.
There are 2 default pager styles that can be set to a DataPager control, Next/Previous Pager and Numeric Pager. Without setting this there will be no pagination displayed for the ListView. To configure, Click the smart tag of DataPager control and choose the style as shown in the below figure,
Configuring DataPager inside ListView
The above walkthrough placed the DataPager control outside the ListView control, in other words, the paging can be displayed at any part of the page. In this section, we will see how the DataPager can be a part of ListView control itself.
It can be done by placing the DataPager control inside the LayoutTemplate of the ListView. In this case, we neither need nor set the PagedControlID property because DataPager control itself will render accordingly when it is placed inside the ListView control.
DataSourceID="SqlDataSource1">
style="font-family: Verdana, Arial, Helvetica, sans-serif;">
EmpID:
EmpName:
Department:
Text='<%# Eval("Department") %>' />
Age:
Address:
In the next section, we will provide paging to ListView control with DataPager without using DataSource controls.
Paging with DataPager without using DataSource Controls
1. Drag a ListView control inside a webform as we did in previous section.
EmpID:
EmpName:
Text='<%# Eval("EmpName") %>' />
Department:
Text='<%# Eval("Department") %>' />
Age:
Address:
2. In the codebehind file, assign the DataTable or DataSet to the DataSource Property of the ListView control like we do for a GridView control.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lvEmployee.DataSource = GetEmployee("Select * from Employee");
lvEmployee.DataBind();
}
}
public DataTable GetEmployee(string query)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
SqlDataAdapter ada = new SqlDataAdapter(query, con);
DataTable dtEmp = new DataTable();
ada.Fill(dtEmp);
return dtEmp;
}
3. Drag a DataPager control from the Data tab of visual studio. When we execute the page, Paging will not work automatically; we need to bind the ListView to the datasource again in the PreRender event of DataPager.
PageSize="2" onprerender="DataPager1_PreRender">
protected void DataPager1_PreRender(object sender, EventArgs e)
{
lvEmployee.DataSource = GetEmployee("Select * from Employee");
lvEmployee.DataBind();
}
Execute the page and you can see your ListView paging working perfectly.
Recent Articles
Rotate Text Link Advertisements Using JQuery in ASP.Net
Posted on 8/19/2010 @ 11:04 AM By Satheesh Babu B
Disable Public holidays in ASP.Net Calendar Control
Posted on 7/30/2010 @ 7:37 AM By Bala Murugan
How to Pass Values from One Page to Another in ASP.Net?
Posted on 7/24/2010 @ 1:31 AM By Bala Murugan
Using ADRotator Control in ASP.Net-Part 2
Posted on 7/17/2010 @ 1:21 AM By Satheesh Babu B
Pass Values from CodeBehind to JavaScript and From JavaScript to CodeBehind in ASP.Net
Posted on 7/10/2010 @ 1:36 AM By Satheesh Babu B
Providing Custom interface for the Paging in ListView
The previous 2 sections of this article provided the paging interface as a Previous, Next button or a numeric pager. Sometimes, we may require a textbox or a dropdown where will select our page number and display the corresponding page in the ListView control. Refer the below figure to have more understanding,
This can be achieved by of the DataPager.
First, we will implement the page selection via the DropDownList control.
Navigate to a page using DropDownList
PageSize="3" onprerender="DataPager1_PreRender">
We can fill the dropdownlist with page numbers in the DataBound event of the ListView. In SelectedIndexChanged event of dropdownlist, we can bind the corresponding page using SetPageProperties method of DataPager.
int CurrentPage = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lvEmployee.DataSource = GetEmployee("Select * from Employee order by empid");
lvEmployee.DataBind();
}
}
public DataTable GetEmployee(string query)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString);
SqlDataAdapter ada = new SqlDataAdapter(query, con);
DataTable dtEmp = new DataTable();
ada.Fill(dtEmp);
return dtEmp;
}
protected void lvEmployee_DataBound(object sender, EventArgs e)
{
DropDownList ddl = DataPager1.Controls[1].FindControl("ddlPage") as DropDownList;
int PageCount = (DataPager1.TotalRowCount / DataPager1.PageSize);
if(PageCount*DataPager1.PageSize != DataPager1.TotalRowCount)
{
PageCount = PageCount + 1;
}
for (int i = 0; i < PageCount; i++ )
{
ddl.Items.Add(new ListItem((i+1).ToString(),i.ToString()));
}
ddl.Items.FindByValue(CurrentPage.ToString()).Selected = true;
}
protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
CurrentPage = int.Parse(ddl.SelectedValue);
int PageSize = DataPager1.PageSize;
DataPager1.SetPageProperties(CurrentPage * PageSize, PageSize, true);
}
protected void DataPager1_PreRender(object sender, EventArgs e)
{
lvEmployee.DataSource = GetEmployee("Select * from Employee order by empid");
lvEmployee.DataBind();
}
When we execute the page, we can navigate between the pages using the dropdownlist.
Navigate to a page using TextBox control
To navigate to a particular page using the textbox control we can use the below code. Since, we have seen how to use a control inside DataPager control we will put the textbox control outside the DataPager and bind the ListView.
protected void btnGoPage_Click(object sender, EventArgs e)
{
if(txtPage.Text != "")
DataPager1.SetPageProperties( (int.Parse(txtPage.Text)-1) * DataPager1.PageSize, DataPager1.PageSize, true);
}
The above code will navigate to the page number entered in the TextBox control.