不用DataGrid控件分页功能,而且当前页显示粗体字,贴出全部代码。
private void RptDataBinder()
{
//建立数据库连接
string dbconn = ConfigurationSettings.AppSettings["dbconn"] + Server.MapPath(ConfigurationSettings.AppSettings["dbpath"]);
OleDbConnection conn = new OleDbConnection(dbconn);
conn.Open();
//读取数据
OleDbCommand cmd = new OleDbCommand("select * from download order by did desc", conn);
OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
oda.Fill(ds);
//利用分页控件 PageDataSource
int curPage = Convert.ToInt32(Request.QueryString["page"]); //获取当前页数
int AllPage; //定义页面
System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();
if (curPage <= 0)
{
curPage = 1;
}
RecordCount = ds.Tables[0].Rows.Count;
ps.DataSource = ds.Tables[0].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 6;
AllPage = ps.PageCount;
if (curPage > AllPage)
{
curPage = 1;
}
ps.CurrentPageIndex = curPage - 1;
this.Rptdownload.DataSource = ps;
this.Rptdownload.DataBind();
for (int i = 1; i <= ps.PageCount; i++)
{
string pi;
if (curPage == i)
{
pi = "<font color=#000000><b>" + i + "</b></font>";
}
else
{
pi = i.ToString();
}
this.PageC = this.PageC + "<font color=#333333><a href=download.aspx?page=" + i + ">" + pi + "</a></font> ";
}
conn.Close();
}
然后在Page_Load 中调用此方法。
this.RptDataBinder();
private void RptDataBinder()
{
//建立数据库连接
string dbconn = ConfigurationSettings.AppSettings["dbconn"] + Server.MapPath(ConfigurationSettings.AppSettings["dbpath"]);
OleDbConnection conn = new OleDbConnection(dbconn);
conn.Open();
//读取数据
OleDbCommand cmd = new OleDbCommand("select * from download order by did desc", conn);
OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
oda.Fill(ds);
//利用分页控件 PageDataSource
int curPage = Convert.ToInt32(Request.QueryString["page"]); //获取当前页数
int AllPage; //定义页面
System.Web.UI.WebControls.PagedDataSource ps = new PagedDataSource();
if (curPage <= 0)
{
curPage = 1;
}
RecordCount = ds.Tables[0].Rows.Count;
ps.DataSource = ds.Tables[0].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 6;
AllPage = ps.PageCount;
if (curPage > AllPage)
{
curPage = 1;
}
ps.CurrentPageIndex = curPage - 1;
this.Rptdownload.DataSource = ps;
this.Rptdownload.DataBind();
for (int i = 1; i <= ps.PageCount; i++)
{
string pi;
if (curPage == i)
{
pi = "<font color=#000000><b>" + i + "</b></font>";
}
else
{
pi = i.ToString();
}
this.PageC = this.PageC + "<font color=#333333><a href=download.aspx?page=" + i + ">" + pi + "</a></font> ";
}
conn.Close();
}
然后在Page_Load 中调用此方法。
this.RptDataBinder();