亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

GridView/DataGrid 整行添加服務器事件

系統 1566 0

需求說明

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

實現方式

這里我們采取的方法有點 "hack" :
通過客戶端 javascript 引發行中隱藏的按鈕(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 " ] = " 單擊選擇當前行 " ;
}

// 若希望將隱藏按鈕單獨放于一列,則設置此列隱藏,占位符<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; // 表示觸發事件的IButtonControl,保持統一性并便于后續操作,我們這里直接轉化為控件基類Control
row = cmdControl.NamingContainer as GridViewRow; // 當前行
// 如何訪問單元格值
// stringtxt=row.Cells[0].Text;
// 如何獲取模板列中的Label
// stringlbl=row.FindControl("MyLabelID")asLabel;
// 執行更多的自定義操作
//
//
Response.Write(String.Format( " GridViewVersion當前第{0}行: " ,row.RowIndex + 1 ));
break ;
// case"Command2":
// morecases
//
}

}



測試效果

GridView/DataGrid 整行添加服務器事件

GridView/DataGrid 整行添加服務器事件


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲一区中文字幕在线 | 久久99亚洲精品久久久久 | 欧美与黑人午夜性猛交久久久 | 久操青青 | 国产精品一级香蕉一区 | 狠狠色丁香婷婷综合小时婷婷 | 亚洲国产日韩欧美mv | 中文字幕视频在线观看 | 日韩不卡在线 | 欧美精品四虎在线观看 | 超清中文乱码字幕在线观看 | 国产色婷婷免费视频 | 美女视频很黄很黄又免费的 | 日本道在线视频 | 免费视频一区二区性色 | 国产精品第二页在线播放 | 日日爱夜夜操 | 99国产精品 | 国产日韩欧美精品一区二区三区 | 亚洲免费视频播放 | 奇米影视色 | 成人在线一区二区 | 99婷婷久久精品国产一区二区 | 成年女人免费视频播放77777 | 欧美日韩久久中文字幕 | 天天色综网 | 99久久精品国产9999高清 | 久久国产影院 | 中文字幕第5页 | 精品中文字幕久久久久久 | 久久久精品视频免费观看 | 九九免费精品视频在这里 | 久操综合| 色综网| 99精品国产一区二区三区 | 国产高清天干天天视频 | 精品在线免费观看视频 | 中文字幕亚洲精品久久 | a毛片免费 | 天天擦天天干 | 国产色婷婷免费视频 |