📄 extendedgridview.cs
字号:
namespace PowerEasy.Controls
{
using PowerEasy.Common;
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
[ToolboxBitmap(typeof(ExtendedGridView), "ExtendedGridView.bmp"), ToolboxData("<{0}:ExtendedGridView ID=\"Egv\" runat=\"server\"></{0}:ExtendedGridView>"), Themeable(true)]
public class ExtendedGridView : GridView
{
private const string CheckBoxColumHeaderID = "{0}_HeaderButton";
private const string CheckBoxColumHeaderTemplate = "<input type='checkbox' hidefocus='true' id='{0}' name='{0}' onclick='CheckAll(this)'>";
private const string ExtendedGridView_JS = "PowerEasy.Controls.ExtendedGridView.ExtendedGridView.js";
private int m_RawPageIndex;
private string m_UniqueControlPageIndex;
private string m_UniqueControlPageSize;
private ArrayList AddCheckBoxColumn(ICollection columnList)
{
ArrayList list = new ArrayList(columnList);
InputCheckBoxField field = new InputCheckBoxField();
string str = string.Format("{0}_HeaderButton", this.ClientID);
field.HeaderText = string.Format("<input type='checkbox' hidefocus='true' id='{0}' name='{0}' onclick='CheckAll(this)'>", str);
field.HeaderStyle.Width = this.CheckBoxFieldHeaderWidth;
field.ReadOnly = true;
field.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
if (this.CheckBoxColumnIndex > list.Count)
{
list.Add(field);
this.CheckBoxColumnIndex = list.Count - 1;
return list;
}
list.Insert(this.CheckBoxColumnIndex, field);
return list;
}
private ArrayList AddSerialColumn(ICollection columnList)
{
ArrayList list = new ArrayList(columnList);
PowerEasy.Controls.TemplateField field = new PowerEasy.Controls.TemplateField();
field.HeaderText = this.SerialText;
if (this.SerialColumnIndex > list.Count)
{
list.Add(field);
this.SerialColumnIndex = list.Count - 1;
return list;
}
list.Insert(this.SerialColumnIndex, field);
return list;
}
protected override ICollection CreateColumns(PagedDataSource dataSource, bool useDataSource)
{
ICollection columnList = base.CreateColumns(dataSource, useDataSource);
if (!this.AutoGenerateCheckBoxColumn && !this.AutoGenerateSerialColumn)
{
return columnList;
}
ArrayList list = new ArrayList();
if (this.AutoGenerateCheckBoxColumn)
{
list = this.AddCheckBoxColumn(columnList);
}
if (this.AutoGenerateSerialColumn)
{
list = this.AddSerialColumn(columnList);
}
return list;
}
private void CreateCustomPagerRow(GridViewRowEventArgs e)
{
e.Row.Cells[0].Controls.Clear();
e.Row.Cells[0].Controls.Add(new LiteralControl("共 "));
Label child = new Label();
child.ID = "LblRowsCount";
child.Text = this.VirtualItemCount.ToString();
child.Font.Bold = true;
e.Row.Cells[0].Controls.Add(child);
e.Row.Cells[0].Controls.Add(new LiteralControl(" " + this.ItemUnit + this.ItemName));
e.Row.Cells[0].Controls.Add(new LiteralControl(" "));
LinkButton button = new LinkButton();
button.CommandName = "Page";
button.CommandArgument = "First";
button.Enabled = this.PageIndex != 0;
button.Text = "首页";
e.Row.Cells[0].Controls.Add(button);
e.Row.Cells[0].Controls.Add(new LiteralControl(" "));
LinkButton button2 = new LinkButton();
button2.CommandName = "Page";
button2.CommandArgument = "Prev";
button2.Enabled = this.PageIndex != 0;
button2.Text = "上一页";
e.Row.Cells[0].Controls.Add(button2);
e.Row.Cells[0].Controls.Add(new LiteralControl(" "));
LinkButton button3 = new LinkButton();
button3.CommandName = "Page";
button3.CommandArgument = "Next";
button3.Enabled = this.PageIndex != (this.PageCount - 1);
button3.Text = "下一页";
e.Row.Cells[0].Controls.Add(button3);
e.Row.Cells[0].Controls.Add(new LiteralControl(" "));
LinkButton button4 = new LinkButton();
button4.CommandName = "Page";
button4.CommandArgument = "Last";
button4.Enabled = this.PageIndex != (this.PageCount - 1);
button4.Text = "尾页";
e.Row.Cells[0].Controls.Add(button4);
e.Row.Cells[0].Controls.Add(new LiteralControl(" "));
if (this.IsHoldState)
{
button.Click += new EventHandler(this.LbtnFirst_Click);
button2.Click += new EventHandler(this.LbtnPrev_Click);
button3.Click += new EventHandler(this.LbtnNext_Click);
button4.Click += new EventHandler(this.LbtnLast_Click);
}
e.Row.Cells[0].Controls.Add(new LiteralControl("页次:"));
Label label2 = new Label();
label2.Text = Convert.ToString((int) (this.PageIndex + 1));
label2.Font.Bold = true;
label2.ForeColor = Color.Red;
e.Row.Cells[0].Controls.Add(label2);
e.Row.Cells[0].Controls.Add(new LiteralControl("/"));
Label label3 = new Label();
label3.Text = Convert.ToString(this.PageCount);
label3.Font.Bold = true;
e.Row.Cells[0].Controls.Add(label3);
e.Row.Cells[0].Controls.Add(new LiteralControl("页"));
e.Row.Cells[0].Controls.Add(new LiteralControl(" "));
TextBox box = new TextBox();
box.ApplyStyleSheetSkin(this.Page);
box.MaxLength = 3;
box.Width = 0x16;
box.Text = Convert.ToString(this.PageSize);
box.AutoPostBack = true;
box.TextChanged += new EventHandler(this.TxtMaxPerPage_TextChanged);
e.Row.Cells[0].Controls.Add(box);
e.Row.Cells[0].Controls.Add(new LiteralControl(this.ItemUnit + this.ItemName + "/页"));
e.Row.Cells[0].Controls.Add(new LiteralControl(" "));
e.Row.Cells[0].Controls.Add(new LiteralControl("转到第"));
if (this.PageCount < 10)
{
DropDownList list = new DropDownList();
list.ApplyStyleSheetSkin(this.Page);
list.AutoPostBack = true;
list.SelectedIndexChanged += new EventHandler(this.DropCurrentPage_SelectedIndexChanged);
ArrayList list2 = new ArrayList();
for (int i = 1; i <= this.PageCount; i++)
{
list2.Add(i);
}
list.DataSource = list2;
list.DataBind();
list.SelectedIndex = this.PageIndex;
e.Row.Cells[0].Controls.Add(list);
}
else
{
TextBox box2 = new TextBox();
box2.ApplyStyleSheetSkin(this.Page);
box2.Width = 30;
box2.Text = Convert.ToString((int) (this.PageIndex + 1));
box2.AutoPostBack = true;
box2.TextChanged += new EventHandler(this.TxtCurrentPage_TextChanged);
e.Row.Cells[0].Controls.Add(box2);
}
e.Row.Cells[0].Controls.Add(new LiteralControl("页"));
}
protected void DropCurrentPage_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList list = (DropDownList) sender;
this.PageIndex = Convert.ToInt32(list.SelectedValue) - 1;
if (this.IsHoldState)
{
this.Context.Session[this.m_UniqueControlPageIndex] = this.PageIndex;
}
}
protected void LbtnFirst_Click(object sender, EventArgs e)
{
this.Context.Session[this.m_UniqueControlPageIndex] = 0;
}
protected void LbtnLast_Click(object sender, EventArgs e)
{
this.Context.Session[this.m_UniqueControlPageIndex] = this.PageCount - 1;
}
protected void LbtnNext_Click(object sender, EventArgs e)
{
this.Context.Session[this.m_UniqueControlPageIndex] = this.PageIndex + 1;
}
protected void LbtnPrev_Click(object sender, EventArgs e)
{
this.Context.Session[this.m_UniqueControlPageIndex] = this.PageIndex - 1;
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Type type = base.GetType();
string webResourceUrl = this.Page.ClientScript.GetWebResourceUrl(type, "PowerEasy.Controls.ExtendedGridView.ExtendedGridView.js");
if (!this.Page.ClientScript.IsClientScriptIncludeRegistered(type, "PowerEasy.Controls.ExtendedGridView.ExtendedGridView.js"))
{
this.Page.ClientScript.RegisterClientScriptInclude(type, "PowerEasy.Controls.ExtendedGridView.ExtendedGridView.js", webResourceUrl);
}
if (this.IsHoldState)
{
this.m_UniqueControlPageSize = this.Page.GetType().Name + "_" + this.UniqueID + "_PageSize";
this.m_UniqueControlPageIndex = this.Page.GetType().Name + "_" + this.UniqueID + "_PageIndex";
if (this.Context.Session[this.m_UniqueControlPageSize] != null)
{
this.PageSize = (int) this.Context.Session[this.m_UniqueControlPageSize];
}
if (this.Context.Session[this.m_UniqueControlPageIndex] != null)
{
this.PageIndex = (int) this.Context.Session[this.m_UniqueControlPageIndex];
this.m_RawPageIndex = this.PageIndex;
}
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (((this.PageCount > 1) && ((this.PageIndex + 1) == this.PageCount)) && ((this.Rows.Count == 0) && base.Initialized))
{
base.RequiresDataBinding = true;
}
if ((this.PageIndex != this.m_RawPageIndex) && (this.Rows.Count == 0))
{
this.PageIndex = 0;
if (base.Initialized)
{
base.RequiresDataBinding = true;
}
}
if (this.AllowPaging && (this.BottomPagerRow != null))
{
this.BottomPagerRow.Visible = true;
if (this.ShowCustomPager)
{
Label label = this.BottomPagerRow.Cells[0].FindControl("LblRowsCount") as Label;
if (label != null)
{
label.Text = this.VirtualItemCount.ToString();
}
}
}
if (this.AutoGenerateCheckBoxColumn)
{
string str = string.Format("{0}_HeaderButton", this.ClientID);
foreach (GridViewRow row in this.Rows)
{
CheckBox box = (CheckBox) row.FindControl("CheckBoxButton");
box.Attributes["onclick"] = string.Concat(new object[] { "CheckItem(this,\"", str, "\",\"", base.RowStyle.CssClass, "\",\"", this.SelectedCssClass, "\",", this.Rows.Count, ")" });
}
}
}
protected override void OnRowCreated(GridViewRowEventArgs e)
{
base.OnRowCreated(e);
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (!string.IsNullOrEmpty(this.MouseOverCssClass))
{
e.Row.Attributes.Add("onmouseover", "MouseOver(this,\"" + this.MouseOverCssClass + "\")");
e.Row.Attributes.Add("onmouseout", "MouseOut(this)");
}
if (this.AutoGenerateSerialColumn)
{
e.Row.Cells[this.SerialColumnIndex].Text = Convert.ToString((int) ((e.Row.DataItemIndex + (this.PageIndex * this.PageSize)) + 1));
}
}
if ((e.Row.RowType == DataControlRowType.Pager) && this.ShowCustomPager)
{
this.CreateCustomPagerRow(e);
}
}
protected override void OnRowDataBound(GridViewRowEventArgs e)
{
base.OnRowDataBound(e);
if ((e.Row.RowType == DataControlRowType.DataRow) && !string.IsNullOrEmpty(this.RowDblclickUrl))
{
if (!string.IsNullOrEmpty(this.RowDblclickBoundField))
{
e.Row.Attributes.Add("ondblclick", "RowDblclick('" + StringHelper.ReplaceIgnoreCase(this.RowDblclickUrl, "{$Field}", DataBinder.Eval(e.Row.DataItem, this.RowDblclickBoundField).ToString()) + "');");
}
else
{
e.Row.Attributes.Add("ondblclick", "RowDblclick('" + this.RowDblclickUrl + "');");
}
}
}
protected override void PerformDataBinding(IEnumerable data)
{
base.PerformDataBinding(data);
this.ViewState["VirtualItemCount"] = this.ViewState["_!ItemCount"];
}
protected override void PrepareControlHierarchy()
{
base.PrepareControlHierarchy();
}
protected void TxtCurrentPage_TextChanged(object sender, EventArgs e)
{
int pageCount;
TextBox box = (TextBox) sender;
if (!int.TryParse(box.Text, out pageCount))
{
pageCount = 1;
}
else if (pageCount < 1)
{
pageCount = 1;
}
if (pageCount > this.PageCount)
{
pageCount = this.PageCount;
}
this.PageIndex = pageCount - 1;
box.Text = pageCount.ToString();
if (this.IsHoldState)
{
this.Context.Session[this.m_UniqueControlPageIndex] = this.PageIndex;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -