Creating a Delete Record Confirmation Class - Demo
<%@ Page Language="VB" %>
<%@ Register TagPrefix="custom" Namespace="DeleteConfirmation" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<style type="text/css">
.grid td, .grid th
{
padding:5px;
}
</style>
<title>Show DeleteButtonField</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView
id="grdMovies"
CssClass="grid"
DataSourceID="srcMovies"
DataKeyNames="Id"
AutoGenerateColumns="false"
Runat="server">
<Columns>
<custom:DeleteButtonField
ConfirmText="Are you sure that you want to delete this record?" />
<asp:BoundField
DataField="Title"
HeaderText="Movie Title" />
<asp:BoundField
DataField="Director"
HeaderText="Movie Director" />
</Columns>
</asp:GridView>
<asp:SqlDataSource
id="srcMovies"
ConnectionString="<%$ ConnectionStrings:MyDatabase %>"
SelectCommand="SELECT Id, Title, Director FROM Movies"
DeleteCommand="DELETE Movies WHERE Id=@Id"
Runat="server" />
</div>
</form>
</body>
</html>
** App_Code/DeleteButtonField.vb **
Imports System
Imports System.Web.UI.WebControls
Namespace DeleteConfirmation
''' <summary>
''' Displays a confirmation before deleting a record
''' </summary>
Public Class DeleteButtonField
Inherits ButtonField
Private _confirmText As String = "Delete this record?"
Public Property ConfirmText() As String
Get
Return _confirmText
End Get
Set(ByVal Value As String)
_confirmText = Value
End Set
End Property
Public Sub New()
Me.CommandName = "Delete"
Me.Text = "Delete"
End Sub
Public Overrides Sub InitializeCell(ByVal cell As DataControlFieldCell, _
ByVal cellType As DataControlCellType, ByVal rowState As DataControlRowState, ByVal rowIndex As Integer)
MyBase.InitializeCell(cell, cellType, rowState, rowIndex)
If cellType = DataControlCellType.DataCell Then
Dim button As WebControl = CType(cell.Controls(0), WebControl)
button.Attributes("onclick") = String.Format("return confirm('{0}');", _confirmText)
End If
End Sub
End Class
End Namespace