Expandir Minimizar
Este tema aún no ha recibido ninguna valoración - Valorar este tema

DetailsViewDeletedEventHandler (Delegado)

Actualización: noviembre 2007

Representa el método que controla el evento ItemDeleted de un control DetailsView.

Espacio de nombres:  System.Web.UI.WebControls
Ensamblado:  System.Web (en System.Web.dll)
public delegate void DetailsViewDeletedEventHandler(
	Object sender,
	DetailsViewDeletedEventArgs e
)
/** @delegate */
public delegate void DetailsViewDeletedEventHandler(
	Object sender,
	DetailsViewDeletedEventArgs e
)
JScript no admite delegados.

Parámetros

sender
Tipo: System.Object
Origen del evento.
e
Tipo: System.Web.UI.WebControls.DetailsViewDeletedEventArgs
Objeto DetailsViewDeletedEventArgs que contiene los datos del evento.

El control DetailsView provoca el evento ItemDeleted cuando se hace clic en un botón Eliminar (botón con su propiedad CommandName establecida en "Delete") dentro del control, pero después de que el control DetailsView elimine el registro. De esta forma, se puede proporcionar un controlador de eventos que realiza una rutina personalizada, como comprobar los resultados de una operación de eliminación, siempre que se produce este evento.

Cuando se crea un delegado DetailsViewDeletedEventHandler, se identifica el método que controlará el evento. Para asociar el evento al controlador de eventos, se debe agregar una instancia del delegado al evento. Siempre que se produzca el evento, se llamará al controlador de eventos, a menos que se quite el delegado. Para obtener más información sobre los delegados de controladores de eventos, vea Eventos y delegados.

En el siguiente ejemplo de código se muestra cómo agregar un delegado DetailsViewDeletedEventHandler mediante programación al evento ItemDeleted de un control DetailsView.


<%@ page language="C#"%>

<!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">

    void Page_Load(Object sender, EventArgs e)
    {

      // Create a new DetailsView object.
      DetailsView customerDetailsView = 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;
      customerDetailsView.DataKeyNames = new String[1] { "CustomerID" };

      // Programmatically register the event-handling method
      // for the ItemDeleted event of a DetailsView control.
      customerDetailsView.ItemDeleted += 
        new DetailsViewDeletedEventHandler(
        this.CustomerDetailView_ItemDeleted);

      // Add the DetailsView object to the Controls collection
      // of the PlaceHolder control.
      DetailsViewPlaceHolder.Controls.Add(customerDetailsView);

    }

    void CustomerDetailView_ItemDeleted(Object sender, 
      DetailsViewDeletedEventArgs e)
    {
      // Use the Exception property to determine whether an exception
      // occurred during the delete operation.
      if (e.Exception == null)
      {
        // Use the AffectedRows property to determine the numbers of
        // rows affected by the delete operation.
        if (e.AffectedRows == 1)
        {
          MessageLabel.Text = e.AffectedRows.ToString() 
            + " record deleted successfully.";
        }
        else
        {
          MessageLabel.Text = e.AffectedRows.ToString() 
            + " records deleted successfully.";
        }
      }
      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;
      }
    }

  </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>



En el siguiente ejemplo de código se muestra cómo agregar un delegado DetailsViewDeletedEventHandler mediante declaración al evento ItemDeleted de un control DetailsView.


<%@ page language="C#"%>

<!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">

    void CustomerDetailView_ItemDeleted(Object sender, 
      DetailsViewDeletedEventArgs e)
    {
      // Use the Exception property to determine whether an exception
      // occurred during the delete operation.
      if (e.Exception == null)
      {
        // Use the AffectedRows property to determine the numbers of
        // rows affected by the delete operation.
        if (e.AffectedRows == 1)
        {
          MessageLabel.Text = e.AffectedRows.ToString() 
            + " record deleted successfully.";
        }
        else
        {
          MessageLabel.Text = e.AffectedRows.ToString() 
            + " records deleted successfully.";
        }
      }
      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;
      }
    }

  </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>



Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.

.NET Framework

Compatible con: 3.5, 3.0, 2.0
¿Te ha resultado útil?
(Caracteres restantes: 1500)

Adiciones de comunidad

AGREGAR
© 2013 Microsoft. Reservados todos los derechos.