You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
562 lines
20 KiB
562 lines
20 KiB
using System;
|
|
using System.Collections;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Web.UI;
|
|
using System.Web.UI.WebControls;
|
|
|
|
namespace OT.Controls.GridView
|
|
{
|
|
/// <summary>
|
|
/// GridView class
|
|
/// </summary>
|
|
[ToolboxData("<{0}:GridView runat=server></{0}:GridView>")]
|
|
public class GridView : System.Web.UI.WebControls.GridView
|
|
{
|
|
#region 頁面變量
|
|
//private GridViewSort _sortTip;
|
|
private object dataSource;
|
|
private const string HID_PAGECOUNT = "_PageCount";
|
|
private bool isDesignModel = true;
|
|
private string pageCountHidName;
|
|
private int pageCountInternal = 0;
|
|
private GridViewPager pager = new GridViewPager();
|
|
private GridViewSort sort = new GridViewSort();
|
|
private Color strRowColor;
|
|
private int totalRowCount = 0;
|
|
#endregion
|
|
|
|
#region 公開屬性
|
|
|
|
[Description("GridView排序字串")]
|
|
public string SortString { get; set; }
|
|
|
|
/// <summary>
|
|
/// GridView排序方向
|
|
/// </summary>
|
|
/// <value>
|
|
/// The grid view sort direction.
|
|
/// </value>
|
|
[Description("GridView排序方向")]
|
|
public SortDirection GridViewSortDirection
|
|
{
|
|
get
|
|
{
|
|
if (this.ViewState["sortDirection"] == null)
|
|
{
|
|
this.ViewState["sortDirection"] = SortDirection.Ascending;
|
|
}
|
|
return (SortDirection)this.ViewState["sortDirection"];
|
|
}
|
|
set
|
|
{
|
|
this.ViewState["sortDirection"] = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// GridView排序欄位
|
|
/// </summary>
|
|
/// <value>
|
|
/// The grid view sort expression.
|
|
/// </value>
|
|
[Description("GridView排序欄位")]
|
|
public string GridViewSortExpression
|
|
{
|
|
get
|
|
{
|
|
if (this.ViewState["sortExpression"] == null)
|
|
{
|
|
return this.SortExpression;
|
|
}
|
|
return this.ViewState["sortExpression"].ToString();
|
|
}
|
|
set
|
|
{
|
|
this.ViewState["sortExpression"] = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 資料行的顏色
|
|
/// </summary>
|
|
/// <value>
|
|
/// The color of the row.
|
|
/// </value>
|
|
[Category("擴展"), Description("資料行的顏色"), DefaultValue(typeof(Color), ""), TypeConverter(typeof(WebColorConverter)), Browsable(true)]
|
|
public Color RowColor
|
|
{
|
|
get
|
|
{
|
|
return this.strRowColor;
|
|
}
|
|
set
|
|
{
|
|
this.strRowColor = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否显示分页部分 默认为True显示
|
|
/// </summary>
|
|
/// <value>
|
|
/// <c>true</c> if [show pager]; otherwise, <c>false</c>.
|
|
/// </value>
|
|
[Category("擴展"), Description("是否顯示分頁部份預設為True顯示"), DefaultValue(typeof(bool), "true"), TypeConverter(typeof(bool)), Browsable(true)]
|
|
public bool ShowPager
|
|
{
|
|
get
|
|
{
|
|
if (ViewState["ShowPager"] != null)
|
|
{
|
|
return (bool)ViewState["ShowPager"];
|
|
}
|
|
else
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
set
|
|
{
|
|
ViewState["ShowPager"] = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 资料笔数多语言 默认为Total
|
|
/// </summary>
|
|
[Category("擴展"), Description("资料笔数多语言 默认为Total"), DefaultValue(typeof(string), "Total"), TypeConverter(typeof(string)), Browsable(true)]
|
|
public string TotalText
|
|
{
|
|
get
|
|
{
|
|
if (ViewState["Total"] != null)
|
|
{
|
|
return (string)ViewState["Total"];
|
|
}
|
|
else
|
|
{
|
|
return "Total";
|
|
}
|
|
}
|
|
set
|
|
{
|
|
ViewState["Total"] = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 页码多语言设置 默认Page
|
|
/// </summary>
|
|
[Category("擴展"), Description("页码多语言设置 默认Page"), DefaultValue(typeof(string), "Page"), TypeConverter(typeof(string)), Browsable(true)]
|
|
public string PageText
|
|
{
|
|
get
|
|
{
|
|
if (ViewState["PageText"] != null)
|
|
{
|
|
return (string)ViewState["PageText"];
|
|
}
|
|
else
|
|
{
|
|
return "Page";
|
|
}
|
|
}
|
|
set
|
|
{
|
|
ViewState["PageText"] = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 排序提示信息
|
|
/// </summary>
|
|
[PersistenceMode(PersistenceMode.InnerProperty), Description("排序提示信息"), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("擴展"), DefaultValue(typeof(GridViewSort), "")]
|
|
public virtual GridViewSort SortTip
|
|
{
|
|
get
|
|
{
|
|
return this.sort;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 取得或設定資料繫結控制項從中擷取其資料項目清單的物件。
|
|
/// </summary>
|
|
/// <returns>
|
|
/// 物件,表示資料繫結控制項從中擷取其資料的資料來源。預設值為 null。
|
|
/// </returns>
|
|
public override object DataSource
|
|
{
|
|
get
|
|
{
|
|
return this.dataSource;
|
|
}
|
|
set
|
|
{
|
|
DataTable table = new DataTable();
|
|
if (value is DataView)
|
|
{
|
|
table = (value as DataView).ToTable();
|
|
}
|
|
else if (value is DataSet)
|
|
{
|
|
table = (value as DataSet).Tables[0];
|
|
}
|
|
else if (value is DataTable)
|
|
{
|
|
table = value as DataTable;
|
|
}
|
|
|
|
if (table.Columns.Contains(GridViewSortExpression))
|
|
{
|
|
DataView view = new DataView(table);
|
|
string direction = "ASC";
|
|
if (this.GridViewSortDirection == SortDirection.Descending)
|
|
{
|
|
direction = "DESC";
|
|
}
|
|
view.Sort = GridViewSortExpression + " " + direction;
|
|
table = view.ToTable();
|
|
}
|
|
|
|
this.dataSource = table;
|
|
base.DataSource = table;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 公有方法
|
|
#endregion
|
|
|
|
#region 私有方法
|
|
private void Datarebind()
|
|
{
|
|
base.DataSource = this.DataSource;
|
|
this.DataBind();
|
|
}
|
|
|
|
private void Datarebind(string sortExpression, string direction)
|
|
{
|
|
if (this.DataSource != null)
|
|
{
|
|
DataView view = new DataView(this.DataSource as DataTable);
|
|
view.Sort = sortExpression + " " + direction;
|
|
SortString = view.Sort;
|
|
this.DataSource = view.ToTable();
|
|
this.DataBind();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 載入先前儲存之 <see cref="T:System.Web.UI.WebControls.GridView"/> 控制項的檢視狀態。
|
|
/// </summary>
|
|
/// <param name="savedState"><see cref="T:System.Object"/>,含有控制項的已儲存的檢視狀態值。</param>
|
|
protected override void LoadViewState(object savedState)
|
|
{
|
|
ArrayList list = savedState as ArrayList;
|
|
if (!list[0].ToString().Equals(""))
|
|
{
|
|
this.dataSource = TypeConverter.DeserializeDataTable(list[0] as string);
|
|
}
|
|
base.LoadViewState(list[1]);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 載入先前儲存之 <see cref="T:System.Web.UI.WebControls.GridView"/> 控制項的檢視狀態。
|
|
/// </summary>
|
|
/// <returns>
|
|
/// <see cref="T:System.Object"/>,包含控制項之已儲存的檢視狀態值。
|
|
/// </returns>
|
|
protected override object SaveViewState()
|
|
{
|
|
ArrayList list = new ArrayList();
|
|
list.Add(TypeConverter.SerializeDataTableXml(this.dataSource as DataTable));
|
|
list.Add(base.SaveViewState());
|
|
return list;
|
|
}
|
|
#endregion
|
|
|
|
#region 頁面事件
|
|
|
|
/// <summary>
|
|
/// 引發 <see cref="E:System.Web.UI.Control.DataBinding"/> 事件。
|
|
/// </summary>
|
|
/// <param name="e">包含事件資料的 <see cref="T:System.EventArgs"/> 物件。</param>
|
|
protected override void OnDataBinding(EventArgs e)
|
|
{
|
|
if (this.GetData().CanRetrieveTotalRowCount)
|
|
{
|
|
this.totalRowCount = base.SelectArguments.TotalRowCount;
|
|
}
|
|
else
|
|
{
|
|
ICollection dataSource = this.DataSource as ICollection;
|
|
if (dataSource != null)
|
|
{
|
|
this.totalRowCount = dataSource.Count;
|
|
this.totalRowCount += this.PageIndex * this.PageSize;
|
|
}
|
|
else
|
|
{
|
|
this.totalRowCount = (this.DataSource as DataTable).Rows.Count;
|
|
}
|
|
}
|
|
this.pager.TotalPageCount = (int)Math.Ceiling((double)(Convert.ToDouble(this.totalRowCount) / Convert.ToDouble(this.PageSize)));
|
|
this.pageCountInternal = this.pager.TotalPageCount;
|
|
this.pager.GoTextBoxWidth = 30;
|
|
this.PagerTemplate = this.pager;
|
|
base.OnDataBinding(e);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 引發 <see cref="E:System.Web.UI.Control.Init"/> 事件。
|
|
/// </summary>
|
|
/// <param name="e"><see cref="T:System.EventArgs"/> 包含事件資料。</param>
|
|
protected override void OnInit(EventArgs e)
|
|
{
|
|
base.OnInit(e);
|
|
this.Page.Load += new EventHandler(this.Page_Load);
|
|
this.pageCountHidName = this.ClientID + "_PageCount";
|
|
bool flag = false;
|
|
if (this.Page != null)
|
|
{
|
|
if (this.Page.Site == null)
|
|
{
|
|
flag = true;
|
|
}
|
|
else if (!this.Page.Site.DesignMode)
|
|
{
|
|
flag = true;
|
|
}
|
|
}
|
|
if (flag && (this.Page.Request[this.pageCountHidName] != null))
|
|
{
|
|
this.pageCountInternal = Convert.ToInt32(this.Page.Request[this.pageCountHidName]);
|
|
this.pager.TotalPageCount = this.pageCountInternal;
|
|
this.PagerTemplate = this.pager;
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 處理 <see cref="E:System.Web.UI.Control.Load"/> 事件。
|
|
/// </summary>
|
|
/// <param name="e"><see cref="T:System.EventArgs"/> 物件,包含事件資料。</param>
|
|
protected override void OnLoad(EventArgs e)
|
|
{
|
|
this.isDesignModel = false;
|
|
base.OnLoad(e);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 引發 <see cref="E:System.Web.UI.Control.PreRender"/> 事件。
|
|
/// </summary>
|
|
/// <param name="e"><see cref="T:System.EventArgs"/> 包含事件資料。</param>
|
|
protected override void OnPreRender(EventArgs e)
|
|
{
|
|
if (this.isDesignModel)
|
|
{
|
|
this.EnsureChildControls();
|
|
}
|
|
if (this.PageIndex > 0)
|
|
{
|
|
((DropDownList)this.BottomPagerRow.FindControl("dprTurnPage")).SelectedValue = Convert.ToString((int)(this.PageIndex + 1));
|
|
}
|
|
this.Page.ClientScript.RegisterHiddenField(this.pageCountHidName, this.pageCountInternal.ToString());
|
|
base.OnPreRender(e);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 引發 <see cref="E:System.Web.UI.WebControls.GridView.RowDataBound"/> 事件。
|
|
/// </summary>
|
|
/// <param name="e"><see cref="T:System.Web.UI.WebControls.GridViewRowEventArgs"/> 包含事件資料。</param>
|
|
protected override void OnRowDataBound(GridViewRowEventArgs e)
|
|
{
|
|
if (e.Row.RowType == DataControlRowType.DataRow)
|
|
{
|
|
string str = this.RowColor.Name.ToString().Equals("0") ? "" : this.RowColor.Name.ToString();
|
|
e.Row.Attributes.Add("onmouseover", "window.previousRowColor = this.style.backgroundColor;this.style.backgroundColor='" + str + "';this.style.cursor='pointer';");
|
|
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=window.previousRowColor;this.style.cursor='default';");
|
|
|
|
}
|
|
else if (e.Row.RowType == DataControlRowType.Pager)
|
|
{
|
|
((Label)e.Row.FindControl("lblItemCount")).Text = " " + TotalText + ":" + this.totalRowCount;
|
|
int num = this.PageIndex + 1;
|
|
((Label)e.Row.FindControl("lblTotalPage")).Text = " " + PageText + ":" + num.ToString() + " / " + this.PageCount.ToString() + " ";
|
|
string str2 = "if(event.keyCode == 13){ var btnGo = event.srcElement.nextSibling.nextSibling; btnGo.click(); event.returnValue = false; }";
|
|
((TextBox)e.Row.FindControl("txtJumper")).Attributes.Add("onkeydown", str2);
|
|
string str3 = @"value=value.replace(/[^\d]/g, '');";
|
|
((TextBox)e.Row.FindControl("txtJumper")).Attributes.Add("onkeyup", str3);
|
|
((TextBox)e.Row.FindControl("txtJumper")).Attributes.Add("onpaste", str3);
|
|
e.Row.HorizontalAlign = HorizontalAlign.Center;
|
|
if (!ShowPager)
|
|
{
|
|
e.Row.Visible = false;
|
|
}
|
|
}
|
|
else if (e.Row.RowType == DataControlRowType.Header)
|
|
{
|
|
for (int i = 0; i < e.Row.Cells.Count; i++)
|
|
{
|
|
if (((e.Row.Cells[i].Controls.Count == 1) && (e.Row.Cells[i].Controls[0] is LinkButton)) && (((LinkButton)e.Row.Cells[i].Controls[0]).CommandArgument == this.GridViewSortExpression))
|
|
{
|
|
System.Web.UI.WebControls.Image child = null;
|
|
Label label = null;
|
|
if (this.GridViewSortDirection == SortDirection.Ascending)
|
|
{
|
|
if (!string.IsNullOrEmpty(this.sort.SortAscImage))
|
|
{
|
|
child = new System.Web.UI.WebControls.Image();
|
|
child.ImageUrl = base.ResolveUrl(this.sort.SortAscImage);
|
|
}
|
|
if (!string.IsNullOrEmpty(this.sort.SortAscText))
|
|
{
|
|
label = new Label();
|
|
label.Text = this.sort.SortAscText;
|
|
}
|
|
}
|
|
else if (this.GridViewSortDirection == SortDirection.Descending)
|
|
{
|
|
if (!string.IsNullOrEmpty(this.sort.SortDescImage))
|
|
{
|
|
child = new System.Web.UI.WebControls.Image();
|
|
child.ImageUrl = base.ResolveUrl(this.sort.SortDescImage);
|
|
}
|
|
if (!string.IsNullOrEmpty(this.sort.SortDescText))
|
|
{
|
|
label = new Label();
|
|
label.Text = this.sort.SortDescText;
|
|
}
|
|
}
|
|
if (child != null)
|
|
{
|
|
e.Row.Cells[i].Controls.Add(child);
|
|
}
|
|
if (label != null)
|
|
{
|
|
(e.Row.Cells[i].Controls[0] as LinkButton).ToolTip = label.Text;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
base.OnRowDataBound(e);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 引發 <see cref="E:System.Web.UI.WebControls.GridView.Sorting"/> 事件。
|
|
/// </summary>
|
|
/// <param name="e"><see cref="T:System.Web.UI.WebControls.GridViewSortEventArgs"/> 包含事件資料。</param>
|
|
/// <exception cref="T:System.Web.HttpException">
|
|
/// <see cref="E:System.Web.UI.WebControls.GridView.Sorting"/> 事件沒有任何處理常式。
|
|
/// </exception>
|
|
protected override void OnSorting(GridViewSortEventArgs e)
|
|
{
|
|
this.GridViewSortExpression = e.SortExpression;
|
|
if (this.GridViewSortDirection == SortDirection.Ascending)
|
|
{
|
|
this.GridViewSortDirection = SortDirection.Descending;
|
|
e.SortDirection = SortDirection.Descending;
|
|
this.Datarebind(this.GridViewSortExpression, "DESC");
|
|
}
|
|
else
|
|
{
|
|
e.SortDirection = SortDirection.Ascending;
|
|
this.GridViewSortDirection = SortDirection.Ascending;
|
|
this.Datarebind(this.GridViewSortExpression, "ASC");
|
|
}
|
|
|
|
//this.sortExpression = e.SortExpression;
|
|
//this.ViewState.Add("sortExpression", this.sortExpression);
|
|
//if (e.SortDirection == SortDirection.Ascending)
|
|
//{
|
|
// this.GridViewSortDirection = SortDirection.Ascending;
|
|
// this.Datarebind(this.sortExpression, "ASC");
|
|
//}
|
|
//else
|
|
//{
|
|
// this.GridViewSortDirection = SortDirection.Descending;
|
|
// this.Datarebind(this.sortExpression, "DESC");
|
|
//}
|
|
}
|
|
|
|
private void Page_Load(object sender, EventArgs e)
|
|
{
|
|
if (this.AllowPaging)
|
|
{
|
|
this.pager.ProviousPageClick += new ImageClickEventHandler(this.pager_ProviousPageClick);
|
|
this.pager.NextPageClick += new ImageClickEventHandler(this.pager_NextPageClick);
|
|
this.pager.PageJump += new EventHandler(this.pager_PageJump);
|
|
this.pager.PageSelect += new EventHandler(this.pager_PageSelect);
|
|
this.pager.LastPageClick += new ImageClickEventHandler(this.pager_LastPageClick);
|
|
this.pager.FirstPageClick += new ImageClickEventHandler(this.pager_FirstPageClick);
|
|
this.pager.GoPageText = "Go";
|
|
this.PagerTemplate = this.pager;
|
|
}
|
|
}
|
|
|
|
private void pager_FirstPageClick(object sender, EventArgs e)
|
|
{
|
|
this.PageIndex = 0;
|
|
this.Datarebind();
|
|
}
|
|
|
|
private void pager_LastPageClick(object sender, EventArgs e)
|
|
{
|
|
this.PageIndex = this.PageCount - 1;
|
|
this.Datarebind();
|
|
}
|
|
|
|
private void pager_NextPageClick(object sender, EventArgs e)
|
|
{
|
|
if (this.PageIndex < this.PageCount - 1)
|
|
{
|
|
this.PageIndex++;
|
|
this.Datarebind();
|
|
}
|
|
}
|
|
|
|
private void pager_PageJump(object sender, EventArgs e)
|
|
{
|
|
string text = ((TextBox)this.BottomPagerRow.FindControl("txtJumper")).Text;
|
|
int result = 0;
|
|
if (int.TryParse(text, out result))
|
|
{
|
|
if (result <= 0)
|
|
{
|
|
result = 1;
|
|
}
|
|
if (result >= this.PageCount)
|
|
{
|
|
result = this.PageCount;
|
|
}
|
|
this.PageIndex = result - 1;
|
|
}
|
|
else
|
|
{
|
|
((TextBox)this.BottomPagerRow.FindControl("txtJumper")).Text = "";
|
|
}
|
|
this.Datarebind();
|
|
((TextBox)this.BottomPagerRow.FindControl("txtJumper")).Text = "";
|
|
}
|
|
|
|
private void pager_PageSelect(object sender, EventArgs e)
|
|
{
|
|
int num = Convert.ToInt32(((DropDownList)sender).SelectedValue);
|
|
this.PageIndex = num - 1;
|
|
this.Datarebind();
|
|
}
|
|
|
|
private void pager_ProviousPageClick(object sender, EventArgs e)
|
|
{
|
|
if (this.PageIndex > 0)
|
|
{
|
|
this.PageIndex--;
|
|
this.Datarebind();
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|