Hi nadeem1218,
Refer the below sample.
HTML
<asp:ListView ID="ListView1" runat="server" GroupItemCount="3" GroupPlaceholderID="groupPlaceHolder1"
ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
<table>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<table cellpadding="2" cellspacing="0" border="1" style="width: 200px; height: 100px;
border: dashed 2px #04AFEF; background-color: #B0E2F5">
<tr>
<td>
<b><u><span class="name">
<%# Eval("ContactName") %></span></u></b>
</td>
</tr>
<tr>
<td>
<b>CustomerId: </b><span class="city">
<%# Eval("CustomerId")%></span><br />
<b>CompanyName: </b><span class="postal">
<%# Eval("CompanyName")%></span><br />
</td>
</tr>
</table>
</td>
</ItemTemplate>
</asp:ListView>
<br />
<asp:Label ID="lblPageer" runat="server" />
<asp:LinkButton ID="lnkPrevious" Text="Previous" runat="server" OnClick="Previous" />
<asp:LinkButton ID="lnkNext" Text="Next" runat="server" OnClick="Next" />
C#
private int PageSize = 18;
static int RecordCount = 0;
static int PageIndex = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GetCustomersPageWise(PageIndex);
}
}
private void GetCustomersPageWise(int pageIndex)
{
string constring = ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("Customers_GetCustomersPageWise", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
cmd.Parameters.AddWithValue("@PageSize", PageSize);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
con.Open();
IDataReader idr = cmd.ExecuteReader();
ListView1.DataSource = idr;
ListView1.DataBind();
idr.Close();
con.Close();
RecordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
int fromvalue = ((PageSize * pageIndex + 1) - PageSize);
int toValue = ((pageIndex * PageSize) <= RecordCount) ? (pageIndex * PageSize) : RecordCount;
lblPageer.Text = fromvalue + " to " + toValue + " out of " + RecordCount;
this.lnkPrevious.Visible = pageIndex > 1;
this.lnkNext.Visible = ((pageIndex * PageSize) > RecordCount) ? false : true;
}
}
}
protected void Next(object sender, EventArgs e)
{
PageIndex++;
this.GetCustomersPageWise(PageIndex);
}
protected void Previous(object sender, EventArgs e)
{
PageIndex--;
this.GetCustomersPageWise(PageIndex);
}
VB.Net
Private PageSize As Integer = 18
Shared RecordCount As Integer = 0
Shared PageIndex As Integer = 1
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.GetCustomersPageWise(PageIndex)
End If
End Sub
Private Sub GetCustomersPageWise(pageIndex As Integer)
Dim constring As String = ConfigurationManager.ConnectionStrings("constring").ConnectionString
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("Customers_GetCustomersPageWise", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@PageIndex", pageIndex)
cmd.Parameters.AddWithValue("@PageSize", PageSize)
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4)
cmd.Parameters("@RecordCount").Direction = ParameterDirection.Output
con.Open()
Dim idr As IDataReader = cmd.ExecuteReader()
ListView1.DataSource = idr
ListView1.DataBind()
idr.Close()
con.Close()
RecordCount = Convert.ToInt32(cmd.Parameters("@RecordCount").Value)
Dim fromvalue As Integer = ((PageSize * pageIndex + 1) - PageSize)
Dim toValue As Integer = If(((pageIndex * PageSize) <= RecordCount), (pageIndex * PageSize), RecordCount)
lblPageer.Text = fromvalue.ToString() + " to " + toValue.ToString() + " out of " + RecordCount.ToString()
Me.lnkPrevious.Visible = pageIndex > 1
Me.lnkNext.Visible = If(((pageIndex * PageSize) > RecordCount), False, True)
End Using
End Using
End Sub
Protected Sub [Next](sender As Object, e As EventArgs)
PageIndex += 1
Me.GetCustomersPageWise(PageIndex)
End Sub
Protected Sub Previous(sender As Object, e As EventArgs)
PageIndex -= 1
Me.GetCustomersPageWise(PageIndex)
End Sub
SQL
Create PROCEDURE [dbo].[Customers_GetCustomersPageWise]
@PageIndex INT = 1,
@PageSize INT = 20,
@RecordCount INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SELECT (ROW_NUMBER() OVER(Order By CustomerId)) AS RowNumber,
[CustomerID]
,[CompanyName]
,[ContactName]
,[ContactTitle]
,[Address]
,[City]
,[Region]
,[PostalCode]
,[Country]
,[Phone]
,[Fax]
INTO #Results
FROM [Customers]
SELECT @RecordCount = Count(*) FROM #Results
SELECT * FROM #Results WHERE
ROWNUMBER BETWEEN (@PageIndex-1)*@PageSize + 1 AND (((@PageIndex-1)*@PageSize + 1)+@PageSize)-1 OR @PageIndex = -1
DROP TABLE #Results
END
Screenshot
