分页排序等
View Code
分页 排序等using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class 分页_Default : System.Web.UI.Page { int pagesize = 10; protected void Page_Load(object sender, EventArgs e) { ViewState["pagesize"] = 10; pagesize = Convert.ToInt32(ViewState["pagesize"]); if (!IsPostBack) { this.BookListBinds(Convert.ToInt32(ViewState["pagesize"])); } } //当前页 protected int PageIndex { get { if (ViewState["PageIndex"] == null) { ViewState["PageIndex"]=0; } return (int)ViewState["PageIndex"]; } set { ViewState["PageIndex"] = value; } } protected void BookListBinds(int pagesize) { //创建分页类 PagedDataSource pds = new PagedDataSource(); //启用分页 pds.AllowPaging = true; //将数据绑定给分页 System.Data.DataView dv = DBHelper.GetDataTable("select * from Books").DefaultView; //设置排序 dv.Sort = Sort; pds.DataSource = dv; //设置当前页 pds.CurrentPageIndex = PageIndex; //设置一页显示多少行数据 pds.PageSize = pagesize; //保存当前总页数 ViewState["PageCount"] = pds.PageCount; //将分页后的数据绑定到Repeater Repeater1.DataSource = pds; Repeater1.DataBind(); //绑定当前页和总页数 l1.Text = (PageIndex + 1).ToString(); l2.Text = pds.PageCount.ToString(); //启用按钮 this.btnEnable(pds); } //禁用按钮方法 protected void btnEnable(PagedDataSource pds) { btnF.Enabled = true; btnU.Enabled = true; btnN.Enabled = true; btnL.Enabled = true; //判断是否是首页 if (pds.IsFirstPage) { btnF.Enabled = false; btnU.Enabled = false; } if (pds.IsLastPage) { btnN.Enabled = false; btnL.Enabled = false; } } //分页按钮 protected void PageCommand(object sender, CommandEventArgs e) { if (e.CommandName=="btnF") { PageIndex =0; } else if (e.CommandName == "btnU") { PageIndex--; } else if (e.CommandName=="btnN") { PageIndex++; } else if (e.CommandName=="btnL") { PageIndex = Convert.ToInt32(ViewState["PageCount"]) - 1; } //重新绑定数据 this.BookListBinds(Convert.ToInt32(ViewState["pagesize"])); } //按几行排列 protected void pagesizeBind(object sender, CommandEventArgs e) { if (e.CommandName=="15") { ViewState["pagesize"] = 15; } else if (e.CommandName == "20") { ViewState["pagesize"] = 20; } else if (e.CommandName == "50") { ViewState["pagesize"] = 50; } else { ViewState["pagesize"] = 10; } this.BookListBinds(Convert.ToInt32(ViewState["pagesize"])); } protected string Sort { get { if (ViewState["Sort"]==null) { ViewState["Sort"] = "ID"; } return ViewState["Sort"].ToString(); } set { ViewState["Sort"] = value; } } //按时间排序 protected void btnTime_Click(object sender, EventArgs e) { Orientation("PublishDate"); this.BookListBinds(Convert.ToInt32(ViewState["pagesize"])); } //按单价排序 protected void PriceBtn_Click(object sender, EventArgs e) { Orientation("UnitPrice"); this.BookListBinds(Convert.ToInt32(ViewState["pagesize"])); } //排序方向 protected void Orientation(String columns) { if (Sort == (columns + "asc")) { Sort = columns + "desc"; } else { Sort = columns + " asc"; } } } ___________________________________________- public class StringHandler { //获取ISBN图书图片 public static string CoverURL(object ISBN) { return "BooKCovers/" + ISBN.ToString().Trim() + ".jpg"; } //详情页面内容过多时 public static String CutString(object content, int num) { if (content.ToString().Length>num) { return content.ToString().Substring(0, num)+"...."; } return content.ToString(); }