日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

GridView/DataGrid 整行添加服務(wù)器事件

系統(tǒng) 1780 0

需求說明

GridView/DataGrid 本身均支持行選擇事件(通過設(shè)置Button/LinkButton.CommandName="Selected",并在 SelectedIndexChanged 事件中處理)。
然而,有時候我們希望用戶點擊 GridView/DataGrid 一行中任意位置都可以實現(xiàn)觸發(fā)一個事件,并在服務(wù)端對此行進行相應(yīng)處理,現(xiàn)在我們就實現(xiàn)此功能。

實現(xiàn)方式

這里我們采取的方法有點 "hack" :
通過客戶端 javascript 引發(fā)行中隱藏的按鈕(Button/LinkButton 均可以)的 click 事件。

主要代碼

< asp:GridView ID ="GridView1" runat ="server" AutoGenerateColumns ="false" OnRowCommand ="GridView1_RowCommand" OnRowDataBound ="GridView1_RowDataBound" >
< Columns >
< asp:TemplateField HeaderText ="ProductName" >
< ItemTemplate >
<% # Eval ( " ProductName " ) %>
< asp:Button ID ="btnHiddenPostButton" CommandName ="HiddenPostButtonCommand" runat ="server" Text ="HiddenPostButton" style ="display:none" />
</ ItemTemplate >
</ asp:TemplateField >
< asp:BoundField DataField ="UnitPrice" HeaderText ="UnitPrice" />
</ Columns >
</ asp:GridView >
protected void GridView1_RowDataBound( object sender,GridViewRowEventArgse)
{
ButtonbtnHiddenPostButton
= e.Row.FindControl( " btnHiddenPostButton " ) as Button;
if (btnHiddenPostButton != null ) {
e.Row.Attributes[
" onclick " ] = String.Format( " javascript:document.getElementById('{0}').click() " ,btnHiddenPostButton.ClientID);
// 額外樣式定義
e.Row.Attributes[ " onmouseover " ] = " javascript:this.style.background='red' " ;
e.Row.Attributes[
" onmouseout " ] = " javascript:this.style.background='' " ;
e.Row.Attributes[
" style " ] = " cursor:pointer " ;
e.Row.Attributes[
" title " ] = " 單擊選擇當(dāng)前行 " ;
}

// 若希望將隱藏按鈕單獨放于一列,則設(shè)置此列隱藏,占位符<cellIndex>表示此列索引
// e.Row.Cells[<cellIndex>].Attributes["style"]="display:none";
}


protected void GridView1_RowCommand( object sender,GridViewCommandEventArgse)
{
int rowIndex = - 1 ;
GridViewRowrow
= null ;
switch (e.CommandName) {
case " HiddenPostButtonCommand " : // 模板列
ControlcmdControl = e.CommandSource as Control; // 表示觸發(fā)事件的IButtonControl,保持統(tǒng)一性并便于后續(xù)操作,我們這里直接轉(zhuǎn)化為控件基類Control
row = cmdControl.NamingContainer as GridViewRow; // 當(dāng)前行
// 如何訪問單元格值
// stringtxt=row.Cells[0].Text;
// 如何獲取模板列中的Label
// stringlbl=row.FindControl("MyLabelID")asLabel;
// 執(zhí)行更多的自定義操作
//
//
Response.Write(String.Format( " GridViewVersion當(dāng)前第{0}行: " ,row.RowIndex + 1 ));
break ;
// case"Command2":
// morecases
//
}

}



測試效果

GridView/DataGrid 整行添加服務(wù)器事件

GridView/DataGrid 整行添加服務(wù)器事件


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 封丘县| 大庆市| 吴堡县| 娱乐| 芒康县| 集安市| 佳木斯市| 前郭尔| 阜康市| 长岭县| 阳信县| 衡水市| 龙南县| 高唐县| 鲁山县| 高清| 潮州市| 无为县| 汉川市| 辽阳县| 巴彦县| 黔江区| 娄底市| 西乌| 竹溪县| 望江县| 获嘉县| 巨鹿县| 齐齐哈尔市| 兴海县| 台东县| 措美县| 金沙县| 高邑县| 寿光市| 平乡县| 和龙市| 大竹县| 景德镇市| 长子县| 平南县|