分页控件

        虽然微软提供了一个DataGrid的翻页功能,除了其外观不容易被编辑外,起性能上的开销也是很大的,比如说数据库里有几十万条数据,每页只要求显示10条记录,那每一次翻页的时候我们完全没有必要全部读出来,然后再将10条记录取出显示出来。
        以下是我自己做的一个翻页控件,它是结合存储过程实现的:
存储过程:
None.gif------------------------------------
None.gif--
用途:分页存储过程(对有主键的表效率极高)  
None.gif--
说明:
None.gif--
修改:继荣
None.gif--
修改时间:2006-03-08
None.gif--
修改说明:当@IsReCount非0时同时返回两个表,第一个为查询出的结果,第二个为总记录数
None.gif--
----------------------------------
None.gif

None.gif
CREATE PROCEDURE UP_GetRecordByPage
None.gif    
@tblName      varchar(255),       -- 表名
None.gif
    @fldName      varchar(255),       -- 主键字段名
None.gif
    @PageSize     int = 10,           -- 页尺寸
None.gif
    @PageIndex    int = 1,            -- 页码
None.gif
    @IsReCount    bit = 0,            -- 返回记录总数, 非 0 值则返回
None.gif
    @OrderType    bit = 0,            -- 设置排序类型, 非 0 值则降序
None.gif
    @strWhere     varchar(1000= ''  -- 查询条件 (注意: 不要加 where)
None.gif
AS
None.gif
None.gif
declare @strSQL   varchar(6000)       -- 主语句
None.gif
declare @strTmp   varchar(100)        -- 临时变量
None.gif
declare @strOrder varchar(400)        -- 排序类型
None.gif

None.gif
if @OrderType != 0
None.gif
begin
None.gif    
set @strTmp = '<(select min'
None.gif    
set @strOrder = ' order by [' + @fldName +'] desc'
None.gif
end
None.gif
else
None.gif
begin
None.gif    
set @strTmp = '>(select max'
None.gif    
set @strOrder = ' order by [' + @fldName +'] asc'
None.gif
end
None.gif
None.gif
set @strSQL = 'select top ' + str(@PageSize+ ' * from ['
None.gif    
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
None.gif    
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize+ ' ['
None.gif    
+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'
None.gif    
+ @strOrder
None.gif
None.gif
if @strWhere != ''
None.gif    
set @strSQL = 'select top ' + str(@PageSize+ ' * from ['
None.gif        
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
None.gif        
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize+ ' ['
None.gif        
+ @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
None.gif        
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
None.gif
None.gif
if @PageIndex = 1
None.gif
begin
None.gif    
set @strTmp =''
None.gif    
if @strWhere != ''
None.gif        
set @strTmp = ' where ' + @strWhere
None.gif
None.gif    
set @strSQL = 'select top ' + str(@PageSize+ ' * from ['
None.gif        
+ @tblName + ']' + @strTmp + ' ' + @strOrder
None.gif
end
None.gif
None.gif
if @IsReCount != 0
None.gif    
set @strSQL = @strSQL + ' select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhere
None.gif
None.gif
exec (@strSQL)
None.gif
GO
None.gif

页面:
ExpandedBlockStart.gifContractedBlock.gif<%dot.gif@ Control Language="c#" AutoEventWireup="false" Codebehind="PageLetNew.ascx.cs" Inherits="QuMeiXJ.Webs.Controls.PageLetNew" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
None.gif当前是第
<asp:label id="lblPageIndex" runat="server">1</asp:label>&nbsp; 共
None.gif
<asp:label id="lblPageCount" runat="server"></asp:label>
None.gif
<asp:LinkButton id="lbtnFirst" runat="server">首页</asp:LinkButton>
None.gif
<asp:LinkButton id="lbtnPrev" runat="server">上一页</asp:LinkButton>
None.gif
<asp:LinkButton id="lbtnNext" runat="server">下一页</asp:LinkButton>
None.gif
<asp:LinkButton id="lbtnLast" runat="server">末页</asp:LinkButton>&gt;|&nbsp;跳到&nbsp;
None.gif
<asp:dropdownlist id="ddlPageSelect" runat="server" AutoPostBack="True"></asp:dropdownlist>
None.gif

代码:
None.gifnamespace QuMeiXJ.Webs.Controls
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
using System;
InBlock.gif    
using System.Data;
InBlock.gif    
using System.Drawing;
InBlock.gif    
using System.Web;
InBlock.gif    
using System.Web.UI.WebControls;
InBlock.gif    
using System.Web.UI.HtmlControls;
InBlock.gif    
using System.Web.UI;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
///    作    者:继荣
InBlock.gif    
///    创建时间:2006-04-12
InBlock.gif    
///    用    途:翻页控件
InBlock.gif    
///    使用方法:
InBlock.gif    
///    <1>翻页控件中的事件说明
InBlock.gif    
///    PageLetNew1.PageIndexChanged += new QuMeiXJ.Webs.Controls.PageLetNew.PageIndexChangedEventHandler(this.PageButtonClick); 
InBlock.gif    
///    <2>翻页处理
InBlock.gif    
///        private void PageButtonClick(Object sender,System.EventArgs e)
InBlock.gif    
///        {
InBlock.gif    
///            dgBind();
InBlock.gif    
///        }
InBlock.gif    
///    <3>DataGrid绑定 和翻页控件设定
InBlock.gif    
///        private void dgdBind()
InBlock.gif    
///        {
InBlock.gif    
///            QuMeiXJ.BLL .pageTable bd = new QuMeiXJ.BLL.pageTable ();
InBlock.gif    
///            DataSet ds = bd.GetList(3,Convert.ToInt32(PageLetNew1.PageIndex),"1=1");
InBlock.gif    
///            dgdpage.DataSource = ds;
InBlock.gif    
///            dgdpage.DataBind ();
InBlock.gif    
///            PageLetNew1.PageSize = 3;
InBlock.gif    
///            PageLetNew1.ItemCount = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
InBlock.gif    
///            PageLetNew1.SetPageUrl();
InBlock.gif    
///        }
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class PageLetNew : System.Web.UI.UserControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.Label lblPageIndex;
InBlock.gif        
protected System.Web.UI.WebControls.Label lblPageCount;
InBlock.gif        
protected System.Web.UI.WebControls.DropDownList ddlPageSelect;
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton lbtnFirst;
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton lbtnPrev;
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton lbtnNext;
InBlock.gif        
protected System.Web.UI.WebControls.LinkButton lbtnLast;
InBlock.gif
InBlock.gif        
//声明翻页事件
InBlock.gif
        public delegate void PageIndexChangedEventHandler(object sender, System.EventArgs e);
InBlock.gif        
public event PageIndexChangedEventHandler PageIndexChanged;
InBlock.gif
InBlock.gif        
private int _itemCount;
InBlock.gif        
private int _pageCount;
InBlock.gif        
private int _pageSize=20;
InBlock.gif        
InBlock.gif
InBlock.gif        
protected virtual void OnPageIndexChanged(object sender,System.EventArgs  e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strNewPageIndex = "1";
InBlock.gif            Control ctrl 
= (Control)sender;
InBlock.gif            
if(ctrl.GetType().Name == "DropDownList")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strNewPageIndex 
= ddlPageSelect.SelectedValue;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if(ctrl.GetType().Name == "LinkButton")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strNewPageIndex 
= ((LinkButton)sender).CommandName;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if(PageIndex != strNewPageIndex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PageIndex 
= strNewPageIndex;
InBlock.gif                
if (PageIndexChanged != null)
InBlock.gif                    PageIndexChanged(sender, e);
ExpandedSubBlockEnd.gif            }

InBlock.gif            ddlPageSelect.SelectedValue 
= strNewPageIndex;
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 当前页数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string PageIndex
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lblPageIndex.Text 
= value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return lblPageIndex.Text;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 记录总数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int ItemCount
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _itemCount 
= value;
InBlock.gif                _pageCount 
= GetPageCount(_pageSize,_itemCount);
InBlock.gif                lblPageCount.Text 
= _pageCount.ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 每页显示记录数
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int PageSize
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _pageSize 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 得到总页数
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="PageSize">每页显示数据个数</param>
InBlock.gif        
/// <param name="ItemCount">数据总个数</param>
ExpandedSubBlockEnd.gif        
/// <returns>总夜数</returns>

InBlock.gif        private int GetPageCount(int PageSize,int ItemCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (ItemCount == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return 1;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(ItemCount % PageSize ==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return ItemCount / PageSize;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return ItemCount / PageSize + 1;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void SetPageUrl()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            lbtnFirst.CommandName 
= "1";
InBlock.gif            lbtnLast.CommandName 
= _pageCount.ToString();
InBlock.gif
InBlock.gif            
int pagePrev = Convert.ToInt32(lblPageIndex.Text) - 1;
InBlock.gif            
int pageNext = Convert.ToInt32(lblPageIndex.Text) + 1;
InBlock.gif            
if (pagePrev > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lbtnPrev.CommandName 
= pagePrev.ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lbtnPrev.CommandName 
= "1";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (pageNext > _pageCount)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lbtnNext.CommandName 
= _pageCount.ToString();
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                lbtnNext.CommandName 
= pageNext.ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            ddlPageSelect.Items.Clear();
InBlock.gif            
for(int i = 1; i < _pageCount+1; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ddlPageSelect.Items.Add(
new ListItem(i.ToString(),i.ToString()));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///        设计器支持所需的方法 - 不要使用代码编辑器
InBlock.gif        
///        修改此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.lbtnFirst.Click += new System.EventHandler(this.lbtnFirst_Click);
InBlock.gif            
this.lbtnPrev.Click += new System.EventHandler(this.lbtnPrev_Click);
InBlock.gif            
this.lbtnNext.Click += new System.EventHandler(this.lbtnNext_Click);
InBlock.gif            
this.lbtnLast.Click += new System.EventHandler(this.lbtnLast_Click);
InBlock.gif            
this.ddlPageSelect.SelectedIndexChanged += new System.EventHandler(this.ddlPageSelect_SelectedIndexChanged);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void lbtnFirst_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OnPageIndexChanged( sender , e );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void lbtnPrev_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OnPageIndexChanged( sender , e );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void lbtnNext_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OnPageIndexChanged( sender , e );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void lbtnLast_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OnPageIndexChanged( sender , e );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void ddlPageSelect_SelectedIndexChanged(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OnPageIndexChanged( sender , e );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/jirong/archive/2006/04/19/378803.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值