System.Web.UI.WebControls Namespace FormView Class
.NET Framework version 2.0
Occurs when an Insert command button within a FormView control is clicked, but after the insert operation.
[ VB ]
Public Event ItemInserted As FormViewInsertedEventHandler
[ C# ]
public event FormViewInsertedEventHandler ItemInserted;
[ C++ ]
public: __event FormViewInsertedEventHandler* ItemInserted;
In [ JScript ], you can handle the events defined by a class, but you cannot define your own.
The ItemInserted event is raised whenever an Insert button associated with an item in the FormView control is clicked, but after the FormView control inserts the record.
This allows you to provide an event-handling method that performs a custom routine, such as checking the results of an insert operation, whenever this event occurs.
Information related to the ItemInserted event is passed via a FormViewInsertedEventArgs object to the method assigned to handle the event. The following FormViewInsertedEventArgs properties provide information specific to this event.
Property |
Description |
AffectedRows |
Gets the number of rows affected by the insert operation. |
Exception |
Gets the exception ( if any ) that was raised during the insert operation. |
ExceptionHandled |
Gets or sets a value indicating whether an exception that was raised during the insert operation was handled in the event handler. |
KeepInInsertMode |
Gets or sets a value indicating whether the FormView control should remain in insert mode after an insert operation. |
Values |
Gets a dictionary of the non-key field name/value pairs for the inserted record. |
The following example demonstrates how to use the ItemInserted event to determine whether an exception occurred during an insert operation.
<script language = "C#" runat = "server">
void EmployeeFormView_ItemInserted ( Object src, FormViewInsertedEventArgs e ) {
// Use the Exception property to determine whether
// an exception occurred during the insert operation.
if ( e.Exception == null ) {
// Use the AffectedRows property to determine whether the record was inserted.
// Sometimes an error might occur but does not raise an exception,
// yet prevents the insert operation from completing.
if ( e.AffectedRows == 1 ) {
MessageLabel.Text = "Record inserted successfully.";
} else {
MessageLabel.Text = "An error occurred during the insert operation.";
// Use the KeepInInsertMode property to remain in insert mode
// when an error occurs during the insert operation.
e.KeepInInsertMode = true;
}
} else {
// Insert the code to handle the exception.
MessageLabel.Text = e.Exception.Message;
// Use the ExceptionHandled property to indicate
// that the exception has already been handled.
e.ExceptionHandled = true;
}
}
</script>
<script language = "VB" runat = "server">
Sub EmployeeFormView_ItemInserted ( ByVal src As Object, ByVal e As FormViewInsertedEventArgs )
' Use the Exception property to determine whether
' an exception occurred during the insert operation.
If e.Exception Is Nothing Then
' Use the AffectedRows property to determine whether the record was inserted.
' Sometimes an error might occur but does not raise an exception,
' yet prevents the insert operation from completing.
If e.AffectedRows = 1 Then
MessageLabel.Text = "Record inserted successfully."
Else
MessageLabel.Text = "An error occurred during the insert operation."
' Use the KeepInInsertMode property to remain in insert mode
' when an error occurs during the insert operation.
e.KeepInInsertMode = True
End If
Else
' Insert the code to handle the exception.
MessageLabel.Text = e.Exception.Message
' Use the ExceptionHandled property to indicate
' that the exception has already been handled.
e.ExceptionHandled = True
End If
End Sub
</script> |
|
C# |
VB |
FormView Members Allowing Users to Add Items in a FormView Control