DetailsView.ItemDeleting Evento
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Aparece cuando se hace clic en un botón Suprimir de un control DetailsView, pero antes de la operación de eliminación.
public:
event System::Web::UI::WebControls::DetailsViewDeleteEventHandler ^ ItemDeleting;
public event System.Web.UI.WebControls.DetailsViewDeleteEventHandler ItemDeleting;
member this.ItemDeleting : System.Web.UI.WebControls.DetailsViewDeleteEventHandler
Public Custom Event ItemDeleting As DetailsViewDeleteEventHandler
Tipo de evento
Ejemplos
En el ejemplo de código siguiente se muestra cómo usar el ItemDeleting evento para cancelar la operación de eliminación si el usuario intenta eliminar el último elemento del DetailsView control.
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void CustomerDetailView_ItemDeleting(Object sender, DetailsViewDeleteEventArgs e)
{
// Cancel the delete operation if the user attempts to delete the last
// record from the data source.
if (CustomerDetailView.DataItemCount <= 1)
{
e.Cancel = true;
MessageLabel.Text = "You must keep at least one store.";
}
else
{
MessageLabel.Text = "";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsView ItemDeleting Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>DetailsView ItemDeleting Example</h3>
<asp:detailsview id="CustomerDetailView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogeneratedeletebutton="true"
autogeneraterows="true"
allowpaging="true"
onitemdeleting="CustomerDetailView_ItemDeleting"
runat="server">
<fieldheaderstyle backcolor="Navy"
forecolor="White"/>
</asp:detailsview>
<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" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country]) VALUES (@CustomerID, @CompanyName, @Address, @City, @PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country]
From [Customers]"
DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = @CustomerID" >
</asp:SqlDataSource>
</form>
</body>
</html>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub CustomerDetailView_ItemDeleting(ByVal sender As Object, ByVal e As DetailsViewDeleteEventArgs)
' Cancel the delete operation if the user attempts to delete the last
' record from the data source.
If (CustomerDetailView.DataItemCount <= 1) Then
e.Cancel = True
MessageLabel.Text = "You must keep at least one store."
Else
MessageLabel.Text = ""
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsView ItemDeleting Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>DetailsView ItemDeleting Example</h3>
<asp:detailsview id="CustomerDetailView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogeneratedeletebutton="true"
autogeneraterows="true"
allowpaging="true"
onitemdeleting="CustomerDetailView_ItemDeleting"
runat="server">
<fieldheaderstyle backcolor="Navy"
forecolor="White"/>
</asp:detailsview>
<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" runat="server"
ConnectionString=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
InsertCommand="INSERT INTO [Customers]([CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country]) VALUES (@CustomerID, @CompanyName, @Address, @City, @PostalCode, @Country)"
SelectCommand="Select [CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country]
From [Customers]"
DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = @CustomerID" >
</asp:SqlDataSource>
</form>
</body>
</html>
Comentarios
El ItemDeleting evento se genera cuando se hace clic en un botón Eliminar dentro del DetailsView control, pero antes de la operación de eliminación. Esto le permite proporcionar un controlador de eventos que realiza una rutina personalizada, como cancelar la operación de eliminación, siempre que se produzca este evento.
Se pasa un DetailsViewDeleteEventArgs objeto al controlador de eventos, que permite determinar el índice del registro que se va a eliminar e indicar que se debe cancelar la operación de eliminación. Para cancelar la operación de eliminación, establezca la Cancel propiedad true
en . También puede manipular las Keys colecciones y Values , si es necesario, antes de pasar los valores al origen de datos.
Para obtener más información acerca de cómo controlar eventos, vea controlar y provocar eventos.