Custom Delete Button in GridView having confirming function.

本文介绍如何在ASP.NET中为GridView控件添加自定义的删除按钮,并通过客户端JavaScript确认框来确认删除操作。通过继承ButtonField并重写InitializeCell方法实现功能。

Add the follow to web.config

        <pages>
            <controls>

<!--custom defined tag prefix for extending delete function of gridview to have confirming function -->
                <add tagPrefix="custom" namespace="CustomControl"/>

            </controls>
        </pages>

 

ContractedBlock.gifExpandedBlockStart.gifCode
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace CustomControl
{
    
public class DeleteButtonField:ButtonField
    {
        
private String ConfirmMsg = "If you want to delete?";
        
public DeleteButtonField()
        {
            
this.CommandName = "Delete";
            
this.Text = "Delete";
        }
        
public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            
base.InitializeCell(cell, cellType, rowState, rowIndex);
            
if (cellType == DataControlCellType.DataCell)
            {
                WebControl wc 
= cell.Controls[0as WebControl;
                wc.Attributes[
"onclick"= String.Format("return confirm('{0}');", ConfirmMsg);
            }
        }
    }
}

 

and use it like

ContractedBlock.gifExpandedBlockStart.gifCode
<asp:GridView ID="GridView1" runat="server">
    
<Columns>
        
<custom:DeleteButtonField></custom:DeleteButtonField>
    
</Columns>
    
</asp:GridView>

 

转载于:https://www.cnblogs.com/jerryhong/archive/2009/02/05/1384590.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值