Export (0) Print
Expand All

LinqDataSource.Updating Event

Occurs before an update operation.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

'Declaration
Public Event Updating As EventHandler(Of LinqDataSourceUpdateEventArgs)
'Usage
Dim instance As LinqDataSource 
Dim handler As EventHandler(Of LinqDataSourceUpdateEventArgs)

AddHandler instance.Updating, handler
<asp:LinqDataSource OnUpdating="EventHandler" />

Handle the Updating event to validate the object to be updated, to examine data validation errors from the data class, to change a value before the update operation, or to cancel the update operation. The LinqDataSourceUpdateEventArgs object that is passed to event handlers contains both the original object and the updated object.

If a validation error occurs during the update operation, the LinqDataSourceInsertEventArgs object contains the validation exceptions that are thrown by the data class. A validation error occurs if a value to be updated does not match the type of the property in the data class, or if it does not pass a custom validation check. In an event handler for the Updating event, you can retrieve the validation exceptions and take appropriate action.

If an exception is thrown in an event handler for the Updating event, you must handle the exception in that event handler. The exception will not be passed to an event handler for the Updated event (through the Exception property of the LinqDataSourceStatusEventArgs object). The Exception property contains only the exceptions that are thrown after the Updating event.

The following example shows an event handler for the Updating event. The code compares properties from the OriginalObject property and the NewObject property to see whether a value has changed. If the value has changed, a value of the NewObject property is set to true.

Protected Sub LinqDataSource_Updating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceUpdateEventArgs)
    Dim originalProduct As Product
    Dim newProduct As Product

    originalProduct = CType(e.OriginalObject, Product)
    newProduct = CType(e.NewObject, Product)

    If (originalProduct.Category <> newProduct.Category) Then
        newProduct.CategoryChanged = True 
    End If 
End Sub

The following example shows an event handler for the Updating event that retrieves validation exceptions.

Protected Sub LinqDataSource_Updating(ByVal sender As Object, _
        ByVal e As LinqDataSourceUpdateEventArgs)
    If (e.Exception IsNot Nothing) Then
        For Each innerException As KeyValuePair(Of String, Exception) _
               In e.Exception.InnerExceptions
            Label1.Text &= innerException.Key & ": " & _
                innerException.Value.Message & "<br />"
        Next
        e.ExceptionHandled = True
    End If
End Sub

[C#]

protected void LinqDataSource_Updating(object sender, 
        LinqDataSourceUpdateEventArgs e)
{
    if (e.Exception != null)
    {
        foreach (KeyValuePair<string, Exception> innerException in 
             e.Exception.InnerExceptions)
        {
        Label1.Text += innerException.Key + ": " + 
            innerException.Value.Message + "<br />";
        }
        e.ExceptionHandled = true;
    }
}

The previous example retrieves validation exceptions. An exception might be thrown if a value does not match the type of the property. It might also be thrown from a customized check such as the one in the following example. The OnAgeChanging method checks that the number for the Age property is not negative.

Private Sub OnAgeChanging(ByVal value As System.Nullable(Of Integer))
    If (value < 0) Then
        Throw New Exception("Age cannot be a negative number.")
    End If
End Sub

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5

Community Additions

ADD
Show:
© 2014 Microsoft