如果需要考慮如時間的過濾、其他條件的加入,可以在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;
}
}
}
本文沒有列出sql語句,是希望我和我的好朋友們的勞動成果已經用于商業用途了,還有就是希望各位自己動手寫寫,應該沒有問題的了
新聞熱點
疑難解答