DataGrid连接Access的快速分页法(5)——实现快速分页

2016-02-19 17:52 8 1 收藏

下面图老师小编要向大家介绍下DataGrid连接Access的快速分页法(5)——实现快速分页,看起来复杂实则是简单的,掌握好技巧就OK,喜欢就赶紧收藏起来吧!

【 tulaoshi.com - 编程语言 】

  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
titleDataGrid + 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'";

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

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 实现
// 设收藏 。

来源:https://www.tulaoshi.com/n/20160219/1616247.html

延伸阅读
标签: 软件教程 word
使用Word的朋友都知道想要分页换页是要不断按回车键,很多朋友苦思冥想着更快捷的方法,寻求快速翻页的秘诀。如果你还没有找到,那么可不要错过本文哦。 问题:Word中如何换页,也就是想在下一页写? 在空白文档录入过程中的换页: 一是用回车的方式强行换行到下一页二是执行“插入/分隔符/下一页”命令到下一页 可以使用下面的...
标签: Web开发
如何在DataGrid控件中实现自定义分页      在一般情况下,DataGrid控件每次实现翻页操作时,都会将数据源中的数据重新调用一次,当数据中 数据很多时,这样做就会很浪费系统资源和降低程序的执行效率.这时候我们一般通过自定义分页来解 决这个问题.     DataGrid控件的AllowCustomPaging属性用来...
标签: Web开发
代码如下: !-- //ASP分页函数 function ShowListPage(page,Pcount,TopicNum,maxperpage,strLink,ListName){     var alertcolor = '#FF0000';     maxperpage=Math.floor(maxperpage);     TopicNum=Math.floor(TopicNum);     p...
标签: Web开发
From: IECN.Net ; Author: 钟钟 /**  * 分页类构造  * 参数 nTotalList: 总条数  * 参数 nPageSize: 每页显示条数  * 参数 nPageNum: 当前页码  * 参数 sPageUrl: 分页链接的URL,页码以[pn]代替,输出时将被替换为实际...
标签: Web开发
由于查询返回的数据量很大,超过10w条数据,因此需要对页面查询功能进行优化。放弃原有程序中使用DataGrid的做法,自己编写分页显示模块。     首先在页面上添加几个DIV:         div id="div_trackpoint" style=" border:solid 1px gray; height:230px; width:99%; overflow-y...

经验教程

159

收藏

41
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部