分页绑定repeater控件
BookList.aspx.cs Code
using System;
using System.Data;
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 MyBookShopModels;
using System.Collections.Generic;
using System.Data.SqlClient;
using MyBookShopDAL;
public partial class BookList1 : System.Web.UI.Page
{
PublisherService pub = new PublisherService();
CategoryService cate = new CategoryService();
int totalNumber = 0;//共多少条数共多少条数据据
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["currentPage"] = 1;//当前显示第一页
ViewState["totalPage"] = this.GetNum(3);//总页数
ViewState["order"] = "Id asc"; //按照id升序排序
ViewState["totalNumber"] = totalNumber;//表中总的条数
for (int i = 1; i <= Convert.ToInt32(ViewState["totalPage"]); i++)
{
this.DropDownList1.Items.Add(i.ToString());
}
this.MyDataBind();
}
}
public void MyDataBind()
{
int currentPage = Convert.ToInt32(ViewState["currentPage"]);
int totalPage = Convert.ToInt32(ViewState["totalPage"]);
int totalNumber = Convert.ToInt32(ViewState["totalNumber"]); //从viewstate中取出总的条数
string order = ViewState["order"].ToString();
if (currentPage == 1)
{
this.Button3.Enabled = false;
}
else
{
this.Button3.Enabled = true;
}
if (currentPage == totalPage)
{
this.Button4.Enabled = false;
}
else
{
this.Button4.Enabled = true;
}
this.Repeater1.DataSource = this.GetPageBooks(currentPage, 3, "Id asc");
this.Repeater1.DataBind();
this.lblInfo.Text = string.Format("当前是第{0}页 共{1}页 每页3条数据 共{2}条数据", currentPage.ToString(), totalPage.ToString(), totalNumber.ToString());
}
/// <summary>
///
/// </summary>
/// <param name="PageIndex">当前是第几页</param>
/// <param name="PageSize">每页显的条数</param>
/// <param name="Order">排序方式</param>
/// <returns></returns>
public List<Book> GetPageBooks(int PageIndex, int PageSize, string Order)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["CONN"]);
// string sql = "select top " + PageSize.ToString() + " * from Books where Id not in(select top " + PageSize * (PageIndex - 1) + " Id from Books order by " + Order + ") order by " + Order;
string sql = string.Format("select top {0} * from Books Where Id not in(select top {1} Id from Books order by {2})order by {3}", PageSize, PageSize * (PageIndex - 1), Order, Order);
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
List<Book> bookList = new List<Book>();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Book book = new Book();
book.Id = (int)reader["Id"];
book.Title = reader["Title"].ToString();
book.Author = reader["Author"].ToString();
book.Publishers = pub.GetPublisherById((int)reader["PublisherId"]);
book.PublishDate = Convert.ToDateTime(reader["PublishDate"]);
book.ISBN = reader["ISBN"].ToString();
book.WordsCount = (int)reader["WordsCount"];
book.Unitprice = Convert.ToDecimal(reader["UnitPrice"]);
book.ContentDescription = reader["ContentDescription"].ToString();
book.AurhorDescription = reader["AurhorDescription"].ToString();
book.EditorComment = reader["EditorComment"].ToString();
book.Categories = cate.GetCategoryByID((int)reader["CategoryId"]);
book.Clicks = (int)reader["Clicks"];
bookList.Add(book);
}
reader.Close();
}
con.Close();
return bookList;
}
/// <summary>
/// 查询总页数
/// </summary>
/// <param name="pageSize">每页显示的条数</param>
/// <returns></returns>
public int GetNum(int pageSize)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["CONN"]);
int allPage = 0; //总页数
string sql = "select count(*) from Books ";
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
totalNumber = Convert.ToInt32(reader[0]);
}
int temp = totalNumber % pageSize;//总条数莫上每页显示的条数
if (temp == 0)
{
allPage = totalNumber / pageSize;
}
else
{
allPage = totalNumber / pageSize + 1;
}
reader.Close();
con.Close();
return allPage;
}
/// <summary>
/// 排序
/// </summary>
/// <param name="sOrder"></param>
/// <returns></returns>
public List<Book> GetBooks(string sOrder)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["CONN"]);
string sql = "select top 3 * from Books";
if (sOrder == "")
{
sOrder = "Id asc";
}
sql += " order by " + sOrder;
List<Book> listBook = new List<Book>();
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Book book = new Book();
book.Id = (int)reader["Id"];
book.Title = reader["Title"].ToString();
book.Author = reader["Author"].ToString();
book.Publishers = pub.GetPublisherById((int)reader["PublisherId"]);
book.PublishDate = Convert.ToDateTime(reader["PublishDate"]);
book.ISBN = reader["ISBN"].ToString();
book.WordsCount = (int)reader["WordsCount"];
book.Unitprice = Convert.ToDecimal(reader["UnitPrice"]);
book.ContentDescription = reader["ContentDescription"].ToString();
book.AurhorDescription = reader["AurhorDescription"].ToString();
book.EditorComment = reader["EditorComment"].ToString();
book.Categories = cate.GetCategoryByID((int)reader["CategoryId"]);
book.Clicks = (int)reader["Clicks"];
listBook.Add(book);
}
reader.Close();
}
con.Close();
return listBook;
}
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button3_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32(ViewState["currentPage"]);
index--;
ViewState["currentPage"] = index;
this.MyDataBind();
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button4_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32(ViewState["currentPage"]);
index++;
ViewState["currentPage"] = index;
this.MyDataBind();
}
/// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button5_Click(object sender, EventArgs e)
{
ViewState["currentPage"] = 1;
this.MyDataBind();
}
/// <summary>
/// 尾页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button6_Click(object sender, EventArgs e)
{
ViewState["currentPage"] = ViewState["totalPage"];
this.MyDataBind();
}
/// <summary>
/// 选择DropDownList中的页码跳转到该页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ViewState["currentPage"] = this.DropDownList1.SelectedValue.ToString();
this.MyDataBind();
}
/// <summary>
/// 出版日期排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["currentPage"]=1;//点击排序按钮时跳到默认的第一页
ViewState["order"] = "PublishDate asc";//出版日期升序
this.MyDataBind();
}
/// <summary>
/// 价格排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
ViewState["currentPage"] = 1;//点击排序按钮时跳到默认的第一页
string order = ViewState["order"].ToString();
if (order != "UnitPrice asc")
{
ViewState["Order"] = "UnitPrice asc";
this.Button2.Text = "价格↑";
}
else
{
ViewState["Order"] = "UnitPrice desc";
this.Button2.Text = "价格↓";
}
MyDataBind();
}
/// <summary>
/// 获取书籍图片路径
/// </summary>
/// <param name="isbn"></param>
/// <returns></returns>
public string GetBookCover(string isbn)
{
string path = string.Empty;
string phyPath = string.Empty;
path = string.Format("~/images/BookCovers/{0}.jpg", isbn);
phyPath = Server.MapPath(path);
if (!System.IO.File.Exists(phyPath))
{
path = "~/images/default.jpg";
}
return path;
}
/// <summary>
/// 截取字符串
/// </summary>
/// <param name="strText"></param>
/// <param name="maxLength"></param>
/// <returns></returns>
public string FixString(string strText, int maxLength)
{
if (strText.Length > maxLength)
{
strText = strText.Substring(0, maxLength);
strText += "....";
}
return strText;
}
}