ASP.NET DEMO 8: 为 GridView/DataGrid 整行添加服务器事件

需求说明

GridView/DataGrid 本身均支持行选择事件(通过设置Button/LinkButton.CommandName="Selected",并在 SelectedIndexChanged 事件中处理)。
然而,有时候我们希望用户点击 GridView/DataGrid 一行中任意位置都可以实现触发一个事件,并在服务端对此行进行相应处理,现在我们就实现此功能。

实现方式

这里我们采取的方法有点 "hack" :
通过客户端 javascript 引发行中隐藏的按钮(Button/LinkButton 均可以)的 click 事件。

主要代码

 

None.gif < asp:GridView  ID ="GridView1"  runat ="server"  AutoGenerateColumns ="false"  OnRowCommand ="GridView1_RowCommand"  OnRowDataBound ="GridView1_RowDataBound" >
None.gif            
< Columns >                               
None.gif                
< asp:TemplateField  HeaderText ="ProductName"   >
None.gif                    
< ItemTemplate >
ExpandedBlockStart.gifContractedBlock.gif                        
<% dot.gif Eval("ProductName" %>
None.gif                        
< asp:Button  ID ="btnHiddenPostButton"  CommandName ="HiddenPostButtonCommand"  runat ="server"  Text ="HiddenPostButton"  style ="display:none"   />
None.gif                    
</ ItemTemplate >
None.gif                
</ asp:TemplateField >
None.gif                
< asp:BoundField  DataField ="UnitPrice"  HeaderText ="UnitPrice"   />
None.gif            
</ Columns >
None.gif        
</ asp:GridView >

 

None.gif protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        Button btnHiddenPostButton 
= e.Row.FindControl("btnHiddenPostButton"as Button;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (btnHiddenPostButton != nulldot.gif{
InBlock.gif            e.Row.Attributes[
"onclick"= String.Format("javascript:document.getElementById('{0}').click()", btnHiddenPostButton.ClientID);
InBlock.gif            
// 额外样式定义
InBlock.gif
            e.Row.Attributes["onmouseover"= "javascript:this.style.background='red'";
InBlock.gif            e.Row.Attributes[
"onmouseout"= "javascript:this.style.background=''";
InBlock.gif            e.Row.Attributes[
"style"= "cursor:pointer";
InBlock.gif            e.Row.Attributes[
"title"= "单击选择当前行";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
// 若希望将隐藏按钮单独放于一列,则设置此列隐藏,占位符 <cellIndex> 表示此列索引
InBlock.gif        
//e.Row.Cells[<cellIndex>].Attributes["style"] = "display:none";
ExpandedBlockEnd.gif
    }

None.gif
None.gif    
protected   void  GridView1_RowCommand( object  sender, GridViewCommandEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
int rowIndex = -1;
InBlock.gif        GridViewRow row 
= null;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
switch (e.CommandName) dot.gif{            
InBlock.gif            
case "HiddenPostButtonCommand"// 模板列                
InBlock.gif
                Control cmdControl = e.CommandSource as Control; // 表示触发事件的 IButtonControl,保持统一性并便于后续操作,我们这里直接转化为控件基类 Control
InBlock.gif
                row = cmdControl.NamingContainer as GridViewRow; // 当前行
InBlock.gif                
// 如何访问单元格值
InBlock.gif                
// string txt = row.Cells[0].Text;
InBlock.gif                
// 如何获取模板列中的 Label
InBlock.gif                
// string lbl = row.FindControl("MyLabelID") as Label;
InBlock.gif                
// 执行更多的自定义操作
InBlock.gif                
// dot.gif
InBlock.gif                
// dot.gif
InBlock.gif
                Response.Write(String.Format("GridView Version 当前第 {0} 行:", row.RowIndex + 1));
InBlock.gif                
break;
InBlock.gif            
// case "Command2":
InBlock.gif            
// more cases
InBlock.gif            
// dot.gif                
ExpandedSubBlockEnd.gif
        }

ExpandedBlockEnd.gif    }

None.gif



测试效果



源码下载 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值