自定义GridView控件(WebBarGridView)
网摘:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Drawing;
using System.ComponentModel;
using System.Web.UI.HtmlControls;
namespace WebBar.Controls
{
public class WebBarGridView : GridView
{
private HtmlInputCheckBox cb;
private Button HideButton = new Button();
[Browsable(true)]
[Description("双击时激发")]
public event EventHandler DblClick;
#region 自定义属性
[Browsable(true)]
[Category("Appearance")]
[Description("隔行背景色(单号行的背景)")]
public Color SingleBackGroundColor
{
get
{
return ViewState["SingleBackGroundColor"] != null ? (Color)ViewState["SingleBackGroundColor"] : Color.Empty;
}
set
{
ViewState["SingleBackGroundColor"] = value;
}
}
[Browsable(true)]
[Category("Appearance")]
[Description("隔行背景色(双号行的背景)")]
public Color DoubleBackGroundColor
{
get
{
return ViewState["DoubleBackGroundColor"] != null ? (Color)ViewState["DoubleBackGroundColor"] : Color.Empty;
}
set
{
ViewState["DoubleBackGroundColor"] = value;
}
}
[Browsable(true)]
[Category("Appearance")]
[Description("选中行背景色")]
public Color ClickBackGroundColor
{
get
{
return ViewState["ClickBackGroundColor"] != null ? (Color)ViewState["ClickBackGroundColor"] : Color.Empty;
}
set
{
ViewState["ClickBackGroundColor"] = value;
}
}
[Browsable(true)]
[Category("Appearance")]
[Description("鼠标经过时背景色")]
public Color MouseOverColor
{
get
{
return ViewState["MouseOverColor"] != null ? (Color)ViewState["MouseOverColor"] : Color.Empty;
}
set
{
ViewState["MouseOverColor"] = value;
}
}
[Description("双击事件后取出该行的行号")]
public int SelectRowIndex
{
get
{
int index = -1;
try
{
HtmlInputHidden hidden = (HtmlInputHidden)this.FindControl("hdSelectedRowId");
index = int.Parse(hidden.Value);
}
catch
{
index = -1;
}
return index;
}
set
{
try
{
HtmlInputHidden hidden = (HtmlInputHidden)this.FindControl("hdSelectedRowId");
hidden.Value = value.ToString();
}
catch
{
base.SelectedIndex = value;
}
}
}
#endregion
#region 自定义方法
/// <summary>
/// 返回选中的选号数组
/// </summary>
/// <returns>string[]</returns>
public string[] SelectRows()
{
string selectIndex = "";
foreach (GridViewRow row in this.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
HtmlInputCheckBox cb = (HtmlInputCheckBox)row.Cells[0].FindControl("cb" + row.RowIndex.ToString());
if (cb == null) continue;
if (cb.Checked)
{
selectIndex += "," + cb.Value;
}
}
}
if (selectIndex!="") { selectIndex = selectIndex.Substring(1); } else { return null; }
string[] re = selectIndex.Split(char.Parse(","));
return re;
}
protected void CreateSelectedRowItems()
{
if (this.Rows.Count > 0)
{
HtmlInputHidden hdSelectedRowId = (HtmlInputHidden)this.FindControl("hdSelectedRowId");
if (hdSelectedRowId == null)
{
hdSelectedRowId = new HtmlInputHidden();
hdSelectedRowId.ID = "hdSelectedRowId";
this.Controls.Add(hdSelectedRowId);
}
if (((Button)this.FindControl("HideButton")) == null)
{
this.HideButton.ID = "HideButton";
this.HideButton.Width = 0;
this.Controls.Add(this.HideButton);
}
}
}
private void OnClick(object sender,EventArgs e)
{
try
{
this.DblClick(this, e);
}
catch
{
}
}
#endregion
/// <summary>
/// 重写初实化事件处理方法
/// </summary>
/// <param name="e"></param>
protected override void OnInit(EventArgs e)
{
this.HideButton.Click += new EventHandler(this.OnClick);
TemplateField gc = new TemplateField();
gc.HeaderText = "<input type='checkbox' id='cbAll' onclick='checkAll(this.form);' />全选";
this.Columns.Insert(0, gc);
base.OnInit(e);
}
protected override void OnRowCreated(GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
cb = new HtmlInputCheckBox();
cb.ID = "cb" + e.Row.RowIndex.ToString();
cb.Value = e.Row.RowIndex.ToString();
//cb.Attributes["onclick"] = "checkOne('" + r.ClientID.ToString() + "_cb" + r.RowIndex.ToString() + "','" + r.ClientID.ToString() + "','" + (r.RowIndex % 2).ToString() + "');";
//cb.Attributes["onclick"] = "checkOne('" + this.ID + "_ctl" + (e.Row.RowIndex + 2).ToString("00") + "_cb" + e.Row.RowIndex.ToString() + "','" + this.ID + "_ctl" + (e.Row.RowIndex + 2).ToString("00") + "','" + (e.Row.RowIndex % 2).ToString() + "');";
//cb.Attributes["onclick"] = "checkOne('" + this.ID + "_ctl" + (e.Row.RowIndex + 2).ToString("00") + "_cb" + e.Row.RowIndex.ToString() + "',this.parent,'" + (e.Row.RowIndex % 2).ToString() + "');";
cb.Attributes["onclick"] = "checkOneByBoxClick(this,this.parent,'" + (e.Row.RowIndex % 2).ToString() + "');";
e.Row.Cells[0].Controls.Add(cb);
}
base.OnRowCreated(e);
}
/// <summary>
/// 重写行数据绑定事件处理方法
/// </summary>
/// <param name="e"></param>
protected override void OnRowDataBound(GridViewRowEventArgs e)
{
if (SingleBackGroundColor != null)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex != -1)
{
//e.Row.Attributes["onmouseover"] = "javascript:this.style.background='" + ColorTranslator.ToHtml(MouseOverColor) + "'";
//e.Row.Attributes["onmouseout"] = "onmouseoutchangestyle('" + e.Row.ClientID.ToString() + "_cb" + e.Row.RowIndex.ToString() + "','" + e.Row.ClientID.ToString() + "');";
//e.Row.Attributes["onclick"] = "checkOne('"+e.Row.ClientID.ToString()+"_cb"+e.Row.RowIndex.ToString()+"','"+e.Row.ClientID.ToString()+"','"+(e.Row.RowIndex % 2).ToString()+"');";
e.Row.Attributes["onclick"] = "checkOne('" + e.Row.ClientID.ToString() + "_cb" + e.Row.RowIndex.ToString() + "',this,'" + (e.Row.RowIndex % 2).ToString() + "');";
e.Row.Attributes["ondblclick"] = "dblClick('" + e.Row.RowIndex.ToString() + "','" + this.ClientID + "_hdSelectedRowId')";
if (e.Row.RowIndex % 2 == 0) { e.Row.BackColor = this.SingleBackGroundColor; } else { e.Row.BackColor = this.DoubleBackGroundColor; }
//e.Row.Attributes.Add("onClick", "javascript:__doPostBack('" + this.ClientID + "','Select$" + e.Row.RowIndex + "');");
}
}
}
base.OnRowDataBound(e);
}
protected override void OnLoad(EventArgs e)
{
this.CreateSelectedRowItems();
base.OnLoad(e);
}
protected override void OnPreRender(EventArgs e)
{
this.CreateSelectedRowItems();
string js = @"<script type='text/javascript'>
function checkOne(checkBoxId,row,rowIndex)
{
if(document.getElementById(checkBoxId)==null){return;}
if(document.getElementById(checkBoxId).checked==true)
{
document.getElementById(checkBoxId).checked=false;
if(rowIndex=='1')
{
row.style.backgroundColor='" + ColorTranslator.ToHtml(DoubleBackGroundColor) + @"';
}
else
{
row.style.backgroundColor='" + ColorTranslator.ToHtml(SingleBackGroundColor) + @"';
}
}
else
{
document.getElementById(checkBoxId).checked=true;
row.style.backgroundColor='" + ColorTranslator.ToHtml(ClickBackGroundColor) + @"';
}
}
function checkOneByBoxClick(checkBox,row,rowIndex)
{
if(checkBox==null){return;}
if(checkBox.checked==true)
{
checkBox.checked=false;
}
else
{
checkBox.checked=true;
}
}
function checkAll(form)
{
for (var i=0;i<form.elements.length;i++)
{
var e = form.elements[i];
if (e.name != 'cbAll')
{
e.checked = form.cbAll.checked;
}
}
}
function dblClick(rowIndex,objHiddenId)
{
document.getElementById(objHiddenId).value=rowIndex;
__doPostBack('" + this.ID + @":HideButton','');
}
</script>";
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ClientScript", js);
base.OnPreRender(e);
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Drawing;
using System.ComponentModel;
using System.Web.UI.HtmlControls;
namespace WebBar.Controls
{
public class WebBarGridView : GridView
{
private HtmlInputCheckBox cb;
private Button HideButton = new Button();
[Browsable(true)]
[Description("双击时激发")]
public event EventHandler DblClick;
#region 自定义属性
[Browsable(true)]
[Category("Appearance")]
[Description("隔行背景色(单号行的背景)")]
public Color SingleBackGroundColor
{
get
{
return ViewState["SingleBackGroundColor"] != null ? (Color)ViewState["SingleBackGroundColor"] : Color.Empty;
}
set
{
ViewState["SingleBackGroundColor"] = value;
}
}
[Browsable(true)]
[Category("Appearance")]
[Description("隔行背景色(双号行的背景)")]
public Color DoubleBackGroundColor
{
get
{
return ViewState["DoubleBackGroundColor"] != null ? (Color)ViewState["DoubleBackGroundColor"] : Color.Empty;
}
set
{
ViewState["DoubleBackGroundColor"] = value;
}
}
[Browsable(true)]
[Category("Appearance")]
[Description("选中行背景色")]
public Color ClickBackGroundColor
{
get
{
return ViewState["ClickBackGroundColor"] != null ? (Color)ViewState["ClickBackGroundColor"] : Color.Empty;
}
set
{
ViewState["ClickBackGroundColor"] = value;
}
}
[Browsable(true)]
[Category("Appearance")]
[Description("鼠标经过时背景色")]
public Color MouseOverColor
{
get
{
return ViewState["MouseOverColor"] != null ? (Color)ViewState["MouseOverColor"] : Color.Empty;
}
set
{
ViewState["MouseOverColor"] = value;
}
}
[Description("双击事件后取出该行的行号")]
public int SelectRowIndex
{
get
{
int index = -1;
try
{
HtmlInputHidden hidden = (HtmlInputHidden)this.FindControl("hdSelectedRowId");
index = int.Parse(hidden.Value);
}
catch
{
index = -1;
}
return index;
}
set
{
try
{
HtmlInputHidden hidden = (HtmlInputHidden)this.FindControl("hdSelectedRowId");
hidden.Value = value.ToString();
}
catch
{
base.SelectedIndex = value;
}
}
}
#endregion
#region 自定义方法
/// <summary>
/// 返回选中的选号数组
/// </summary>
/// <returns>string[]</returns>
public string[] SelectRows()
{
string selectIndex = "";
foreach (GridViewRow row in this.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
HtmlInputCheckBox cb = (HtmlInputCheckBox)row.Cells[0].FindControl("cb" + row.RowIndex.ToString());
if (cb == null) continue;
if (cb.Checked)
{
selectIndex += "," + cb.Value;
}
}
}
if (selectIndex!="") { selectIndex = selectIndex.Substring(1); } else { return null; }
string[] re = selectIndex.Split(char.Parse(","));
return re;
}
protected void CreateSelectedRowItems()
{
if (this.Rows.Count > 0)
{
HtmlInputHidden hdSelectedRowId = (HtmlInputHidden)this.FindControl("hdSelectedRowId");
if (hdSelectedRowId == null)
{
hdSelectedRowId = new HtmlInputHidden();
hdSelectedRowId.ID = "hdSelectedRowId";
this.Controls.Add(hdSelectedRowId);
}
if (((Button)this.FindControl("HideButton")) == null)
{
this.HideButton.ID = "HideButton";
this.HideButton.Width = 0;
this.Controls.Add(this.HideButton);
}
}
}
private void OnClick(object sender,EventArgs e)
{
try
{
this.DblClick(this, e);
}
catch
{
}
}
#endregion
/// <summary>
/// 重写初实化事件处理方法
/// </summary>
/// <param name="e"></param>
protected override void OnInit(EventArgs e)
{
this.HideButton.Click += new EventHandler(this.OnClick);
TemplateField gc = new TemplateField();
gc.HeaderText = "<input type='checkbox' id='cbAll' onclick='checkAll(this.form);' />全选";
this.Columns.Insert(0, gc);
base.OnInit(e);
}
protected override void OnRowCreated(GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
cb = new HtmlInputCheckBox();
cb.ID = "cb" + e.Row.RowIndex.ToString();
cb.Value = e.Row.RowIndex.ToString();
//cb.Attributes["onclick"] = "checkOne('" + r.ClientID.ToString() + "_cb" + r.RowIndex.ToString() + "','" + r.ClientID.ToString() + "','" + (r.RowIndex % 2).ToString() + "');";
//cb.Attributes["onclick"] = "checkOne('" + this.ID + "_ctl" + (e.Row.RowIndex + 2).ToString("00") + "_cb" + e.Row.RowIndex.ToString() + "','" + this.ID + "_ctl" + (e.Row.RowIndex + 2).ToString("00") + "','" + (e.Row.RowIndex % 2).ToString() + "');";
//cb.Attributes["onclick"] = "checkOne('" + this.ID + "_ctl" + (e.Row.RowIndex + 2).ToString("00") + "_cb" + e.Row.RowIndex.ToString() + "',this.parent,'" + (e.Row.RowIndex % 2).ToString() + "');";
cb.Attributes["onclick"] = "checkOneByBoxClick(this,this.parent,'" + (e.Row.RowIndex % 2).ToString() + "');";
e.Row.Cells[0].Controls.Add(cb);
}
base.OnRowCreated(e);
}
/// <summary>
/// 重写行数据绑定事件处理方法
/// </summary>
/// <param name="e"></param>
protected override void OnRowDataBound(GridViewRowEventArgs e)
{
if (SingleBackGroundColor != null)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex != -1)
{
//e.Row.Attributes["onmouseover"] = "javascript:this.style.background='" + ColorTranslator.ToHtml(MouseOverColor) + "'";
//e.Row.Attributes["onmouseout"] = "onmouseoutchangestyle('" + e.Row.ClientID.ToString() + "_cb" + e.Row.RowIndex.ToString() + "','" + e.Row.ClientID.ToString() + "');";
//e.Row.Attributes["onclick"] = "checkOne('"+e.Row.ClientID.ToString()+"_cb"+e.Row.RowIndex.ToString()+"','"+e.Row.ClientID.ToString()+"','"+(e.Row.RowIndex % 2).ToString()+"');";
e.Row.Attributes["onclick"] = "checkOne('" + e.Row.ClientID.ToString() + "_cb" + e.Row.RowIndex.ToString() + "',this,'" + (e.Row.RowIndex % 2).ToString() + "');";
e.Row.Attributes["ondblclick"] = "dblClick('" + e.Row.RowIndex.ToString() + "','" + this.ClientID + "_hdSelectedRowId')";
if (e.Row.RowIndex % 2 == 0) { e.Row.BackColor = this.SingleBackGroundColor; } else { e.Row.BackColor = this.DoubleBackGroundColor; }
//e.Row.Attributes.Add("onClick", "javascript:__doPostBack('" + this.ClientID + "','Select$" + e.Row.RowIndex + "');");
}
}
}
base.OnRowDataBound(e);
}
protected override void OnLoad(EventArgs e)
{
this.CreateSelectedRowItems();
base.OnLoad(e);
}
protected override void OnPreRender(EventArgs e)
{
this.CreateSelectedRowItems();
string js = @"<script type='text/javascript'>
function checkOne(checkBoxId,row,rowIndex)
{
if(document.getElementById(checkBoxId)==null){return;}
if(document.getElementById(checkBoxId).checked==true)
{
document.getElementById(checkBoxId).checked=false;
if(rowIndex=='1')
{
row.style.backgroundColor='" + ColorTranslator.ToHtml(DoubleBackGroundColor) + @"';
}
else
{
row.style.backgroundColor='" + ColorTranslator.ToHtml(SingleBackGroundColor) + @"';
}
}
else
{
document.getElementById(checkBoxId).checked=true;
row.style.backgroundColor='" + ColorTranslator.ToHtml(ClickBackGroundColor) + @"';
}
}
function checkOneByBoxClick(checkBox,row,rowIndex)
{
if(checkBox==null){return;}
if(checkBox.checked==true)
{
checkBox.checked=false;
}
else
{
checkBox.checked=true;
}
}
function checkAll(form)
{
for (var i=0;i<form.elements.length;i++)
{
var e = form.elements[i];
if (e.name != 'cbAll')
{
e.checked = form.cbAll.checked;
}
}
}
function dblClick(rowIndex,objHiddenId)
{
document.getElementById(objHiddenId).value=rowIndex;
__doPostBack('" + this.ID + @":HideButton','');
}
</script>";
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ClientScript", js);
base.OnPreRender(e);
}
}
}