轉(zhuǎn)帖|其它|編輯:郝浩|2010-11-25 14:27:10.000|閱讀 614 次
概述:我們?cè)谑褂肎ridView的時(shí)候,很多時(shí)候需要使用CheckBox列,比如批量刪除,批量審批,但是每每都需要記住繁瑣的實(shí)現(xiàn)方法。多麻煩呀!本文將手把手教你如何擴(kuò)展GridView之自帶CheckBox ,希望對(duì)大家有幫助。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
我們?cè)谑褂肎ridView的時(shí)候,很多時(shí)候需要使用CheckBox列,比如批量刪除,批量審批,但是每每都需要記住繁瑣的實(shí)現(xiàn)方法。多麻煩呀!本文將手把手教你如何擴(kuò)展GridView之自帶CheckBox ,希望對(duì)大家有幫助。
下面談?wù)勎疫@實(shí)現(xiàn)的思路:
因?yàn)镚ridView是基于模板的,Columns也不能在后臺(tái)添加,所以排除通過(guò)添加Column來(lái)實(shí)現(xiàn),而采用在GridView創(chuàng)建行的時(shí)候動(dòng)態(tài)創(chuàng)建表單元格,在表頭行上添加一個(gè)全選CheckBox,數(shù)據(jù)行上添加選擇框,點(diǎn)擊全選,通過(guò)向頁(yè)面注冊(cè)的腳本來(lái)實(shí)現(xiàn)全選。
下面就看看關(guān)鍵的代碼:
為了增加靈活性,添加了一個(gè)屬性,控制是否顯示CheckBox列
//是否顯示全選
[
Description("顯示全選列"),
Category("擴(kuò)展"),
DefaultValue(false)
]
public virtual bool ShowCheckAll
{
get
{
object obj2 = this.ViewState["ShowCheckAll"];
if (obj2 != null)
{
return (bool)obj2;
}
return false;
}
set
{
bool aShowCheckAll = this.ShowCheckAll;
if (value != aShowCheckAll)
{
this.ViewState["ShowCheckAll"] = value;
if (base.Initialized)
{
base.RequiresDataBinding = true;
}
}
}
}
用于控制選擇列是添加到表的左端還是右端的屬性
public enum CheckColumnAlign
{
Left, Right
}
//是否顯示全選
[
Description("全選列的位置"),
Category("擴(kuò)展"),
DefaultValue(CheckColumnAlign.Left)
]
public virtual CheckColumnAlign CheckColumnAlign
{
get
{
object obj2 = this.ViewState["CheckColumnAlign"];
if (obj2 != null)
{
return (CheckColumnAlign)obj2;
}
return CheckColumnAlign.Left;
}
set
{
CheckColumnAlign aCheckColumnAlign = this.CheckColumnAlign;
if (value != aCheckColumnAlign)
{
this.ViewState["CheckColumnAlign"] = value;
if (base.Initialized)
{
base.RequiresDataBinding = true;
}
}
}
}
在頁(yè)面加載的時(shí)候,注冊(cè)全選腳本
StringBuilder sb = new StringBuilder();
sb.Append(" <script type=\"text/javascript\">");
sb.Append("function CheckAll(oCheckbox)");
sb.Append("{");
sb.Append("var GridView2 = document.getElementById(\"" + this.ClientID + "\");");
sb.Append(" for(i = 1;i < GridView2.rows.length; i++)");
sb.Append("{");
sb.Append("var inputArray = GridView2.rows[i].getElementsByTagName(\"INPUT\");");
sb.Append("for(var j=0;j<inputArray.length;j++)");
sb.Append("{ if(inputArray[j].type=='checkbox')");
sb.Append("{if(inputArray[j].id.indexOf('ItemCheckBox',0)>-1){inputArray[j].checked =oCheckbox.checked; }} }");
sb.Append("}");
sb.Append(" }");
sb.Append("</script>");
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "CheckFun", sb.ToString(), false);
在GridView的RowCreate事件中,添加如下代碼,用于創(chuàng)建CheckBox列
if (ShowCheckAll)
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.Header)
{
TableCell cell = new TableCell();
cell.Wrap = Wrap;
cell.Width = Unit.Pixel(50);
cell.Text = " <input id='Checkbox2' type='checkbox' onclick='CheckAll(this)'/><label>全選</label>";
if (CheckColumnAlign == CheckColumnAlign.Left)
{
row.Cells.AddAt(0, cell);
}
else
{
int index = row.Cells.Count;
row.Cells.AddAt(index, cell);
}
}
else if (row.RowType == DataControlRowType.DataRow)
{
TableCell cell = new TableCell();
cell.Wrap = Wrap;
CheckBox cb = new CheckBox();
cell.Width = Unit.Pixel(50);
cb.ID = "ItemCheckBox";
cell.Controls.Add(cb);
if (CheckColumnAlign == CheckColumnAlign.Left)
{
row.Cells.AddAt(0, cell);
}
else
{
int index = row.Cells.Count;
row.Cells.AddAt(index, cell);
}
}
}
用于記錄CheckBox的ID的屬性
public string CheckBoxID
{
get
{
return "ItemCheckBox";
}
}
使用的時(shí)候,只需要設(shè)置擴(kuò)展GridView的ShowCheckAll=True,設(shè)置CheckColumnAlign為L(zhǎng)eft,CheckBox列在最左邊,Right在最右面,注意因?yàn)闆](méi)有添加Columns,所以Columns并沒(méi)有因?yàn)橐驗(yàn)樘砑恿薈heckBox列而變化,在Column的索引中,CheckBox列不在計(jì)算范圍。
在擴(kuò)展的GridView的OnRowDataBound事件中,就可以通過(guò)
CheckBox cb = e.Row.FindControl(gridView.CheckBoxID) as CheckBox;
if(cb!=null)
{
if(cb.Checked)
{
//...
}
}
來(lái)獲取是否已經(jīng)選中此行。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:博客轉(zhuǎn)載