ListViewInsertedEventArgs 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 ListViewInsertedEventArgs : EventArgs
public class ListViewInsertedEventArgs : EventArgs
type ListViewInsertedEventArgs = class
inherit EventArgs
Public Class ListViewInsertedEventArgs
Inherits EventArgs
- Ereditarietà
Esempio
Nell'esempio seguente viene illustrato come utilizzare l'oggetto ListViewInsertedEventArgs passato al gestore per l'evento ItemInserted .
Importante
In questo esempio è presente una casella di testo che accetta l'input dell'utente, che rappresenta una potenziale minaccia per la sicurezza. Per impostazione predefinita, ASP.NET pagine Web verificare che l'input dell'utente non includa elementi SCRIPT o HTML. Per altre informazioni, vedere Cenni preliminari sugli exploit di script.
<%@ 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">
//<Snippet3>
void ContactsListView_ItemInserted(Object sender, ListViewInsertedEventArgs e)
{
if (e.Exception != null)
{
if (e.AffectedRows == 0)
{
e.KeepInInsertMode = true;
Message.Text = "An exception occurred inserting the new Contact. " +
"Please verify your values and try again.";
}
else
Message.Text = "An exception occurred inserting the new Contact. " +
"Please verify the values in the newly inserted item.";
e.ExceptionHandled = true;
}
}
//</Snippet3>
protected void Page_Load(object sender, EventArgs e)
{
Message.Text = "";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView.ItemInserted Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListViewItemInserted Example</h3>
<asp:Label ID="Message"
ForeColor="Red"
runat="server"/>
<br/>
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
DataKeyNames="ContactID"
OnItemInserted="ContactsListView_ItemInserted"
InsertItemPosition="LastItem"
runat="server">
<LayoutTemplate>
<table cellpadding="2" border="1" runat="server" id="tblContacts" width="640px">
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
<Fields>
<asp:NextPreviousPagerField
ShowFirstPageButton="true" ShowLastPageButton="true"
FirstPageText="|<< " LastPageText=" >>|"
NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td valign="top">
<asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
<asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
</td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr style="background-color:#D3D3D3">
<td valign="top">
<asp:Label runat="server" ID="FirstNameLabel"
AssociatedControlID="FirstNameTextBox" Text="First Name"/>
<asp:TextBox ID="FirstNameTextBox" runat="server"
Text='<%#Bind("FirstName") %>' /><br />
<asp:Label runat="server" ID="LastNameLabel"
AssociatedControlID="LastNameTextBox" Text="Last Name" />
<asp:TextBox ID="LastNameTextBox" runat="server"
Text='<%#Bind("LastName") %>' /><br />
<asp:Label runat="server" ID="EmailLabel"
AssociatedControlID="EmailTextBox" Text="Email" />
<asp:TextBox ID="EmailTextBox" runat="server"
Text='<%#Bind("EmailAddress") %>' />
</td>
<td>
<asp:LinkButton ID="InsertButton" runat="server"
CommandName="Insert" Text="Insert" />
</td>
</tr>
</InsertItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="ContactsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress]
FROM Person.Contact"
InsertCommand="INSERT INTO Person.Contact
([FirstName], [LastName], [EmailAddress], [PasswordHash], [PasswordSalt])
Values(@FirstName, @LastName, @EmailAddress, '', '');
SELECT @ContactID = SCOPE_IDENTITY()">
<InsertParameters>
<asp:Parameter Name="FirstName" />
<asp:Parameter Name="LastName" />
<asp:Parameter Name="EmailAddress" />
<asp:Parameter Name="ContactID" Type="Int32" Direction="Output" />
</InsertParameters>
</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">
'<Snippet3>
Sub ContactsListView_ItemInserted(ByVal sender As Object, ByVal e As ListViewInsertedEventArgs)
If e.Exception IsNot Nothing Then
If e.AffectedRows = 0 Then
e.KeepInInsertMode = True
Message.Text = "An exception occurred inserting the new Contact. " & _
"Please verify your values and try again."
Else
Message.Text = "An exception occurred inserting the new Contact. " & _
"Please verify the values in the newly inserted item."
End If
e.ExceptionHandled = True
End If
End Sub
'</Snippet3>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Message.Text = ""
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>ListView.ItemInserted Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListViewItemInserted Example</h3>
<asp:Label ID="Message"
ForeColor="Red"
runat="server"/>
<br/>
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
DataKeyNames="ContactID"
OnItemInserted="ContactsListView_ItemInserted"
InsertItemPosition="LastItem"
runat="server">
<LayoutTemplate>
<table cellpadding="2" border="1" runat="server" id="tblContacts" width="640px">
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
<Fields>
<asp:NextPreviousPagerField
ShowFirstPageButton="true" ShowLastPageButton="true"
FirstPageText="|<< " LastPageText=" >>|"
NextPageText=" > " PreviousPageText=" < " />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td valign="top">
<asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
<asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
</td>
<td>
<asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
</td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr style="background-color:#D3D3D3">
<td valign="top">
<asp:Label runat="server" ID="FirstNameLabel"
AssociatedControlID="FirstNameTextBox" Text="First Name"/>
<asp:TextBox ID="FirstNameTextBox" runat="server"
Text='<%#Bind("FirstName") %>' /><br />
<asp:Label runat="server" ID="LastNameLabel"
AssociatedControlID="LastNameTextBox" Text="Last Name" />
<asp:TextBox ID="LastNameTextBox" runat="server"
Text='<%#Bind("LastName") %>' /><br />
<asp:Label runat="server" ID="EmailLabel"
AssociatedControlID="EmailTextBox" Text="Email" />
<asp:TextBox ID="EmailTextBox" runat="server"
Text='<%#Bind("EmailAddress") %>' />
</td>
<td>
<asp:LinkButton ID="InsertButton" runat="server"
CommandName="Insert" Text="Insert" />
</td>
</tr>
</InsertItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="ContactsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress]
FROM Person.Contact"
InsertCommand="INSERT INTO Person.Contact
([FirstName], [LastName], [EmailAddress], [PasswordHash], [PasswordSalt])
Values(@FirstName, @LastName, @EmailAddress, '', '');
SELECT @ContactID = SCOPE_IDENTITY()">
<InsertParameters>
<asp:Parameter Name="FirstName" />
<asp:Parameter Name="LastName" />
<asp:Parameter Name="EmailAddress" />
<asp:Parameter Name="ContactID" Type="Int32" Direction="Output" />
</InsertParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
Commenti
Il ListView controllo genera l'evento ItemInserted quando si fa clic su un pulsante Inserisci nel InsertItemTemplate modello e dopo che il ListView controllo aggiorna il record nell'origine dati. Un pulsante Inserisci è un pulsante la cui CommandName proprietà è impostata su "Inserisci".) L'evento ItemInserted consente di eseguire un'azione personalizzata, ad esempio il recupero di valori generati automaticamente dal database per l'elemento inserito.
Un ListViewInsertedEventArgs oggetto viene passato al metodo di gestione degli eventi, che consente di determinare il numero di elementi inseriti ed eventuali eccezioni che potrebbero verificarsi. Per determinare il numero di elementi interessati dall'operazione di inserimento, utilizzare la AffectedRows proprietà . Utilizzare la Exception proprietà per determinare se si è verificata un'eccezione. È anche possibile indicare se l'eccezione è stata gestita nel metodo di gestione degli eventi impostando la ExceptionHandled proprietà . Se è necessario accedere ai valori di campo dell'elemento inserito inviato all'origine dati, utilizzare la Values proprietà .
Per impostazione predefinita, il ListView controllo cancella il InsertItemTemplate modello dopo un'operazione di inserimento, che consente agli utenti di aggiungere valori per un nuovo elemento da inserire. Se si verifica un'eccezione durante l'operazione di inserimento, è possibile mantenere il ListView controllo in modalità di inserimento impostando la KeepInInsertMode proprietà su true. In questo modo il InsertItemTemplate modello viene riassociato ai valori del tentativo precedente di inserire un elemento.
Per un elenco dei valori iniziali delle proprietà per un'istanza della ListViewInsertedEventArgs classe , vedere il ListViewInsertedEventArgs costruttore .
Costruttori
| Nome | Descrizione |
|---|---|
| ListViewInsertedEventArgs(Int32, Exception) |
Inizializza una nuova istanza della classe ListViewInsertedEventArgs. |
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 oggetto 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 i valori di input dell'utente vengono mantenuti per i controlli all'interno del InsertItemTemplate modello. |
| Values |
Ottiene 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 il Type dell'istanza corrente. (Ereditato da Object) |
| MemberwiseClone() |
Crea una copia superficiale del Objectcorrente. (Ereditato da Object) |
| ToString() |
Restituisce una stringa che rappresenta l'oggetto corrente. (Ereditato da Object) |