使用AspNetPager控件实现GridView分页

1 aspx文件代码 :
2
3 view plaincopy to clipboardprint?
4  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AspNetPager.aspx.cs" Inherits="AspNetPager" %>
5  <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
6 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
7 <html xmlns="http://www.w3.org/1999/xhtml">
8 <head runat="server">
9 <title>使用AspNetPager控件实现GridView分页</title>
10 </head>
11 <body>
12 <form id="form1" runat="server">
13 <div>
14 <asp:GridView ID="GridView1" runat="server" Style="width: 100%" mce_Style="width: 100%">
15 </asp:GridView>
16 <webdiyer:AspNetPager ID="AspNetPager1" runat="server" CustomInfoHTML="共%PageCount%页,当前为第%CurrentPageIndex%页,每页%PageSize%条"
17 FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PrevPageText="上一页"
18 ShowBoxThreshold="1" ShowCustomInfoSection="Left" Width="100%" OnPageChanging="AspNetPager1_PageChanging">
19 </webdiyer:AspNetPager>
20 </div>
21 </form>
22 </body>
23 </html>


view plaincopy to clipboardprint?
using System;
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.Data;
using System.Data.SqlClient;
public partial class AspNetPager : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex
= e.NewPageIndex;
BindData();
}
#region 绑定数据
protected void BindData()
{
string connStr = "server=.;database=***;uid=***;pwd=***";//设定数据库连接字符串
SqlConnection conn = new SqlConnection(connStr);//连接数据库
conn.Open();//打开数据库
string sql = "select * from 表名";
SqlCommand comm
= new SqlCommand(sql, conn);
SqlDataAdapter da
= new SqlDataAdapter(comm);
DataSet ds
= new DataSet();
da.Fill(ds,
"表名");
PagedDataSource pds
= new PagedDataSource();
pds.DataSource
= ds.Tables[0].DefaultView;//设置分页的数据源
pds.AllowPaging = true;//设置允许分页
AspNetPager1.RecordCount = pds.Count;//AspNetPager1.RecordCount = ds.Tables[0].DefaultView.Count;等价//获取数据的条数
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;//设置当前页的索引
pds.PageSize = AspNetPager1.PageSize;//设置每页显示的页数
this.GridView1.DataSource = pds;
this.GridView1.DataBind();//绑定数据
}
#endregion
}
posted @ 2011-04-28 13:48  xfyn  阅读(3842)  评论(0编辑  收藏  举报