麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 數據庫 > Access > 正文

DataGrid連接Access的快速分頁法(5)——實現快速分頁

2024-09-07 19:04:58
字體:
來源:轉載
供稿:網友
datagrid連接access的快速分頁法(5)——實現快速分頁

我使用access自帶的northwind中文數據庫的“訂單明細”表作為例子,不過我在該表添加了一個名為“id”的字段,數據類型為“自動編號”,并把該表命名為“訂單明細表”。

fastpaging_dataset.aspx
--------------------------------------------------------------------------------------
<%@ page language="c#" codebehind="fastpaging_dataset.aspx.cs" autoeventwireup="false" inherits="paging.fastpaging_dataset" enablesessionstate="false" enableviewstate="true" enableviewstatemac="false" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>datagrid + datareader 自定義分頁</title>
<meta content="microsoft visual studio .net 7.1" name="generator">
<meta content="c#" name="code_language">
<meta content="javascript" name="vs_defaultclientscript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema">
</head>
<body>
<form runat="server">
<asp:datagrid id="datagrid1" runat="server" borderwidth="1px" bordercolor="black" font-size="12pt"
alternatingitemstyle-backcolor="#eeeeee" headerstyle-backcolor="#aaaadd" pagerstyle-horizontalalign="right"
cellpadding="3" allowpaging="true" allowcustompaging="true" autogeneratecolumns="false" onpageindexchanged="mydatagrid_page"
pagesize="15" allowsorting="true" onsortcommand="datagrid1_sortcommand">
<alternatingitemstyle backcolor="#eeeeee"></alternatingitemstyle>
<itemstyle font-size="smaller" borderwidth="22px"></itemstyle>
<headerstyle backcolor="#aaaadd"></headerstyle>
<columns>
<asp:boundcolumn datafield="id" sortexpression="id" headertext="id"></asp:boundcolumn>
<asp:boundcolumn datafield="訂單id" headertext="訂單id"></asp:boundcolumn>
<asp:boundcolumn datafield="產品id" headertext="產品id"></asp:boundcolumn>
<asp:boundcolumn datafield="單價" headertext="單價"></asp:boundcolumn>
<asp:boundcolumn datafield="數量" headertext="數量"></asp:boundcolumn>
<asp:boundcolumn datafield="折扣" headertext="折扣"></asp:boundcolumn>
</columns>
<pagerstyle font-names="verdana" font-bold="true" horizontalalign="right" forecolor="coral"
mode="numericpages"></pagerstyle>
</asp:datagrid></form>
</body>
</html>


fastpaging_dataset.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.oledb;
using system.text;

namespace paging
{
public class fastpaging_dataset : system.web.ui.page
{
protected system.web.ui.webcontrols.datagrid datagrid1;

const string query_fields = "*"; //要查詢的字段
const string table_name = "訂單明細表"; //數據表名稱
const string primary_key = "id"; //主鍵字段
const string def_order_type = "asc"; //默認排序方式
const string sec_order_type = "desc"; //可選排序方式
const string condition = "產品id='av-cb-1'";

oledbconnection conn;
oledbcommand cmd;
oledbdataadapter da;

#region 屬性

#region currentpageindex
/// <summary>
/// 獲取或設置當前頁的索引。
/// </summary>
public int currentpageindex
{
get { return (int) viewstate["currentpageindex"]; }
set { viewstate["currentpageindex"] = value; }
}
#endregion

#region ordertype
/// <summary>
/// 獲取排序的方式:升序(asc)或降序(desc)。
/// </summary>
public string ordertype
{
get {
string ordertype = def_order_type;
if (viewstate["ordertype"] != null) {
ordertype = (string)viewstate["ordertype"];
if (ordertype != sec_order_type)
ordertype = def_order_type;
}
return ordertype;
}
set { viewstate["ordertype"] = value.toupper(); }
}
#endregion

#endregion

private void page_load(object sender, system.eventargs e)
{
#region 實現
string strconn = "provider=microsoft.jet.oledb.4.0;data source="
+ server.mappath("northwind.mdb");
conn = new oledbconnection(strconn);
cmd = new oledbcommand("",conn);
da = new oledbdataadapter(cmd);

if (!ispostback) {
// 設置用于自動計算頁數的記錄總數
datagrid1.virtualitemcount = getrecordcount(table_name);

currentpageindex = 0;
binddatagrid();
}
#endregion
}

#region web 窗體設計器生成的代碼
override protected void oninit(eventargs e)
{
//
// codegen: 該調用是 asp.net web 窗體設計器所必需的。
//
initializecomponent();
base.oninit(e);
}

/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion

private void binddatagrid()
{
#region 實現
// 設置當前頁的索引
datagrid1.currentpageindex = currentpageindex;
// 設置數據源
datagrid1.datasource = getdataview();
datagrid1.databind();
#endregion
}

/// <summary>
/// 取得數據庫表中的記錄總數。
/// </summary>
/// <param name="tablename">數據庫中的表的名稱。</param>
/// <returns>成功則為表中記錄的總數;否則為 -1。</returns>
private int getrecordcount(string tablename)
{
#region 實現
int count;
cmd.commandtext = "select count(*) as recordcount from " + tablename;
try {
conn.open();
count = convert.toint32(cmd.executescalar());
} catch(exception ex) {
response.write (ex.message.tostring());
count = -1;
} finally {
conn.close();
}
return count;
#endregion
}

private dataview getdataview()
{
#region 實現
int pagesize = datagrid1.pagesize;
dataset ds = new dataset();
dataview dv = null;

cmd.commandtext = fastpaging.paging(
pagesize,
currentpageindex,
datagrid1.virtualitemcount,
table_name,
query_fields,
primary_key,
fastpaging.isascending(ordertype) );

try {
da.fill(ds, table_name);
dv = ds.tables[0].defaultview;
} catch(exception ex) {
response.write (ex.message.tostring());
}
return dv;
#endregion
}

protected void mydatagrid_page(object sender, datagridpagechangedeventargs e)
{
currentpageindex = e.newpageindex;
binddatagrid();
}

protected void datagrid1_sortcommand(object source, datagridsortcommandeventargs e)
{
#region 實現
datagrid1.currentpageindex = 0;
this.currentpageindex = 0;

if (ordertype == def_order_type)
ordertype = sec_order_type;
else
ordertype = def_order_type;

binddatagrid();
#endregion
}
}
}

國內最大的酷站演示中心!
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧洲狠狠鲁 | 毛片在线免费播放 | 国产免费一区二区三区网站免费 | 欧美日本在线视频 | 免费一级a毛片免费观看 | 久久国产亚洲视频 | 一区二区网 | 精品免费国产一区二区三区 | 黄色a级片免费观看 | 中国女警察一级毛片视频 | 深夜免费观看视频 | 玩偶姐姐 在线观看 | 国产高潮好爽好大受不了了 | 欧美日韩在线视频一区 | 欧美日本免费一区二区三区 | 亚洲 综合 欧美 动漫 丝袜图 | 欧美18videos性处按摩 | 久久九九热re6这里有精品 | 韩国精品视频在线观看 | 精品亚洲va在线va天堂资源站 | 欧美成a人片在线观看久 | 欧美人与禽性xxxxx杂性 | 操操影视 | 另类亚洲孕妇分娩网址 | 91九色视频 | 国产成人在线观看免费 | 日韩视频区 | 久久久午夜电影 | 依依成人综合 | 丰满年轻岳中文字幕一区二区 | 久草在线播放视频 | 成人午夜免费福利 | 精品国产一区二区亚洲人成毛片 | 免费a级观看 | 亚洲一区二区免费 | 久章草在线观看 | 午夜精品网站 | 成人9禁啪啪无遮挡免费 | 日韩激情一区 | 护士hd欧美free性xxxx | 久久国产乱子伦精品 |