Monday, 1 September 2014

Paging Using Repeater

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        .Repeater, .Repeater td, .Repeater td
        {
            border: 1px solid #ccc;
        }
        .Repeater td
        {
            background-color: #eee !important;
        }
        .Repeater th
        {
            background-color: #6C6C6C !important;
            color: White;
            font-size: 10pt;
            line-height: 200%;
        }
        .Repeater span
        {
            color: black;
            font-size: 10pt;
            line-height: 200%;
        }
        .page_enabled, .page_disabled
        {
            display: inline-block;
            height: 20px;
            min-width: 20px;
            line-height: 20px;
            text-align: center;
            text-decoration: none;
            border: 1px solid #ccc;
        }
        .page_enabled
        {
            background-color: #eee;
            color: #000;
        }
        .page_disabled
        {
            background-color: #6C6C6C;
            color: #fff !important;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table class="Repeater" cellspacing="0" rules="all" border="1">
        <tr>
            <th scope="col" style="width: 80px">
                Customer Id
            </th>
            <th scope="col" style="width: 150px">
                Customer Name
            </th>
            <th scope="col" style="width: 150px">
                Company Name
            </th>
        </tr>
        <asp:Repeater ID="rptCustomers" runat="server">
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("CustomerId") %>' />
                    </td>
                    <td>
                        <asp:Label ID="lblContactName" runat="server" Text='<%# Eval("ContactName") %>' />
                    </td>
                    <td>
                        <asp:Label ID="lblCompanyName" runat="server" Text='<%# Eval("CompanyName") %>' />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </table>
    <br />
    <asp:Repeater ID="rptPager" runat="server">
        <ItemTemplate>
            <asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>'
                CssClass='<%# Convert.ToBoolean(Eval("Enabled")) ? "page_enabled" : "page_disabled" %>'
                OnClick="Page_Changed" OnClientClick='<%# !Convert.ToBoolean(Eval("Enabled")) ? "return false;" : "" %>'></asp:LinkButton>
        </ItemTemplate>
    </asp:Repeater>
    </form>
</body>
</html>


.CS..............>>>

using System;
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;
using System.Collections.Generic;
using Microsoft.ApplicationBlocks.Data;


public partial class CS : System.Web.UI.Page
{
    private int PageSize = 10;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.GetCustomersPageWise(1);
        }
    }

    private void GetCustomersPageWise(int pageIndex)
    {
        string constring = ConfigurationManager.ConnectionStrings["Constring"].ConnectionString;
        SqlConnection con = new SqlConnection(constring);

        SqlParameter[] prm = new SqlParameter[4];
        prm[0] = new SqlParameter("@PageIndex", pageIndex);
        prm[1] = new SqlParameter("@PageSize", PageSize);
        prm[2] = new SqlParameter("@RecordCount", SqlDbType.Int,4);
        prm[3] = new SqlParameter("@ret", SqlDbType.Int);
        prm[3].Direction = ParameterDirection.ReturnValue;
      DataSet ds=  SqlHelper.ExecuteDataset(con, CommandType.StoredProcedure, "GetCustomersPageWise", prm);
        Int32 check = Convert.ToInt32(prm[3].Value);
       // return Convert.ToInt32(prm[3].Value);

     //   SqlCommand cmd = new SqlCommand("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();
            //    SqlDataReader idr = cmd.ExecuteReader();
              //  IDataReader idr = cmd.ExecuteReader();
                rptCustomers.DataSource = ds;
                rptCustomers.DataBind();
               
               // con.Close();
                int recordCount = Convert.ToInt32(check);
                this.PopulatePager(recordCount, pageIndex);
            
        
    }

    private void PopulatePager(int recordCount, int currentPage)
    {
        double dblPageCount = (double)((decimal)recordCount / Convert.ToDecimal(PageSize));
        int pageCount = (int)Math.Ceiling(dblPageCount);
        List<ListItem> pages = new List<ListItem>();
        if (pageCount > 0)
        {
            for (int i = 1; i <= pageCount; i++)
            {
                pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
            }
        }
        rptPager.DataSource = pages;
        rptPager.DataBind();
    }

    protected void Page_Changed(object sender, EventArgs e)
    {
        int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
        this.GetCustomersPageWise(pageIndex);
    }
}

Store Procedure .............>>>
GO
/****** Object:  StoredProcedure [dbo].[GetCustomersPageWise]    Script Date: 9/1/2014 12:51:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[GetCustomersPageWise] ---GetCustomersPageWise 1,3,3
      @PageIndex INT = 1
      ,@PageSize INT = 10
     ,@RecordCount INT OUTPUT
AS
BEGIN
      SET NOCOUNT ON;
      SELECT ROW_NUMBER() OVER
      (
            ORDER BY [CustomerID] ASC
      )AS RowNumber
      ,[CustomerID]
      ,[CompanyName]
      ,[ContactName]
     INTO #Results
      FROM [Customer_Details]
      SELECT @RecordCount = COUNT(*)
      FROM #Results
 --set @RecordCount=Count(*) from # Results
           
      SELECT * FROM #Results
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1
     --RETURN Count(*) #Results
      DROP TABLE #Results
 return @RecordCount
END

Table.....>>>
USE [test]
GO

/****** Object:  Table [dbo].[Customer_Details]    Script Date: 9/1/2014 12:52:29 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Customer_Details](
[CustomerID] [varchar](50) NULL,
[CompanyName] [varchar](100) NULL,
[ContactName] [varchar](100) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


No comments:

Post a Comment