程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C# 簡單分頁 代碼

C# 簡單分頁 代碼

編輯:關於C#
 

如果需要考慮如時間的過濾、其他條件的加入,可以在SQL語句進行編輯,普通的網站,下面的數據浏覽分頁

就可以了。

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結合進行數據浏覽分頁
</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();%><!--輸出數據-->
</table>
<table align="right">
<tr>
<td>
<%PageLoad_Count();%>
<INPUT id="first" type="button" value=" |< " name="first" runat ="server"><!--第一頁-->
<INPUT id="prior" type="button" value=" < " name="prior" runat ="server"><!--上一頁-->
<INPUT id="next" type="button" value=" > " name="next" runat ="server"><!--下一頁-->
<INPUT id="last" type="button" value=" >| " name="last" runat ="server"><!--最後一頁-->
</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;//初始化開始頁面
protected static int RowCount = 0 ;//本頁有多少條
private static bool IsPrior = false;//有“前一頁”
private static bool IsNext = false;//有“下一頁”
private static bool IsLast = false;//有“最後一頁”
protected static int not_shown_records=0;//計算未顯示記錄數
private static string startID = "";//設置上一頁開始ID
private static string endID = "";//設置下一頁結束ID

private static int page_count = 10;//初始化頁面記錄數


private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
if (!IsPostBack)
{
this.CountRecord().ToString();// 記錄總數
this.Page_Count().ToString();//分頁總數

Init_Brower();//初始化浏覽
}
}

#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </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>
/// 顯示數據
/// </summary>
protected void ShowData()
{
DataSet ds = new DataSet();//數據集
ASWBLM.Include.UnionInfo_Provider _uip = new ASWBLM.Include.UnionInfo_Provider();
string vSQL = "";
vSQL = GetSQLCommond(vSQL,startID,endID);

ds = _uip.ShowAllUnionInfo(vSQL);//取得全部數據的數據集

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(); //通過數組,取第一個數據,得到開始號“startID”
RowCount = ds.Tables["Table"].DefaultView.Count;//得到表的行數
endID = ds.Tables["Table"].Rows[RowCount-1].ItemArray[0].ToString();//通過數組,取最後一個數據,得到結束號“endID”
}
catch(SqlException e)
{
Response.Write(e.Message);
}
}


/// <summary>
/// 計算未顯示記錄數
/// </summary>
/// <returns></returns>
protected void NotShownRecords()
{
not_shown_records = this.CountRecord()/*查詢總記錄數*/ - (CurrentPage/*當前頁*/ - 1) * page_count/*每頁記錄數*/;
}


/// <summary>
/// 進行輸出信息
/// </summary>
protected void PageLoad_Count()
{
this.NotShownRecords();
Response.Write("總共"+this.CountRecord()+"條記錄 ");
Response.Write("共有"+this.Page_Count()+"頁 ");
Response.Write("第"+CurrentPage.ToString()+"頁 ");
Response.Write("本頁共有"+RowCount.ToString()+"條記錄 ");
}

 

/// <summary>
/// 獲得總記錄總數
/// </summary>
/// <returns>時間條件范圍內記錄總數intCount</returns>
protected int CountRecord()
{
int intCount = 0;
SqlConnection SqlCon = new SqlConnection(Common._DBConnStr);
SqlCon.Open ();

//找到條件范圍內的記錄總數
string strCount = "select count(*) from UnionInfo";

//找到符合條件的第一個記錄
//string strNum = "select top 1 Id from UnionInfo";

SqlCommand MyComm = new SqlCommand(strCount,SqlCon);
SqlDataReader dr = MyComm.ExecuteReader();//讀取數據流
if(dr.Read())
{
  intCount = Int32.Parse(dr[0].ToString());
}
else
{
  intCount = 0;
}
dr.Close();
SqlCon.Close();
return intCount;
}


/// <summary>
/// 總分頁數
/// </summary>
/// <returns>分頁總數</returns>
protected int Page_Count()
{
int pageSum = 0;//分頁總數
pageSum = this.CountRecord() / page_count; ///記錄總數/分頁的頁數
if ((this.CountRecord() % page_count) > 0) pageSum++;
return pageSum;
}


/// <summary>
/// 取得SQL語句
/// </summary>
/// <param name="vCmd">返回命令行</param>
/// <returns></returns>
private string GetSQLCommond(string vCommond,string startID,string endID)
{
this.NotShownRecords();//執行未顯示的行

vCommond = "SELECT TOP "+page_count+" {0},{1},{2},{3} FROM [UnionInfo]";

if(IsPrior)//判斷“上一頁”
{

}

if(IsNext)//判斷“下一頁”
{

}

if (IsLast)//判斷“最後一頁”
{

}

vCommond = string.Format(vCommond,"Id","Title","SummaryDateTime","ReadTimes");//這個是數據表的字段
return vCommond;
}


/// <summary>
/// 輸入按鈕的狀態,進行是否可用
/// </summary>
/// <param name="first">第一頁的狀態</param>
/// <param name="prior">上一頁的狀態</param>
/// <param name="next1">下一頁的狀態</param>
/// <param name="last">最後一頁的狀態</param>
protected void SetButtonState(bool first_,bool prior_,bool next_,bool last_)
{
if (CurrentPage==1)//到“第一頁”
{
first.Disabled = true;//第一頁狀態
prior.Disabled = true;//上一頁狀態
next.Disabled = false; //下一頁狀態
last.Disabled = false; //最後一頁狀態
}
else if (CurrentPage==this.Page_Count())//到“最後一頁”
{
first.Disabled = false;//第一頁狀態
prior.Disabled = false;//上一頁狀態
next.Disabled = true; //下一頁狀態
last.Disabled = true; //最後一頁狀態
}
else
{
first.Disabled = first_;//第一頁狀態
prior.Disabled = prior_;//上一頁狀態
next.Disabled = next_; //下一頁狀態
last.Disabled = last_; //最後一頁狀態
}
}

 

/// <summary>
/// 第一頁按鈕
/// </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>
/// 上一頁按鈕
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void prior_ServerClick(object sender, System.EventArgs e)
{
if( CurrentPage == 1)//判斷“當前頁”是否為1
{
this.SetButtonState(true,true,false,false);
}
else
{
CurrentPage=CurrentPage - 1;//“當前頁”自減
this.SetButtonState(false,false,false,false);
}
IsPrior = true;
IsNext = false;
IsLast = false;
}


/// <summary>
/// 最後一頁
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void last_ServerClick(object sender, System.EventArgs e)
{
CurrentPage=this.Page_Count();//到最後一頁
this.SetButtonState(false,false,true,true);
IsLast = true;
IsPrior = false;
IsNext = false;
}


/// <summary>
/// 下一頁
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void next_ServerClick(object sender, System.EventArgs e)
{
if(CurrentPage == this.Page_Count())//判斷“當前頁”是否為“分頁總數”
{
this.SetButtonState(false,false,true,true);
}
else
{
CurrentPage=CurrentPage + 1;//“當前頁”自加
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;//肯定是從第一頁開始
if ((CurrentPage == 1) && (this.Page_Count() == 1))
{
first.Disabled = true;//第一頁狀態
prior.Disabled = true;//上一頁狀態
next.Disabled = true;//下一頁狀態
last.Disabled = true; //最後一頁狀態
}
else
{
first.Disabled = true;//第一頁狀態
prior.Disabled = true;//上一頁狀態
next.Disabled = false;//下一頁狀態
last.Disabled = false; //最後一頁狀態
}
startID = "";//開始號
endID = "";//結束號
IsLast = false;
IsPrior = false;
IsNext = false;
}
}
}
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved