代码改变世界

推荐AspNetPager分页控件

2010-07-13 15:45  老羽  阅读(319)  评论(0编辑  收藏  举报

前台代码:

html xmlns="http://www.w3.org/1999/xhtml" >
<
head runat="server">
<
title></title>
<
style>
/*拍拍网风格*/
/*.paginator { font: 11px Arial, Helvetica, sans-serif;padding:10px 20px 10px 0; margin: 0px;}
.paginator a {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;margin-right:2px}
.paginator a:visited {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;}
.paginator .cpb {padding: 1px 6px;font-weight: bold; font-size: 13px;border:none}
.paginator a:hover {color: #fff; background: #ffa501;border-color:#ffa501;text-decoration: none;}
*/
/*淘宝风格*/
.paginator { font: 12px Arial, Helvetica, sans-serif;padding:10px 20px 10px 0; margin: 0px;}
.paginator a {border:solid 1px #ccc;color:#0063dc;cursor:pointer;text-decoration:none;}
.paginator a:visited {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;}
.paginator .cpb {border:1px solid #F50;font-weight:700;color:#F50;background-color:#ffeee5;}
.paginator a:hover {border:solid 1px #F50;color:#f60;text-decoration:none;}
.paginator a,.paginator a:visited,.paginator .cpb,.paginator a:hover
{float:left;height:16px;line-height:16px;min-width:10px;_width:10px;margin-right:5px;text-align:center;
white-space:nowrap;font-size:12px;font-family:Arial,SimSun;padding:0 3px;}
</style>
</
head>
<
body>
<
form id="form1" runat="server">
<
div>
<
h3>分页测试</h3>
<
div>
<
asp:GridView ID="GridView1" runat="server">
</
asp:GridView>
</
div>
<
div>
<
webdiyer:AspNetPager ID="AspNetPager1" CssClass="paginator" CurrentPageButtonClass="cpb" runat="server" AlwaysShow="True"
FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PageSize="20" PrevPageText="上一页" ShowCustomInfoSection="Left"
ShowInputBox="Never" onpagechanged="AspNetPager1_PageChanged" CustomInfoTextAlign="Left" LayoutType="Table" >
</
webdiyer:AspNetPager>
</
div>
</
div>
</
form>
</
body>
</
html>

后台代码:(用到企业库)

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}

protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
BindData();

}

private void BindData()
{
string sql = @" BEGIN
DECLARE @START INT
DECLARE @END INT
DECLARE @row_number int
SET @START = (@current_page -1) * @page_size + 1
SET @END = @START+@page_size -1

SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY @row_number) AS row_number,* FROM t_um_ob) rp
WHERE row_number BETWEEN @START AND @END;
SELECT count(1) FROM t_um_ob
END"
;

Database db = DatabaseFactory.CreateDatabase("DB");
DbCommand cmd = db.GetSqlStringCommand(sql);
db.AddInParameter(cmd, "@current_page", System.Data.DbType.Int32, AspNetPager1.CurrentPageIndex);
db.AddInParameter(cmd, "@page_size", System.Data.DbType.Int32, AspNetPager1.PageSize);
DataSet ds = db.ExecuteDataSet(cmd);
AspNetPager1.RecordCount = (int)ds.Tables[1].Rows[0][0];
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();

感觉效果还不错,值得推荐