DetailsViewDeletedEventHandler Delegate
Represents the method that handles the ItemDeleted event of a DetailsView control.
Assembly: System.Web (in System.Web.dll)
Public Delegate Sub DetailsViewDeletedEventHandler ( sender As Object, e As DetailsViewDeletedEventArgs )
Parameters
- sender
-
Type:
System.Object
The source of the event.
- e
-
Type:
System.Web.UI.WebControls.DetailsViewDeletedEventArgs
A DetailsViewDeletedEventArgs that contains the event data.
The DetailsView control raises the ItemDeleted event when a Delete button (a button with its CommandName property set to "Delete") within the control is clicked, but after the DetailsView control deletes the record. This allows you to provide an event handler that performs a custom routine, such as checking the results of a delete operation, whenever this event occurs.
When you create a DetailsViewDeletedEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event-handler delegates, see NIB: Events and Delegates.
The following code example demonstrates how to programmatically add a DetailsViewDeletedEventHandler delegate to the ItemDeleted event of a DetailsView control.
<%@ page language="VB"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <script runat="server"> Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) ' Create a new DetailsView object. Dim customerDetailsView As New DetailsView() ' Set the DetailsView object's properties. customerDetailsView.ID = "CustomerDetailsView" customerDetailsView.DataSourceID = "DetailsViewSource" customerDetailsView.AutoGenerateRows = True customerDetailsView.AutoGenerateDeleteButton = True customerDetailsView.AllowPaging = True customerDetailsView.PagerSettings.Mode = _ PagerButtons.NextPrevious Dim keyArray() As String = {"CustomerID"} customerDetailsView.DataKeyNames = keyArray ' Programmatically register the event-handling method ' for the ItemDeleted event of a DetailsView control. AddHandler customerDetailsView.ItemDeleted, _ AddressOf CustomerDetailView_ItemDeleted ' Add the DetailsView object to the Controls collection ' of the PlaceHolder control. DetailsViewPlaceHolder.Controls.Add(customerDetailsView) End Sub Sub CustomerDetailView_ItemDeleted(ByVal sender As Object, _ ByVal e As DetailsViewDeletedEventArgs) ' Use the Exception property to determine whether an exception ' occurred during the delete operation. If e.Exception Is Nothing Then ' Use the AffectedRows property to determine the numbers of ' rows affected by the delete operation. If e.AffectedRows = 1 Then MessageLabel.Text = e.AffectedRows.ToString() _ & " record deleted successfully." Else MessageLabel.Text = e.AffectedRows.ToString() _ & " records deleted successfully." End If Else ' Insert the code to handle the exception. MessageLabel.Text = e.Exception.Message ' Use the ExceptionHandled property to indicate that the ' exception is already handled. e.ExceptionHandled = True End If End Sub </script> <head runat="server"> <title>DetailsViewDeletedEventHandler Example</title> </head> <body> <form id="form1" runat="server"> <h3>DetailsViewDeletedEventHandler Example</h3> <!-- Use a PlaceHolder control as the container for the --> <!-- dynamically generated DetailsView control. --> <asp:PlaceHolder id="DetailsViewPlaceHolder" runat="server"/> <br/><br/> <asp:label id="MessageLabel" forecolor="Red" runat="server"/> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the web.config file. --> <asp:sqldatasource id="DetailsViewSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" deletecommand="Delete [Customers] Where [CustomerID]=@CustomerID" connectionstring= "<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
The following code example demonstrates how to declaratively add a DetailsViewDeletedEventHandler delegate to the ItemDeleted event of a DetailsView control.
<%@ page language="VB"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <script runat="server"> Sub CustomerDetailView_ItemDeleted(ByVal sender As Object, _ ByVal e As DetailsViewDeletedEventArgs) ' Use the Exception property to determine whether an exception ' occurred during the delete operation. If e.Exception Is Nothing Then ' Use the AffectedRows property to determine the numbers of ' rows affected by the delete operation. If e.AffectedRows = 1 Then MessageLabel.Text = e.AffectedRows.ToString() _ & " record deleted successfully." Else MessageLabel.Text = e.AffectedRows.ToString() _ & " records deleted successfully." End If Else ' Insert the code to handle the exception. MessageLabel.Text = e.Exception.Message ' Use the ExceptionHandled property to indicate that the ' exception is already handled. e.ExceptionHandled = True End If End Sub </script> <head runat="server"> <title>DetailsViewDeletedEventHandler Example</title> </head> <body> <form id="form1" runat="server"> <h3>DetailsViewDeletedEventHandler Example</h3> <asp:detailsview id="CustomerDetailsView" datasourceid="DetailsViewSource" autogeneraterows="true" autogeneratedeletebutton="true" allowpaging="true" datakeynames="CustomerID" runat="server"> <pagersettings mode="NextPrevious"/> </asp:detailsview> <br/><br/> <asp:label id="MessageLabel" forecolor="Red" runat="server"/> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the web.config file. --> <asp:sqldatasource id="DetailsViewSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" deletecommand="Delete [Customers] Where [CustomerID]=@CustomerID" connectionstring= "<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"/> </form> </body> </html>
Available since 2.0