datalist 分页显示不用PagedDataSource对象
时间:2010-10-26 来源:gull
int total;
protected void Page_Load(object sender, EventArgs e)
{
string series, category;
if (!IsPostBack) {
string series1 = Series(out series);
string category1 = Category(out category);
string cseries = series1 + category1;
int pageSize = 3;//分页中一页的条数,可以用下拉框选择你喜欢的条数
string sql="";
int currentPage = 0;
if (HttpContext.Current.Request.QueryString["page"] != null)
{
currentPage = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"].ToString());//获得当前的页码
}
if (currentPage <=1)
{
sql = "select top " + pageSize + " * from T_product " + cseries; //第一页的绑定
}
else
{
sql = "SELECT TOP " + pageSize + " * FROM T_product " + cseries + " and (id NOT IN (SELECT TOP " + (currentPage-1)* pageSize + " id FROM T_product " + cseries + "))"; //后面的页的绑定
}
DataTable dt = me.readData(sql); // me 是读取数据库的一个类的实例
//我这里想不到好的方法,有没有可以不用读全部的数据,把数据都读出来
if (flag == 0) //第一次绑定时触发的
{
DataTable dt1 = me.readData("select * from T_product"); //读取全部的数据
total = dt1.Rows.Count; //读出全部的数据
flag = 1;
}
Label1.Text = GetPageNum(dt, DataList1, pageSize, series, category,total );
}
}
/// </summary>
/// <param name="ds">DataTable实例</param>
/// <param name="datalistname">DataList名称</param>
/// <param name="pagesize">分页大小</param>
public string GetPageNum(DataTable dt, DataList datalistname, int pagesize, string series,string category,total)
{
int total = total;
int page;
if (HttpContext.Current.Request.QueryString["page"] != null)
page = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]);
else
page = 1;
//objPds.CurrentPageIndex = page - 1;
datalistname.DataSource = dt.DefaultView;
datalistname.DataBind();
int allpage = 0;
int next = 0;
int pre = 0;
int startcount = 0;
int endcount = 0;
string pagestr = "";
if (page < 1) { page = 1; }
//计算总页数
if (pagesize != 0)
{
allpage = (total / pagesize);
allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
allpage = (allpage == 0 ? 1 : allpage);
}
next = page + 1;
pre = page - 1;
startcount = (page + 5) > allpage ? allpage - 9 : page - 4;//中间页起始序号
//中间页终止序号
endcount = page < 5 ? 10 : page + 5;
if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
if (allpage < endcount) { endcount = allpage; } //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
pagestr = "共" + allpage + "页" + total + "条 ";
pagestr += page > 1 ? "<a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=1&Series=" + series + "&Category=" + category + "\">首页</a> <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + pre + "&Series=" + series + "&Category=" + category+ "\">上一页</a>" : "首页 上一页"; //中间页处理,这个增加时间复杂度,减小空间复杂度 for (int i = startcount; i <= endcount; i++)
{
pagestr += page == i ? " <font color=\"#ff0000\">" + i + "</font>" : " <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + i + "&Series=" + series + "&Category=" + category + "\">" + i + "</a>"; }
pagestr += page != allpage ? " <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + next + "&Series=" + series + "&Category=" + category + "\">下一页</a> <a href=\"" + HttpContext.Current.Request.CurrentExecutionFilePath + "?page=" + allpage + "&Series=" + series + "&Category=" + category + "\">末页</a>" : " 下一页 末页";
return pagestr;
}