asp.net url分頁類代碼

來源:互聯網
上載者:User

複製代碼 代碼如下:using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;

/// <summary>
///CutPage 的摘要說明
/// </summary>
public class CutPage
{
public CutPage()
{
//
//TODO: 在此處添加建構函式邏輯
//
}
#region 私人成員變數
private string url; //分頁時所用到的頁面地址
private int count; //資料總條數
private int pageCount; //總頁數
private int curretPage; //當前頁數
private string id; //接收傳遞參數的值
private int startId; //資料迴圈的開始值
private int endId; //資料結束的值
private DataTable dt; //資料dt值
private int dataCount; //每頁現實的資料條數
private string cssUrl; //cssURL地址
#endregion
#region 公開變數
/// <summary>
/// Url地址
/// </summary>
public string Url
{
get
{
return url;
}
set
{
this.url = value;
}
}
/// <summary>
/// 資料總條數
/// </summary>
public int Count
{
get
{
return count;
}
set
{
this.count = value;
}
}
/// <summary>
/// 資料總頁數(該欄位唯讀)
/// </summary>
public int PageCount
{
get
{
if (count % dataCount == 0)
{
return Convert.ToInt32(count / dataCount);
}
else
{
return Convert.ToInt32(count / dataCount) + 1;
}
}
}
/// <summary>
/// 分頁樣式表url
/// </summary>
public string CssUrl
{
get { return cssUrl; }
set { this.cssUrl = value; }
}
/// <summary>
/// 當前頁數
/// </summary>
public int CurretPage
{
get { return this.curretPage; }
set { this.curretPage = value; }
}
/// <summary>
/// 傳遞的參數值
/// </summary>
public string ID
{
get { return this.id; }
set { this.id = value; }
}
/// <summary>
/// 資料開始值(該欄位唯讀)
/// </summary>
public int StartID
{
get
{
if (curretPage == 1)
{
return 0;
}
else
{
return (curretPage-1) * dataCount;
}
}
}
/// <summary>
/// 資料結束的值(該欄位唯讀)
/// </summary>
public int EndID
{
get
{
if (CurretPage == PageCount)
{
return this.DT.Rows.Count;
}
else
{
return (curretPage) * dataCount;
}
}
}
/// <summary>
/// 用於分頁的資料來源
/// </summary>
public DataTable DT
{
get { return this.dt; }
set { this.dt = value; }
}
/// <summary>
/// 每頁顯示的資料條數
/// </summary>
public int DataCount
{
get { return this.dataCount; }
set { this.dataCount = value; }
}
#endregion
/// <summary>
/// 分頁方法(產生分頁代碼的過程)
/// </summary>
/// <param name="PageInfo">Literal控制項 </param>
public void CutPageMethod(Literal pt)
{
StringBuilder orderInfoSb = new StringBuilder();
orderInfoSb.Append("<span style=\"width:1000px\"><tr id=\"pagination-digg\"><th style=\"width:180px\">");
orderInfoSb.Append("當前" + CurretPage + "/" + PageCount + "頁 共" + Count + "條資料");
orderInfoSb.Append("</th><th class=\"previous-off\" style=\"align:right\">");
if (Convert.ToInt32(this.ID) == 1)
{
orderInfoSb.Append("<a href='#' disabled='flase'>首頁</a>");
}
else
{
orderInfoSb.Append("<a href='" + Url + "?id=1'>首頁</a>");
}
orderInfoSb.Append("</th><th>");
if (Convert.ToInt32(this.ID )== 1 || this.ID==null || this.ID==string.Empty)
{
orderInfoSb.Append("<a href='#'disabled='flase'>上一頁</a>");
}
else
{
orderInfoSb.Append("<a href='" + Url + "?id="+Convert.ToString(CurretPage-1)+"'>上一頁</a>");
}
if (Convert.ToInt32(this.ID) < PageCount)
{
orderInfoSb.Append("<a href='" + Url + "?id=" +Convert.ToString(CurretPage + 1) + "'>下一頁</a>");

}
else
{
orderInfoSb.Append("<a href='#'disabled='flase'>下一頁</a>");
}
orderInfoSb.Append("</th><th>");
if (Convert.ToInt32(this.ID) == PageCount)
{
orderInfoSb.Append("<a href='#' disabled='flase'>末頁</a>");
}
else
{
orderInfoSb.Append("<a href='" + Url + "?id="+Convert.ToString(PageCount)+"'>末頁</a>");

}
orderInfoSb.Append("</th></tr></span>");
pt.Text = orderInfoSb.ToString();

}
}

樣式大家可以自己添,老實說沒什麼技術含量。
前台代碼:
代碼 複製代碼 代碼如下:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CutPageTest.aspx.cs" Inherits="CutPageTest" EnableViewState="false"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server" id="had">
<%--<link href="Style/Base.css" type="text/css" />--%>

<title>無標題頁</title>
<style type="text/css">
a
{
text-decoration: none;

}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Literal ID="LiInfo" runat="server"></asp:Literal>
<asp:Literal ID="lt" runat="server"></asp:Literal>
<a href="#"
</div>
</form>
</body>
</html>

後台代碼:
代碼 複製代碼 代碼如下:using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class CutPageTest : System.Web.UI.Page
{
CutPage cp=new CutPage();
string id;
protected void Page_Load(object sender, EventArgs e)
{
ShowPageData(DbHelperSQL.QueryReDt("select * from test",GlobalConfig.TCCLineDbHelper), "CutPageTest.aspx");
//Response.Write();

}
public void ShowPageData(DataTable dt,string url)
{
//cp.CssUrl = "Style/PageCut.css";
id = Request.QueryString["id"];
cp.ID = id;
cp.DT = dt;
had.InnerHtml = "<link href=\"css/text\" src='" + cp.CssUrl + "'/>";
if (id == null || id == "")
{
cp.CurretPage = 1;
}
else
{
cp.CurretPage = Convert.ToInt32(id);
}
cp.Url = url;
cp.DataCount = 2;
cp.Count = cp.DT.Rows.Count;
cp.CutPageMethod(lt);
for (int i = cp.StartID; i < cp.EndID; i++)
{
LiInfo.Text += cp.DT.Rows[i][1].ToString() + "<br/>";
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.