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

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精品热线在线观看免费视频 | 国产成人精品一区二区免费 | 91在线免费公开视频 | 久久91av| 色综合久久中文字幕综合网 | 110139日韩欧美 | 国产a久久精品一区二区三区 | 五月天婷婷在线观看高清 | 国产精品久久久久久久午夜片 | 亚洲精品一区专区 | 国产精品成人第一区 | 成人亚洲视频在线观看 | 欧美一级欧美三级 | 国产高清免费午夜在线视频 | 成人特黄午夜性a一级毛片 成人特级毛片69免费观看 | 性欧美暴力猛交xxxxx高清 | 日韩欧美视频一区二区三区 | 国产亚洲精品热视频在线观看 | 久久综合九色综合国产 | 亚洲精品久久九九热 | 久久国产热这里只有精品 | 成人性色生活片免费网 | 久久久久久久免费 | 视频二区 中文字幕 欧美 | 国产乱子伦 | 成人网影| 另类最猛性xxxxx | 精品国产日韩亚洲一区二区 | 国产精品成人免费 | 国产91系列| 欧美亚洲综合一区 | 高清国产一区二区 | 精品无人区乱码1区2区 | 久久久综合九色合综国产 | 风流一代在线播放 | 免费看黄色片视频 | 99久久久国产精品免费播放器 | 亚洲欧洲精品视频 | 中文在线亚洲 | 中文字幕亚洲综合久久2 | 欧美干干 |