asp.net中如何给按钮添加事件

 ButtonType obj = (ButtonType) e.Row.Cells[commandFieldIndex].Controls[controlIndex];

ButtonType是被CommandField使用的一种按钮类型,可以是Button,LinkButton,或者ImageButton。默认情况下,CommandField使用LinkButton,但也可以通过CommandField的ButtonType属性来定制。CommandFieldIndex是CommandField在GridView中Columns集合中的原始索引,而controlIndex是删除按钮在CommandField的Controls集合中的索引。controlIndex的值由按钮在CommandField中和其他按钮的相对位置决定。例如,如果在CommandField中只有一个删除按钮,那么它的索引就是0。然而,如果在删除按钮前面还有一个编辑按钮,那么索引值是2。因为在删除按钮前面有两个控件,一个是编辑按钮,另一个是LiteralControl,用来隔离编辑按钮和删除按钮。

使用TemplateFields

ButtonType obj = (ButtonType) e.Row.FindControl("controlID");

< asp:TemplateColumn HeaderText = " 删除 " >
                                                
< ItemTemplate >
                                                    
< asp:ImageButton ID = " delete "  Runat = " server "  CommandName = " DelInfo "  ImageAlign = " Middle "  ImageUrl = " Images/btn_del.gif " ></ asp:ImageButton >
                                                
</ ItemTemplate >
  
</ asp:TemplateColumn >

private   void  DataGrid_ItemDataBound( object  sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        
{
            
if (e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
            
{
                DataRowView drv
=(DataRowView)e.Item.DataItem;
            
                ImageButton l
=(ImageButton)e.Item.Cells[4].FindControl("delete");
                l.Attributes.Add(
"onclick","javascript:return confirm('你确认要删除"+drv.Row["SName"].ToString()+"吗?')");
            }

        }

注意:"SName"确认中要添加确认数据的列名,此例中用的是ImageButton其它按钮与此类似。

再说2.0的GridView,添加GridView的RowDataBound事件并添加以下类似代码

< asp:CommandField ShowDeleteButton = " True "   />

 

protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowType == DataControlRowType.DataRow)
            e.Row.Cells[
10].Attributes.Add("onclick""javascript:return confirm('你确认要删除"" + e.Row.Cells[1].Text + ""吗?')");

    }

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值