Introduction: In this
article I will explain how to implement the paging in asp.net listview control
using datapager without using datasource control
Description:
In
the previous article I have explained Add dynamically created controls towebpage and read their values in asp.net and Display gridview selected rowvalues on another page in asp.net.
To
implement this functionality add a webform to project. Drag and drop the
listview and datapager control from toolbox to webform.
HTML
markup of webpage:
<table>
<tr><td>
<asp:ListView ID="ListView1" runat="server"
onpagepropertieschanging="ListView1_PagePropertiesChanging">
<LayoutTemplate>
<table>
<tr>
<th>Student Name</th>
<th>Student
Address</th>
<th>Student Class</th>
</tr>
<tr id="itemPlaceholder" runat="server"></tr>
</LayoutTemplate>
<ItemTemplate>
<tr align="center">
<td> <asp:Label ID="lblstudentname"
runat="server"
Text='<%#Eval("Student_Name")%>'></asp:Label></td>
<td><asp:Label ID="lbladdress" runat="server" Text='<%#Eval("Student_Address") %>'></asp:Label></td>
<td><asp:Label ID="lblroll" runat="server" Text='<%#Eval("RollNo") %>'></asp:Label></td>
</tr>
</ItemTemplate>
</asp:ListView>
</td></tr>
<tr><td></td></tr>
<tr><td><asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="3">
<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>
After
that write the code to bind the listview.
Add
the namespaces
C#:-
using
System.Data;
using
System.Data.SqlClient;
using
System.Configuration;
VB:-
Imports
System.Data
Imports
System.Data.SqlClient
Imports
System.Configuration
Create
a connection.
C#:-
SqlConnection
con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
VB:-
Private
con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection").ToString())
Write
the code to bind the listview
C#:-
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
Bindlist();
}
}
public void Bindlist()
{
SqlDataAdapter
adp = new SqlDataAdapter("Select * from Tb_Student", con);
DataTable
dt = new DataTable();
adp.Fill(dt);
if
(dt.Rows.Count > 0)
{
ListView1.DataSource = dt;
ListView1.DataBind();
}
}
VB:-
Protected Sub Page_Load(sender As
Object, e As
System.EventArgs) Handles
Me.Load
If Not IsPostBack Then
Bindlist()
End If
End Sub
Public Sub Bindlist()
Dim adp
As New SqlDataAdapter("Select
* from Tb_Student", con)
Dim dt As New DataTable()
adp.Fill(dt)
If
dt.Rows.Count > 0 Then
ListView1.DataSource = dt
ListView1.DataBind()
End If
End Sub
When page is changed onpagepropertieschanging event is triggered. So we write the code on this event to
implement the paging.
C#:-
protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs
e)
{
DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
Bindlist();
}
VB:-
Protected Sub ListView1_PagePropertiesChanging(sender As Object, e As System.Web.UI.WebControls.PagePropertiesChangingEventArgs)
Handles ListView1.PagePropertiesChanging
DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
Bindlist()
End Sub
Build,
run the project and see the result.
In this article we have learn how to implement the pagination in asp.net listview control using datapager without using datasource control (C#,VB). I hope you enjoyed this article.
No comments:
Post a Comment