GridViewDeleteEventArgs GridViewDeleteEventArgs GridViewDeleteEventArgs GridViewDeleteEventArgs Class

Definizione

Fornisce dati per l'evento RowDeleting.Provides data for the RowDeleting event.

public ref class GridViewDeleteEventArgs : System::ComponentModel::CancelEventArgs
public class GridViewDeleteEventArgs : System.ComponentModel.CancelEventArgs
type GridViewDeleteEventArgs = class
    inherit CancelEventArgs
Public Class GridViewDeleteEventArgs
Inherits CancelEventArgs
Ereditarietà
GridViewDeleteEventArgsGridViewDeleteEventArgsGridViewDeleteEventArgsGridViewDeleteEventArgs

Esempi

Nell'esempio seguente viene illustrato come utilizzare l' GridViewDeleteEventArgs oggetto passato al gestore dell'evento.The following example shows how to use the GridViewDeleteEventArgs object that is passed to the event handler. Il codice annulla l'operazione di eliminazione se l'utente tenta di rimuovere l'ultimo record da un GridView controllo.The code cancels the delete operation if the user tries to remove the last record from a GridView 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 CustomersGridView_RowDeleting
        (Object sender, GridViewDeleteEventArgs e)
    {
        TableCell cell = CustomersGridView.Rows[e.RowIndex].Cells[2];
        if (cell.Text == "Beaver")
        {
            e.Cancel = true;
            Message.Text = "You cannot delete customer Beaver.";
        }
        else
        {
            Message.Text = "";
        }
    }  

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>GridView RowDeleting Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <h3>
        GridView RowDeleting Example
    </h3>
    <asp:Label ID="Message" ForeColor="Red" runat="server" />
    <br />
    <asp:GridView ID="CustomersGridView" runat="server" 
        DataSourceID="CustomersSqlDataSource" 
        AutoGenerateColumns="False"
        AutoGenerateDeleteButton="True" 
        OnRowDeleting="CustomersGridView_RowDeleting"
        DataKeyNames="CustomerID,AddressID">
        <Columns>
            <asp:BoundField DataField="FirstName" 
                HeaderText="FirstName" SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
            <asp:BoundField DataField="City" HeaderText="City" 
                SortExpression="City" />
            <asp:BoundField DataField="StateProvince" HeaderText="State" 
                SortExpression="StateProvince" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="CustomersSqlDataSource" runat="server"
        SelectCommand="SELECT SalesLT.CustomerAddress.CustomerID, 
            SalesLT.CustomerAddress.AddressID, 
            SalesLT.Customer.FirstName, 
            SalesLT.Customer.LastName, 
            SalesLT.Address.City, 
            SalesLT.Address.StateProvince 
            FROM SalesLT.Customer 
            INNER JOIN SalesLT.CustomerAddress 
            ON SalesLT.Customer.CustomerID = 
                SalesLT.CustomerAddress.CustomerID 
            INNER JOIN SalesLT.Address ON SalesLT.CustomerAddress.AddressID = 
                SalesLT.Address.AddressID"
        DeleteCommand="Delete from SalesLT.CustomerAddress where CustomerID = 
            @CustomerID and AddressID = @AddressID" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorksLTConnectionString %>">
        <DeleteParameters>
            <asp:Parameter Name="AddressID" />
            <asp:Parameter Name="CustomerID" />
        </DeleteParameters>
    </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">

    Private Sub CustomersGridView_RowDeleting _
        (ByVal sender As [Object], _
        ByVal e As GridViewDeleteEventArgs)
        Dim cell As TableCell
        cell = CustomersGridView.Rows(e.RowIndex).Cells(2)
        If cell.Text = "Beaver" Then
            e.Cancel = True
            Message.Text = "You cannot delete customer Beaver."
        Else
            Message.Text = ""
        End If
    End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>GridView RowDeleting Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <h3>
        GridView RowDeleting Example
    </h3>
    <asp:Label ID="Message" ForeColor="Red" runat="server" />
    <br />
    <asp:GridView ID="CustomersGridView" runat="server" 
        DataSourceID="CustomersSqlDataSource" 
        AutoGenerateColumns="False"
        AutoGenerateDeleteButton="True" 
        OnRowDeleting="CustomersGridView_RowDeleting"
        DataKeyNames="CustomerID,AddressID">
        <Columns>
            <asp:BoundField DataField="FirstName" 
                HeaderText="FirstName" SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
            <asp:BoundField DataField="City" HeaderText="City" 
                SortExpression="City" />
            <asp:BoundField DataField="StateProvince" HeaderText="State" 
                SortExpression="StateProvince" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="CustomersSqlDataSource" runat="server"
        SelectCommand="SELECT SalesLT.CustomerAddress.CustomerID, 
            SalesLT.CustomerAddress.AddressID, 
            SalesLT.Customer.FirstName, 
            SalesLT.Customer.LastName, 
            SalesLT.Address.City, 
            SalesLT.Address.StateProvince 
            FROM SalesLT.Customer 
            INNER JOIN SalesLT.CustomerAddress 
            ON SalesLT.Customer.CustomerID = 
                SalesLT.CustomerAddress.CustomerID 
            INNER JOIN SalesLT.Address ON SalesLT.CustomerAddress.AddressID = 
                SalesLT.Address.AddressID"
        DeleteCommand="Delete from SalesLT.CustomerAddress where CustomerID = 
            @CustomerID and AddressID = @AddressID" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorksLTConnectionString %>">
        <DeleteParameters>
            <asp:Parameter Name="AddressID" />
            <asp:Parameter Name="CustomerID" />
        </DeleteParameters>
    </asp:SqlDataSource>
    </form>
</body>
</html>

Commenti

Il GridView controllo genera l' RowDeleting evento quando viene fatto clic su un pulsante Elimina, ma prima GridView che il controllo elimini il record.The GridView control raises the RowDeleting event when a Delete button is clicked, but before the GridView control deletes the record. Il pulsante Elimina è un controllo pulsante la cui proprietà CommandName è impostata su "Elimina". Ogni volta che si verifica questo evento, è possibile eseguire una routine personalizzata, ad esempio l'annullamento dell'operazione di eliminazione.(A Delete button is a button control whose CommandName property is set to "Delete".) You can perform a custom routine whenever this event occurs, such as canceling the delete operation.

Un GridViewDeleteEventArgs oggetto viene passato al gestore eventi.A GridViewDeleteEventArgs object is passed to the event handler. Questo consente di determinare l'indice della riga da eliminare e di annullare l'operazione di eliminazione.This enables you to determine the index of the row being deleted and to cancel the delete operation. Per annullare l'operazione di eliminazione, impostare Cancel la proprietà GridViewDeleteEventArgs dell'oggetto su true.To cancel the delete operation, set the Cancel property of the GridViewDeleteEventArgs object to true. È inoltre possibile modificare le Keys raccolte Values e prima che i valori vengano passati all'origine dati.You can also manipulate the Keys and Values collections before the values are passed to the data source.

Per altre informazioni su come gestire gli eventi, vedere la gestione e generazione di eventi.For more information about how to handle events, see Handling and Raising Events.

Per un elenco dei valori iniziali delle proprietà di un'istanza GridViewDeleteEventArgsdi, vedere GridViewDeleteEventArgs il costruttore.For a list of initial property values for an instance of GridViewDeleteEventArgs, see the GridViewDeleteEventArgs constructor.

Costruttori

GridViewDeleteEventArgs(Int32) GridViewDeleteEventArgs(Int32) GridViewDeleteEventArgs(Int32) GridViewDeleteEventArgs(Int32)

Inizializza una nuova istanza della classe GridViewDeleteEventArgs.Initializes a new instance of the GridViewDeleteEventArgs class.

Proprietà

Cancel Cancel Cancel Cancel

Ottiene o imposta un valore che indica se l'evento debba essere annullato.Gets or sets a value indicating whether the event should be canceled.

(Inherited from CancelEventArgs)
Keys Keys Keys Keys

Ottiene un dizionario delle coppie nome/valore dei campi che rappresentano la chiave primaria della riga da eliminare.Gets a dictionary of field name/value pairs that represent the primary key of the row to delete.

RowIndex RowIndex RowIndex RowIndex

Ottiene l'indice della riga da eliminare.Gets the index of the row being deleted.

Values Values Values Values

Ottiene un dizionario delle coppie nome/valore dei campi non chiave della riga da eliminare.Gets a dictionary of the non-key field name/value pairs for the row to delete.

Metodi

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Funge da funzione hash predefinita.Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Ottiene l'oggetto Type dell'istanza corrente.Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.Returns a string that represents the current object.

(Inherited from Object)

Si applica a

Vedi anche