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

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條評論
主站蜘蛛池模板: 亚洲精品在线观看视频 | 4hu最新网址 | 国产日韩欧美精品在线 | 色综合久久亚洲国产日韩 | 福利网站在线播放 | 日本特级毛片 | 天天干夜夜草 | 国产精品18久久久久久久久久 | 色综合综合 | 亚洲一区二区三区久久久久 | 午夜男人 | 国产日韩在线 | 国产精品毛片一区 | 欧美又粗又硬 | 天天射天天舔 | 俺来也欧美亚洲a∨在线 | 国产高清视频在线 | 97免费视频在线观看 | 久草丁香 | 在线播放精品一区二区啪视频 | 久久精品国产400部免费看 | 我要看免费一级毛片 | 97se亚洲| 久久国产精品久久国产精品 | 国产成人精品综合网站 | 日韩精品一区二区三区视频 | 亚洲一区二区三区不卡在线播放 | 国产综合视频在线 | 激情在线观看视频 | 久久久久久毛片免费播放 | 久草视频免费 | 天天干视频 | 九天玄帝诀在线观看 | 亚洲精品视频免费看 | 精品91精品91精品国产片 | 中文字幕在线日本 | 久久精品国产亚洲综合色 | 国内精品免费 | 亚洲福利一区二区三区 | 女人用粗大自熨喷水在线视频 | 国产精品亚洲欧美大片在线看 |