DetailsViewInsertedEventArgs Classe
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Fornisce i dati per l'evento ItemInserted.
public ref class DetailsViewInsertedEventArgs : EventArgs
public class DetailsViewInsertedEventArgs : EventArgs
type DetailsViewInsertedEventArgs = class
inherit EventArgs
Public Class DetailsViewInsertedEventArgs
Inherits EventArgs
- Ereditarietà
Esempio
Nell'esempio di codice seguente viene illustrato come utilizzare l'oggetto DetailsViewInsertedEventArgs passato al gestore eventi per l'evento ItemInserted per determinare se si è verificata un'eccezione durante un'operazione di inserimento.
<%@ 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 CustomerDetailsView_ItemInserted(Object sender,
DetailsViewInsertedEventArgs e)
{
// Use the Exception property to determine whether an exception
// occurred during the insert operation.
if (e.Exception == null && e.AffectedRows == 1)
{
// Use the Values property to get the value entered by
// the user for the CompanyName field.
String name = e.Values["CompanyName"].ToString();
// Display a confirmation message.
MessageLabel.Text = name + " added 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;
// When an exception occurs, keep the DetailsView
// control in insert mode.
e.KeepInInsertMode = true;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsViewInsertedEventArgs Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DetailsViewInsertedEventArgs Example</h3>
<asp:detailsview id="CustomerDetailsView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogenerateinsertbutton="true"
autogeneraterows="true"
allowpaging="true"
oniteminserted="CustomerDetailsView_ItemInserted"
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"
selectcommand="Select [CustomerID], [CompanyName], [Address],
[City], [PostalCode], [Country] From [Customers]"
insertcommand="INSERT INTO [Customers]([CustomerID],
[CompanyName], [Address], [City], [PostalCode],
[Country]) VALUES (@CustomerID, @CompanyName, @Address,
@City, @PostalCode, @Country)"
connectionstring=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
<%@ Page language="VB" autoeventwireup="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub CustomerDetailsView_ItemInserted(ByVal sender As Object, _
ByVal e As DetailsViewInsertedEventArgs) _
Handles CustomerDetailsView.ItemInserted
' Use the Exception property to determine whether an exception
' occurred during the insert operation.
If e.Exception Is Nothing And e.AffectedRows = 1 Then
' Use the Values property to get the value entered by
' the user for the CompanyName field.
Dim name As String = e.Values("CompanyName").ToString()
' Display a confirmation message.
MessageLabel.Text = name & " added 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
' When an exception occurs, keep the DetailsView
' control in insert mode.
e.KeepInInsertMode = True
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DetailsViewInsertedEventArgs Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>DetailsViewInsertedEventArgs Example</h3>
<asp:detailsview id="CustomerDetailsView"
datasourceid="DetailsViewSource"
datakeynames="CustomerID"
autogenerateinsertbutton="true"
autogeneraterows="true"
allowpaging="true"
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"
selectcommand="Select [CustomerID], [CompanyName], [Address],
[City], [PostalCode], [Country] From [Customers]"
insertcommand="INSERT INTO [Customers]([CustomerID], [CompanyName],
[Address], [City], [PostalCode], [Country])
VALUES (@CustomerID, @CompanyName, @Address, @City,
@PostalCode, @Country)"
connectionstring=
"<%$ ConnectionStrings:NorthWindConnectionString%>"
runat="server"/>
</form>
</body>
</html>
Commenti
Il DetailsView controllo genera l'evento ItemInserted quando si fa clic su un pulsante Inserisci (un pulsante con la relativa CommandName proprietà impostata su "Inserisci") all'interno del controllo , ma dopo che il DetailsView controllo inserisce il record. In questo modo è possibile fornire un gestore eventi che esegue una routine personalizzata, ad esempio il controllo dei risultati di un'operazione di inserimento, ogni volta che si verifica questo evento.
Un DetailsViewInsertedEventArgs oggetto viene passato al gestore eventi, che consente di determinare il numero di record interessati ed eventuali eccezioni che potrebbero essersi verificate. Per determinare il numero di record interessati dall'operazione di inserimento, utilizzare la AffectedRows proprietà . Utilizzare la Exception proprietà per determinare se si sono verificate eccezioni. È anche possibile indicare se l'eccezione è stata gestita nel gestore eventi impostando la ExceptionHandled proprietà . Se è necessario accedere ai valori del record inserito, utilizzare la Values proprietà .
Per impostazione predefinita, il DetailsView controllo torna alla modalità specificata dalla DefaultMode proprietà dopo un'operazione di inserimento. Per mantenere il DetailsView controllo in modalità di inserimento, impostare la KeepInInsertMode proprietà su true.
Per altre informazioni su come gestire gli eventi, vedere Gestione e generazione di eventi.
Per un elenco dei valori iniziali delle proprietà per un'istanza della DetailsViewDeletedEventArgs classe , vedere il DetailsViewDeletedEventArgs costruttore .
Costruttori
| Nome | Descrizione |
|---|---|
| DetailsViewInsertedEventArgs(Int32, Exception) |
Inizializza una nuova istanza della classe DetailsViewInsertedEventArgs. |
Proprietà
| Nome | Descrizione |
|---|---|
| AffectedRows |
Ottiene il numero di righe interessate dall'operazione di inserimento. |
| Exception |
Ottiene l'eccezione (se presente) generata durante l'operazione di inserimento. |
| ExceptionHandled |
Ottiene o imposta un valore che indica se un'eccezione generata durante l'operazione di inserimento è stata gestita nel gestore eventi. |
| KeepInInsertMode |
Ottiene o imposta un valore che indica se il DetailsView controllo deve rimanere in modalità di inserimento dopo un'operazione di inserimento. |
| Values |
Ottiene un dizionario contenente le coppie nome/valore del campo per il record inserito. |
Metodi
| Nome | Descrizione |
|---|---|
| Equals(Object) |
Determina se l'oggetto specificato è uguale all'oggetto corrente. (Ereditato da Object) |
| GetHashCode() |
Funge da funzione hash predefinita. (Ereditato da Object) |
| GetType() |
Ottiene l'oggetto Type dell'istanza corrente. (Ereditato da Object) |
| MemberwiseClone() |
Crea una copia superficiale dell'oggetto corrente Object. (Ereditato da Object) |
| ToString() |
Restituisce una stringa che rappresenta l'oggetto corrente. (Ereditato da Object) |