ASPX页面的代码:
- C# code
<div class="lst_box" id="divNewsList" runat="server"><!--文章列表--></div><div class="page"><a href="/NewsList_1">第一页</a><span class="NewsList_2">2</span><a href="NewsList_3">3</a>/div>
.CS的代码
- C# code
void bindData(string strSortID,string strNewsTitle) { if (ViewState["Sort"] == null) ViewState["Sort"] = "AddDate DESC"; int TotalPage = 0; int TotalRecord = 0; int PageSize = 12; int CurrentPage = 1; //#region 循环分类 DataTable dtBizClass = FrontPageBiz.GetPageNewsListByConditions(1,NewsSortID,SearchKeyWord, PageSize, ref CurrentPage, out TotalPage, out TotalRecord, (string)ViewState["Sort"]); for (int i = 1; i < dtBizClass.Rows.Count; i++) { divNewsList.Controls.Add(new LiteralControl("<div class=\"lstbox_m\"><h3><a href=\"/html/" + dtBizClass.Rows[i]["NewsID"].ToString() + "\">" + dtBizClass.Rows[i]["NewsTitle"].ToString() + "</a></h3><p class=\"more_big\">日期:" + string.Format("{0:d}", dtBizClass.Rows[i]["AddDate"]) + " 关注:100 <a href=\"#\">咨询专家</a></p> <p class=\"txt\">" + dtBizClass.Rows[i]["Descriptions"].ToString() + "</p></div>")); }
SQL:
- SQL code
ALTER PROCEDURE [dbo].[XMLPageNewsListByCondition] ( @vType int, @SortID varchar(12), @NewsTitle varchar(120), @PageSize int, @CurrentPage int output, @TotalPage int output, @TotalRecord int output, @SortBy varchar(100) )AS
因为小弟以前一直是用Datalist和GridView+AspNetPager控件做后台分页,并带有搜索功能,现在想尝试下用div和css输出前台纯html的分页信息,但是分页做不出来,想请哪位大侠帮忙看看
------解决方案--------------------------------------------------------
看实际情况而定
看你的代码想要静态页分页,又像url重写。
前者比较纯粹,其实就是拼接字符串输出静态文件
后者,如果要纯粹,不用控件也是拼接字符串输出
附常规分页一份
- C# code
//-----分页所使用的变量 private int num = 148; //在页面显示几条信息 private int pageSize = 5; protected void Page_Load(object sender, EventArgs e) { //首次加载 if (!IsPostBack) { DlBooksPage(); } } /// <summary> /// 分页 /// </summary> /// <param name="CurrentPage"></param> private void DataListBind(int CurrentPage) { int pageCount = this.BookNum(); if (CurrentPage < 1) { CurrentPage = 1; } if (CurrentPage > pageCount) { CurrentPage = pageCount; } if (CurrentPage == 1) { this.btnPre.Enabled = false; } if (CurrentPage == pageCount) { this.btnNext.Enabled = false; } this.lblCurrentPage.Text = CurrentPage.ToString(); this.lblPageCount.Text = pageCount.ToString(); //DataSourceID 和 DataSource 不能并存,如果要 rptSearch.DataSource 起作用必须把 DataSourceID 赋为空 if (dlBooks.DataSourceID != null) { dlBooks.DataSourceID = null; } //调用方法 dlBooks.DataSource = BookManager.GetBooksByPageSize(this.GetKey(), pageSize, CurrentPage, this.GetOrder(), this.GetDesc()); //重新绑定数据源 dlBooks.DataBind(); } /// <summary> /// 查询图书的总数 /// </summary> /// <returns></returns> public int BookNum() { int booksNum = BookManager.GetAllBooksTotal(); int pageCount = 0; if (booksNum % pageSize == 0) { pageCount = booksNum / pageSize; } else { pageCount = booksNum / pageSize + 1; } return pageCount; } public void DlBooksPage() { this.DataListBind(this.ReturnCurrentPage()); } //当前页 public int ReturnCurrentPage() { int currentPage = 1; if (Request.QueryString["Page"] != null) { try { currentPage = Convert.ToInt32(Request.QueryString["page"]); } catch { } } if (currentPage < 1) { currentPage = 1; } if (currentPage > this.BookNum()) { currentPage = this.BookNum(); } return currentPage; } //截取字符串,多余内容以“......”显示在页面 public string FormatString(string message) { if (message.Length > num) { return message.Substring(0, num) + "..."; } else { return message; } } //上一页 protected void btnPre_Click1(object sender, EventArgs e) { Response.Redirect(Request.Url.AbsolutePath + "?page=" + (this.ReturnCurrentPage() - 1) + "&order=" + this.GetOrderString() + "&Desc=" + this.GetDescString() + "&key=" + this.GetKey()); } //下一页 protected void btnNext_Click(object sender, EventArgs e) { Response.Redirect(Request.Url.AbsolutePath + "?page=" + (this.ReturnCurrentPage() + 1) + "&order=" + this.GetOrderString() + "&Desc=" + this.GetDescString() + "&key=" + this.GetKey()); } //按出版日期排序 protected void btnDate_Click(object sender, EventArgs e) { //DataSourceID 和 DataSource 不能并存,如果要 rptSearch.DataSource 起作用必须把 DataSourceID 赋为空 if (dlBooks.DataSourceID != null) { dlBooks.DataSourceID = null; } if (this.GetDescString() == "0") { Response.Redirect(Request.Url.AbsolutePath + "?page=" + this.ReturnCurrentPage() + "&order=2&Desc=1"); } else { Response.Redirect(Request.Url.AbsolutePath + "?page=" + this.ReturnCurrentPage() + "&order=2&Desc=0"); } } //按价格排序 protected void btnPrice_Click(object sender, EventArgs e) { //DataSourceID 和 DataSource 不能并存,如果要 rptSearch.DataSource 起作用必须把 DataSourceID 赋为空 if (dlBooks.DataSourceID != null) { dlBooks.DataSourceID = null; } if (this.GetDescString() == "0") { Response.Redirect(Request.Url.AbsolutePath + "?page=" + this.ReturnCurrentPage() + "&order=1&Desc=1"); } else { Response.Redirect(Request.Url.AbsolutePath + "?page=" + this.ReturnCurrentPage() + "&order=1&Desc=0"); } this.DataBind(); } public string GetOrder() { int num = 0; if (Request.QueryString["Order"] != null) { try { num = Convert.ToInt32(Request.QueryString["Order"]); } catch { } } switch (num) { case 0: return "id"; break; case 1: return "UnitPrice"; break; case 2: return "PublishDate"; break; default: return "id"; } } public string GetOrderString() { int num = 0; if (Request.QueryString["Order"] != null) { try { num = Convert.ToInt32(Request.QueryString["Order"]); } catch { } } switch (num) { case 0: return "0"; break; case 1: return "1"; break; case 2: return "2"; break; default: return "0"; } } //获取地址栏 此方法提供给数据库 public string GetDesc() { int num = 0; if (Request.QueryString["Desc"] != null) { try { num = Convert.ToInt32(Request.QueryString["Desc"]); } catch { } } if (num == 0) { return "asc"; } else { return "desc"; } } //此方法提供给地址栏 public string GetDescString() { int num = 0; if (Request.QueryString["Desc"] != null) { try { num = Convert.ToInt32(Request.QueryString["Desc"]); } catch { } } if (num == 0) { return "0"; } else { return "1"; } } public string GetKey() { if (Request.QueryString["key"] != null) { return Request.QueryString["Key"]; } return ""; }