简单分页
如果需要考慮如時(shí)間的過(guò)濾、其他條件的加入,可以在SQL語(yǔ)句進(jìn)行編輯,普通的網(wǎng)站,下面的數(shù)據(jù)瀏覽分頁(yè)
就可以了。
aspx代碼:
<%@ Page language="c#" Codebehind="StockOrderFormBrower.aspx.cs" AutoEventWireup="false" Inherits="GSP.StockOrderFormBrower" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
?<HEAD>
??<title>
???用C#和SQL結(jié)合進(jìn)行數(shù)據(jù)瀏覽分頁(yè)
??</title>
??<LINK href="css/main.css" type="text/css" rel="stylesheet">
??<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
??<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
??<meta content="C#" name="CODE_LANGUAGE">
??<meta content="JavaScript" name="vs_defaultClientScript">
?</HEAD>
?
?<body MS_POSITIONING="GridLayout">
??<form id="form1" method="post" runat?="server">
???<table id="ShowData" cellSpacing="0" cellPadding="0" align="center" border="0">
????<%ShowData();%><!--輸出數(shù)據(jù)-->
???</table>
???<table align="right">
????<tr>
?????<td>
??????<%PageLoad_Count();%>
??????<INPUT id="first" type="button" value="? |<? " name="first" runat?="server"><!--第一頁(yè)-->
??????<INPUT id="prior" type="button" value="? <? " name="prior" runat?="server"><!--上一頁(yè)-->
??????<INPUT id="next" type="button" value="? >? " name="next" runat?="server"><!--下一頁(yè)-->
??????<INPUT id="last" type="button" value="? >|? " name="last" runat?="server"><!--最后一頁(yè)-->
?????</td>
????</tr>
???</table>
??</form>
?</body>
</HTML>
aspx.cs代碼:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace ASWBLM
{
?/// <summary>
?///?
?/// </summary>
?public class UnionInfo : System.Web.UI.Page
?{???????????????????????????????????
??protected System.Web.UI.HtmlControls.HtmlInputButton first;
??protected System.Web.UI.HtmlControls.HtmlInputButton prior;
??protected System.Web.UI.HtmlControls.HtmlInputButton last;
??protected System.Web.UI.HtmlControls.HtmlInputButton next;
??protected static int CurrentPage = 1;//初始化開(kāi)始頁(yè)面
??protected static int RowCount = 0 ;//本頁(yè)有多少條
??private static bool IsPrior = false;//有“前一頁(yè)”
??private static bool IsNext = false;//有“下一頁(yè)”
??private static bool IsLast = false;//有“最后一頁(yè)”
??protected static int not_shown_records=0;//計(jì)算未顯示記錄數(shù)
??private static string startID = "";//設(shè)置上一頁(yè)開(kāi)始ID
??private static string endID = "";//設(shè)置下一頁(yè)結(jié)束ID
??private static int page_count = 10;//初始化頁(yè)面記錄數(shù)
??private void Page_Load(object sender, System.EventArgs e)
??{???
???// 在此處放置用戶代碼以初始化頁(yè)面
???if (!IsPostBack)
???{?????????????
????this.CountRecord().ToString();// 記錄總數(shù)
????this.Page_Count().ToString();//分頁(yè)總數(shù)???
????Init_Brower();//初始化瀏覽
???}
??}
??#region Web 窗體設(shè)計(jì)器生成的代碼
??override protected void OnInit(EventArgs e)
??{
???//
???// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計(jì)器所必需的。
???//
???InitializeComponent();
???base.OnInit(e);
??}
??
??/// <summary>
??/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
??/// 此方法的內(nèi)容。
??/// </summary>
??private void InitializeComponent()
??{
???this.first.ServerClick += new System.EventHandler(this.first_ServerClick);
???this.prior.ServerClick += new System.EventHandler(this.prior_ServerClick);
???this.next.ServerClick += new System.EventHandler(this.next_ServerClick);
???this.last.ServerClick += new System.EventHandler(this.last_ServerClick);
???this.Load += new System.EventHandler(this.Page_Load);
??}
??#endregion????????????????
??
???????
??/// <summary>
??/// 顯示數(shù)據(jù)
??/// </summary>
??protected void ShowData()
??{
???DataSet ds = new DataSet();//數(shù)據(jù)集
???ASWBLM.Include.UnionInfo_Provider _uip = new ASWBLM.Include.UnionInfo_Provider();
???string vSQL = "";
???vSQL = GetSQLCommond(vSQL,startID,endID);
???
???ds = _uip.ShowAllUnionInfo(vSQL);//取得全部數(shù)據(jù)的數(shù)據(jù)集
???try
???{?
????Response.Write("<p align=center>");???
????foreach(DataRow dr in ds.Tables["Table"].Rows)
????{?? ????
?????Response.Write("<tr align = center onmouseover = /"this.bgColor = '#cccccc'/" onmouseout = /"this.bgColor='';/">");
?????
?????Response.Write("<td align=/"left/" width=/"60%/"><font color=/"#00309C/">");
?????Response.Write("<a href=/"UnionInfo_Read.aspx?id="+dr["Id"].ToString()+"/" target=/"_self/">");?????
?????Response.Write(dr["Title"].ToString());
?????Response.Write("</a>");
?????Response.Write("</td>");
?????Response.Write("<td align=/"right/">");
?????Response.Write("<font color=/"#999999/">");
?????Response.Write("( "+dr["SummaryDateTime"].ToString()+" )");
?????Response.Write("???? ( 已閱讀"+dr["ReadTimes"].ToString()+"次 )");
?????Response.Write("</font>");
?????Response.Write("</td>");
?????Response.Write("</tr>");
????}????????????????
????Response.Write("</p>");
????startID = ds.Tables["Table"].Rows[0].ItemArray[0].ToString();?????? //通過(guò)數(shù)組,取第一個(gè)數(shù)據(jù),得到開(kāi)始號(hào)“startID”
????RowCount = ds.Tables["Table"].DefaultView.Count;//得到表的行數(shù)
????endID = ds.Tables["Table"].Rows[RowCount-1].ItemArray[0].ToString();//通過(guò)數(shù)組,取最后一個(gè)數(shù)據(jù),得到結(jié)束號(hào)“endID”
???}
???catch(SqlException e)
???{?????
????Response.Write(e.Message);
???}
??}
??/// <summary>
??/// 計(jì)算未顯示記錄數(shù)
??/// </summary>
??/// <returns></returns>
??protected void NotShownRecords()
??{
???not_shown_records = this.CountRecord()/*查詢總記錄數(shù)*/ - (CurrentPage/*當(dāng)前頁(yè)*/ - 1) * page_count/*每頁(yè)記錄數(shù)*/;
??}
??/// <summary>
??/// 進(jìn)行輸出信息
??/// </summary>
??protected? void PageLoad_Count()
??{
???this.NotShownRecords();
???Response.Write("總共"+this.CountRecord()+"條記錄?????? ");
???Response.Write("共有"+this.Page_Count()+"頁(yè)?????? ");
???Response.Write("第"+CurrentPage.ToString()+"頁(yè)?????? ");
???Response.Write("本頁(yè)共有"+RowCount.ToString()+"條記錄?????? ");
??}
?
??/// <summary>
??/// 獲得總記錄總數(shù)
??/// </summary>
??/// <returns>時(shí)間條件范圍內(nèi)記錄總數(shù)intCount</returns>
??protected int CountRecord()
??{???
???int intCount = 0;
???SqlConnection SqlCon = new SqlConnection(Common._DBConnStr);
???SqlCon.Open ();
???
???//找到條件范圍內(nèi)的記錄總數(shù)
???string strCount = "select count(*) from UnionInfo";
???
???//找到符合條件的第一個(gè)記錄
???//string strNum = "select top 1 Id from UnionInfo";
???SqlCommand MyComm = new SqlCommand(strCount,SqlCon);
???SqlDataReader dr = MyComm.ExecuteReader();//讀取數(shù)據(jù)流
???if(dr.Read())
???{
?????????????? intCount = Int32.Parse(dr[0].ToString());
???}
???else
???{
????????????? intCount = 0;
???}
???dr.Close();
???SqlCon.Close();??????????????
???return intCount;
??}
??/// <summary>
??/// 總分頁(yè)數(shù)
??/// </summary>
??/// <returns>分頁(yè)總數(shù)</returns>
??protected int Page_Count()
??{
???int pageSum = 0;//分頁(yè)總數(shù)???
???pageSum = this.CountRecord() / page_count;?????????? ///記錄總數(shù)/分頁(yè)的頁(yè)數(shù)
???if ((this.CountRecord() % page_count) > 0) pageSum++;???
???return pageSum;
??}
??/// <summary>
??/// 取得SQL語(yǔ)句
??/// </summary>
??/// <param name="vCmd">返回命令行</param>
??/// <returns></returns>
??private string GetSQLCommond(string vCommond,string startID,string endID)
??{
???this.NotShownRecords();//執(zhí)行未顯示的行
???vCommond = "SELECT TOP "+page_count+"? {0},{1},{2},{3}? FROM [UnionInfo]";
???
???if(IsPrior)//判斷“上一頁(yè)”
???{
????
???}
?
???if(IsNext)//判斷“下一頁(yè)”
???{
???}
???if (IsLast)//判斷“最后一頁(yè)”
???{
???}
???vCommond = string.Format(vCommond,"Id","Title","SummaryDateTime","ReadTimes");//這個(gè)是數(shù)據(jù)表的字段
???return vCommond;
??}
??/// <summary>
??/// 輸入按鈕的狀態(tài),進(jìn)行是否可用
??/// </summary>
??/// <param name="first">第一頁(yè)的狀態(tài)</param>
??/// <param name="prior">上一頁(yè)的狀態(tài)</param>
??/// <param name="next1">下一頁(yè)的狀態(tài)</param>
??/// <param name="last">最后一頁(yè)的狀態(tài)</param>
??protected void SetButtonState(bool first_,bool prior_,bool next_,bool last_)
??{
???if (CurrentPage==1)//到“第一頁(yè)”
???{
????first.Disabled = true;//第一頁(yè)狀態(tài)
????prior.Disabled = true;//上一頁(yè)狀態(tài)
????next.Disabled = false;?? //下一頁(yè)狀態(tài)
????last.Disabled = false; //最后一頁(yè)狀態(tài)
???}
???else if (CurrentPage==this.Page_Count())//到“最后一頁(yè)”
???{
????first.Disabled = false;//第一頁(yè)狀態(tài)
????prior.Disabled = false;//上一頁(yè)狀態(tài)
????next.Disabled = true;?? //下一頁(yè)狀態(tài)
????last.Disabled = true; //最后一頁(yè)狀態(tài)
???}
???else
???{
????first.Disabled = first_;//第一頁(yè)狀態(tài)
????prior.Disabled = prior_;//上一頁(yè)狀態(tài)
????next.Disabled = next_;?? //下一頁(yè)狀態(tài)
????last.Disabled = last_; //最后一頁(yè)狀態(tài)
???}
??}
?
??/// <summary>
??/// 第一頁(yè)按鈕
??/// </summary>
??/// <param name="sender"></param>
??/// <param name="e"></param>
??private void first_ServerClick(object sender, System.EventArgs e)
??{????????????
???CurrentPage? = 1;
???this.SetButtonState(true,true,false,false);?
???startID = "";
???endID = "";
???RowCount = '0';
???IsLast = false;
???IsPrior = false;
???IsNext = false;
??}
??/// <summary>
??/// 上一頁(yè)按鈕
??/// </summary>
??/// <param name="sender"></param>
??/// <param name="e"></param>
??private void prior_ServerClick(object sender, System.EventArgs e)
??{
???if( CurrentPage == 1)//判斷“當(dāng)前頁(yè)”是否為1
???{
????this.SetButtonState(true,true,false,false);
???}
???else
???{
????CurrentPage=CurrentPage - 1;//“當(dāng)前頁(yè)”自減
????this.SetButtonState(false,false,false,false);
???}
???IsPrior = true;
???IsNext = false;
???IsLast = false;??
??}
??/// <summary>
??/// 最后一頁(yè)
??/// </summary>
??/// <param name="sender"></param>
??/// <param name="e"></param>
??private void last_ServerClick(object sender, System.EventArgs e)
??{????????
???CurrentPage=this.Page_Count();//到最后一頁(yè)
???this.SetButtonState(false,false,true,true);
???IsLast = true;
???IsPrior = false;
???IsNext = false;
??}
??/// <summary>
??/// 下一頁(yè)
??/// </summary>
??/// <param name="sender"></param>
??/// <param name="e"></param>
??private void next_ServerClick(object sender, System.EventArgs e)
??{??????????????????????
???if(CurrentPage == this.Page_Count())//判斷“當(dāng)前頁(yè)”是否為“分頁(yè)總數(shù)”
???{
????this.SetButtonState(false,false,true,true);
???}
???else
???{
????CurrentPage=CurrentPage + 1;//“當(dāng)前頁(yè)”自加
????this.SetButtonState(false,false,false,false);
???}
???IsNext = true;?
???IsLast = false;
???IsPrior = false;
??}
??/// <summary>
??/// 初始瀏覽按鈕
??/// </summary>
??/// <param name="sender"></param>
??/// <param name="e"></param>
??private void Init_Brower()
??{
???CurrentPage = 1;//肯定是從第一頁(yè)開(kāi)始
???if ((CurrentPage == 1) && (this.Page_Count() == 1))
???{
????first.Disabled = true;//第一頁(yè)狀態(tài)
????prior.Disabled = true;//上一頁(yè)狀態(tài)
????next.Disabled = true;//下一頁(yè)狀態(tài)
????last.Disabled = true; //最后一頁(yè)狀態(tài)
???}
???else
???{
????first.Disabled = true;//第一頁(yè)狀態(tài)
????prior.Disabled = true;//上一頁(yè)狀態(tài)
????next.Disabled = false;//下一頁(yè)狀態(tài)
????last.Disabled = false; //最后一頁(yè)狀態(tài)
???}
???startID = "";//開(kāi)始號(hào)
???endID = "";//結(jié)束號(hào)??
???IsLast = false;
???IsPrior = false;
???IsNext = false;
??}
?}
}
總結(jié)
- 上一篇: ASP.NET抓取其他网页代码
- 下一篇: C# 静态构造函数