Custom Search

Thursday, June 26, 2008

Creating Deleting Warning Button



if we use delete button rendered by the GridView control's CommandField, its doesn’t provide any warning before we delete a record. In this posted, I give example how to creating a delete button that display a confirmation dialog box.

Default.aspx



< %@ Page Language="VB" %>
< %@ Register TagPrefix="custom" Namespace="myControls" %>

< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

< xmlns="http://www.w3.org/1999/xhtml">
< id="Head1" runat="server">
<>Untitled Page< /title>
< /head>
<>
< id="form1" runat="server">
<>
< id="SqlDataSource1" runat="server" connectionstring="">"
SelectCommand="SELECT * FROM [std]" DeleteCommand="delete from std where id=@id">< /asp:SqlDataSource>

< /div>
< id="GridView1" runat="server" autogeneratecolumns="False" cellpadding="4" datasourceid="SqlDataSource1" forecolor="#333333" gridlines="None" datakeynames="id">
< backcolor="#990000" bold="True" forecolor="White">
<>
< confirmtext="Are you sure that you want to delete this record?">

< datafield="name" headertext="name" sortexpression="name">
< /Columns>
< backcolor="#FFFBD6" forecolor="#333333">
< backcolor="#FFCC66" bold="True" forecolor="Navy">
< backcolor="#FFCC66" forecolor="#333333" horizontalalign="Center">
< backcolor="#990000" bold="True" forecolor="White">
< backcolor="White">
< /asp:GridView>
< /form>
< /body>
< /html>

and this is DeleteButtonField class

Imports System
Imports System.Web.UI.WebControls

Namespace myControls
'''
''' Displays a confirmation before deleting a record
'''

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

No comments:

Free Traffic

 Join My Community at MyBloglog! Blog Flux Directory 

Web Developement Blogs - BlogCatalog Blog Directory web development blog  Internet Blogs - Blog Top Sites 

Blog Directory 

Click Here for Free Traffic!

Link2Communion.com