<%@ Control Language=C# AutoEventWireup=true CodeFile=paging1.ascx.cs Inherits=Controls_Paging_paging1 %>
<div class=”PagingStyle”>
<table border=”0″ cellpadding=”0″ cellspacing=”0″ width=”100%”>
<tr>
<td align=”left”>
<asp:Label ID=”lblPageOf” CssClass=”PageOf” meta:resourceKey=”lblPageOf” runat=”Server”>หน้าที่ {0} ของ {1}</asp:Label>
</td>
<td align=”right”>
<asp:LinkButton ID=”lnkFirst” CssClass=”FirstPage” Text=”lnkFirst หน้าแรก” runat=”Server” meta:resourceKey=”lnkFirst” OnClick=”lnkFirst_Click” CausesValidation=”False”></asp:LinkButton> 
<asp:LinkButton ID=”lnkPrevious” CssClass=”PreviousPage” Text=”lnkPrevious หน้าที่แล้ว” runat=”Server” meta:resourceKey=”lnkPrevious” OnClick=”lnkPrevious_Click” CausesValidation=”False”></asp:LinkButton> 
<asp:Repeater ID=”rptPageNumbers” runat=”server” OnItemCommand=”rptPageNumbers_ItemCommand” OnItemDataBound=”rptPageNumbers_ItemDataBound”>
<ItemTemplate>
<asp:LinkButton ID=”lnkPageNumber” CssClass=”PageNumber” CommandName=”Selected” runat=”Server” CausesValidation=”False”></asp:LinkButton>
</ItemTemplate>
</asp:Repeater> 
<asp:LinkButton ID=”lnkNext” CssClass=”NextPage” Text=”lnkNext” runat=”Server” meta:resourceKey=”lnkNext” OnClick=”lnkNext_Click” CausesValidation=”False”></asp:LinkButton> 
<asp:LinkButton ID=”lnkLast” CssClass=”LastPage” Text=”lnkLast” runat=”Server” meta:resourceKey=”lnkLast” OnClick=”lnkLast_Click” CausesValidation=”False”></asp:LinkButton> 
</td>
</tr>
</table>
</div>


 


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;[ToolboxData(<{0}:PagingControl runat=server></{0}:PagingControl>)]
 
public partial class Controls_Paging_paging1 : System.Web.UI.UserControl
  {
 
#region Private methods section
 
private void SetPageController()
  {
 
this.lnkFirst.Enabled = true;
 
this.lnkPrevious.Enabled = true;
 
this.lnkNext.Enabled = true;
 
this.lnkLast.Enabled = true;if (CurrentPage == 1)
  {
 
this.lnkPrevious.Enabled = false;
 
this.lnkFirst.Enabled = false;
  }
 
if (CurrentPage == MaximumPage)
  {
 
this.lnkNext.Enabled = false;
 
this.lnkLast.Enabled = false;
  }
  }

private void ShowPageOf()
  {
 
if (ViewState[pageOfFormat] != null)
  {
 
this.lblPageOf.Text = string.Format(ViewState[pageOfFormat].ToString(), CurrentPage.ToString(), MaximumPage.ToString());
  }
  }
private void BindPageNumber()
  {

if (TotalRecords == 0)
  {
 
this.rptPageNumbers.DataSource = null;
  }
 
else
  {
 
int startPage = 1;
 
if (CurrentPage > 6 && MaximumPage > 10)
  {
  startPage
= CurrentPage 5;
  }
 
int[] pageNumbers;
 
if (MaximumPage < 10)
  {
  pageNumbers
= new int[MaximumPage];
 
for (int i = 0; i < MaximumPage; i++)
  {
  pageNumbers[i]
= startPage + i;
  }
  }
 
else
  {
  pageNumbers
= new int[10];
 
if ((startPage + 10) > MaximumPage)
  {
  startPage
= MaximumPage 9;
  }
for (int i = 0; i < 10; i++)
  {
  pageNumbers[i]
= startPage + i;
  }
  }

this.rptPageNumbers.DataSource = pageNumbers;
  }
 
this.rptPageNumbers.DataBind();
  }
#endregion

#region Protected events section
 
protected void Page_Load(object sender, EventArgs e)
  {
 
if (!Page.IsPostBack)
  {
  ViewState[
pageOfFormat] = lblPageOf.Text;
  ShowPageOf();
  BindPageNumber();
  SetPageController();
  }
  }
protected virtual void OnPageIndexChanged(EventArgs e)
  {
 
if (PageIndexChanged != null) PageIndexChanged(this, e);
  }

protected void rptPageNumbers_ItemCommand(object source, RepeaterCommandEventArgs e)
  {
 
if (e.CommandName == Selected)
  {
  CurrentPage
= int.Parse(e.CommandArgument.ToString());
  ViewState[
CurrentPage] = CurrentPage.ToString();
  DataBind();
  OnPageIndexChanged(e);
  }
  }
protected void rptPageNumbers_ItemDataBound(object source, RepeaterItemEventArgs e)
  {
  LinkButton lnkPageNumber
= (LinkButton)e.Item.FindControl(lnkPageNumber);
 
if (lnkPageNumber != null)
  {
 
if (e.Item.DataItem.ToString() == CurrentPage.ToString())
  {
  lnkPageNumber.Text
= [ + e.Item.DataItem.ToString() + ];
  lnkPageNumber.CommandArgument
= e.Item.DataItem.ToString();
  lnkPageNumber.Enabled
= false;
  }
 
else
  {
  lnkPageNumber.Text
= e.Item.DataItem.ToString();
  lnkPageNumber.CommandArgument
= e.Item.DataItem.ToString();
  }
  }
  }

protected void lnkFirst_Click(object sender, EventArgs e)
  {
  CurrentPage
= 1;
  OnPageIndexChanged(e);
  }
protected void lnkPrevious_Click(object sender, EventArgs e)
  {
  CurrentPage
= CurrentPage 1;
  OnPageIndexChanged(e);
  }

protected void lnkNext_Click(object sender, EventArgs e)
  {
  CurrentPage
= CurrentPage + 1;
  OnPageIndexChanged(e);
  }
 
protected void lnkLast_Click(object sender, EventArgs e)
  {
  CurrentPage
= MaximumPage;
  OnPageIndexChanged(e);
  }
#endregion

#region Public properties section
  [Bindable(
true), Category(Page), DefaultValue(0)]
 
public int TotalRecords
  {
 
get { return (ViewState[TotalRecord]==null)?0:int.Parse(ViewState[TotalRecord].ToString()); }
 
set { ViewState[TotalRecord] = value; }
  }
[Bindable(true), Category(Page), DefaultValue(10)]
 
public int PageSize
  {
 
get { return (ViewState[PageSize] == null) ? 0 : int.Parse(ViewState[PageSize].ToString()); }
 
set { ViewState[PageSize] = value; }
  }[Bindable(

true), Category(Page), DefaultValue(1)]
 
public int CurrentPage
  {
 
get { return (ViewState[CurrentPage] == null) ? 0 : int.Parse(ViewState[CurrentPage].ToString()); ; }
 
set { ViewState[CurrentPage] = value; }
  }
[Bindable(true), Category(Page), DefaultValue(1)]
 
public int MaximumPage
  {
 
get
  {
 
if (ViewState[TotalRecord] == null) return 0;
 
if (ViewState[PageSize] == null ||
— store procedure

CREATE PROCEDURE GETVENDOR(@Page int,@RecsPerPage int)

AS
begin

 set nocount on

declare @FirstRec int, @LastRec int

–set @Page = 4
–set @RecsPerPage = 3

CREATE TABLE #TempTable1
(
 ID int IDENTITY,
 Comp_Code varchar(3),
 Org_id varchar(8)
)

INSERT INTO #TempTable1 (Comp_Code, Org_id)
SELECT Comp_Code, Org_id FROM WF_MST_VENDOR

SELECT @FirstRec = (@Page – 1) * @RecsPerPage
SELECT @LastRec = (@Page * @RecsPerPage + 1)

— Table[0]

SELECT *,MoreRecords = ( SELECT COUNT(*)  FROM #TempTable1 TI  WHERE TI.ID >= @LastRec)
FROM #TempTable1
WHERE ID > @FirstRec AND ID < @LastRec

— Table[1]

SELECT count(*) from  WF_MST_VENDOR

drop TABLE #TempTable1

set nocount off

end
GO