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

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

系統 1773 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條評論
主站蜘蛛池模板: 色视频网站在线观看 | 日本视频播放免费线上观看 | 久久久久亚洲国产 | 日韩亚洲一区二区三区 | 性刺激的欧美三级视频 | 九草在线免费观看 | 伊人一道本 | 欧美综合图片区 | 老司机深夜影院入口aaaa | 欧美兽皇另类 | 久久精品只有这里有 | 九九精品久久久久久噜噜 | 久热爱免费精品视频在线播放 | 国产一级特黄高清在线大片 | 综合色99| 久青草国产高清在线视频 | 久操福利视频 | 亚洲乱码一区二区三区在线观看 | 亚洲国产成人久久三区 | 精品国产一区二区二三区在线观看 | 欧美一区二区三区免费视频 | 一区二区三区免费在线观看 | 日韩精品久久久毛片一区二区 | 97精品视频在线观看 | 亚洲不卡视频在线观看 | 四虎高清成人永久免费影院 | 亚洲国产精品日韩在线 | 亚洲精品一区二区三区四区手机版 | 国外成人免费高清激情视频 | 欧美黄色网页 | 成人免费aa在线观看 | 国产草| 欧美一区二区精品 | 国产高清区 | 高清中文字幕免费观在线 | 国产日韩欧美精品在线 | 国产精品美女久久久久 | 亚洲欧美国产一区二区三区 | 日本一级毛片2021免费 | 久久精品视频1 | 日本老年人精品久久中文字幕 |