数据量很大时,使用gridview内置的分頁功能,页面加载会非常的慢,此处自定义分頁是在需要时才加载当页数据。
using System;
using System.Collections.Generic;using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Reflection;
using System.Text.RegularExpressions;
namespace MyWeb.WebControls
{
/// <summary>
/// 自定GridView分頁
/// 元件使用說明:
///
/// 1.特性說明
///
/// 【SelectCommand: 查詢字符串】
/// 【OrderBy: 排序字符串】
/// 【AllowCustPaging: 是否允許分頁。Default:true】
/// 【ShowRecordCount:是否顯示查詢結果筆數。Default:true】
/// 【ShowExportExcelButton: 是否匯出excel。Default:false】
/// 【ExportExcelHiddenColIndex: 匯出Excel時需隱藏欄位。ex:7,8】
/// 【ExportExcelClearControlIndex:清除gridview列中控件。ex:0,3】
///
/// 2.方法說明
///
/// public void ReDataBind() //重新綁定所有資料
/// </summary>
public class GridViewPage : GridView
{
#region 屬性宣告
private bool _setNewCommand = false;
/// <summary>
/// 查詢的SQL Command
/// </summary>
[Browsable(false)]
public string SelectCommand
{
set
{
ViewState["Command"] = value;
CurrentPage = 1;
if (_setNewCommand)
{
SetDataSource(false);
_setNewCommand = false;
}
else
{
_setNewCommand = true;
}
}
get
{
return (string)ViewState["Command"] ?? "";
}
}
/// <summary>
/// 查詢排序
/// </summary>
[Browsable(false)]
public string OrderBy
{
set
{
ViewState["OrderBy"] = value;
if (_setNewCommand)
{
SetDataSource(false);
_setNewCommand = false;
}
else
{
_setNewCommand = true;
}
}
get
{
return (string)ViewState["OrderBy"] ?? "";
}
}
/// <summary>
/// 取得或設定資料控制項從中擷取資料項目清單的物件
/// </summary>
public override object DataSource
{
get
{
return base.DataSource;
}
set
{
base.DataSource = value;
if (SelectCommand.IsEmpty() && OrderBy.IsEmpty() && value is DataTable)
{
TotalCount = ((DataTable)value).Rows.Count;
TotalPage = TotalCount >0 ? 1 : 0;
}
}
}
/// <summary>
/// 是否顯示匯出excel 按鈕
/// </summary>
[Description("是否顯示匯出excel 按鈕"), Category("CustomAttribute")]
public bool ShowExportExcelButton
{
set { ViewState["ShowExportExcelButton"] = value; }
get { return ViewState["ShowExportExcelButton"] == null ? false : (bool)ViewState["ShowExportExcelButton"]; }
}
/// <summary>
/// 是否顯示查詢結果筆數
/// </summary>
[Description("是否顯示查詢結果筆數"),Category("CustomAttribute")]
public bool ShowRecordCount
{
set { ViewState["ShowRecordCount"] = value; }
get { return ViewState["ShowRecordCount"] == null ? true : (bool)ViewState["ShowRecordCount"]; }
}
/// <summary>
/// 是否允許分頁
/// </summary>
[Description("是否允許分頁"),Category("CustomAttribute")]
public bool AllowCustPaging
{
set { ViewState["AllowCustPaging"] = value; }
get { return ViewState["AllowCustPaging"] == null ? true : (bool)ViewState["AllowCustPaging"]; }
}
/// <summary>
/// 清除gridview列中控件
/// </summary>
[Description("清除gridview列中控件(ex:0,3)"),Category("CustomAttribute")]
public string ExportExcelClearControlIndex
{
set { ViewState["ExportExcelClearControlIndex"] = value; }
get { return (string)ViewState["ExportExcelClearControlIndex"] ?? ""; }
}
/// <summary>
/// 匯出excel需隱藏的列
/// </summary>
[Description("匯出excel需隱藏的列(ex:0,3)"),Category("CustomAttribute")]
public string ExportExcelHiddenColIndex
{
set { ViewState["ExportExcelHiddenColIndex"] = value; }
get { return (string)ViewState["ExportExcelHiddenColIndex"] ?? ""; }
}
protected string EmptyTableRowText
{
get
{
object o = ViewState["EmptyTableRowText"];
return (o != null ? o.ToString() : "");
}
set
{
ViewState["EmptyTableRowText"] = value;
}
}
/// <summary>
/// 是否自動建立欄位
/// </summary>
public override bool AutoGenerateColumns
{
get
{
return false;
}
set
{
base.AutoGenerateColumns = value;
}
}
/// <summary>
/// 資料總頁數
/// </summary>
protected int TotalPage
{
set { ViewState["TotalPage"] = value; }
get { return ViewState["TotalPage"] == null ? -1 : (int)ViewState["TotalPage"]; }
}
/// <summary>
/// 當前頁數
/// </summary>
[Browsable(false)]
public int CurrentPage
{
set { ViewState["CurrentPage"] = value; }
get { return ViewState["CurrentPage"] == null ? 1 : (int)ViewState["CurrentPage"]; }
}
/// <summary>
/// 資料總筆數
/// </summary>
[Browsable(false)]
public int TotalCount
{
set { ViewState["TotalCount"] = value; }
get { return ViewState["TotalCount"] == null ? -1 : (int)ViewState["TotalCount"]; }
}
#endregion