Upraviť

Zdieľať cez


LinqDataSourceUpdateEventArgs Class

Definition

Provides data for the Updating event.

public ref class LinqDataSourceUpdateEventArgs : System::ComponentModel::CancelEventArgs
public class LinqDataSourceUpdateEventArgs : System.ComponentModel.CancelEventArgs
type LinqDataSourceUpdateEventArgs = class
    inherit CancelEventArgs
Public Class LinqDataSourceUpdateEventArgs
Inherits CancelEventArgs
Inheritance
LinqDataSourceUpdateEventArgs

Examples

The following example shows an event handler for the Updating event. The example shows how to compare properties from the OriginalObject property and the NewObject property to determine whether the value in the Category property has changed. If so, the CategoryChanged property of the object in the NewObject property is set to true.

protected void LinqDataSource_Updating(object sender, LinqDataSourceUpdateEventArgs e)
{
    Product originalProduct = (Product)e.OriginalObject;
    Product newProduct = (Product)e.NewObject;

    if (originalProduct.Category != newProduct.Category)
    {
        newProduct.CategoryChanged = 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. It displays any validation exception messages by using a Label control.

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  
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;  
    }  
}  

Remarks

The LinqDataSourceUpdateEventArgs object is passed to any event handler for the Updating event. You can use the LinqDataSourceUpdateEventArgs object to examine the data before the update operation is executed in the data source. You can then validate the data, examine validation exceptions thrown by the data class, or change a value before the update. You can also cancel the update operation.

The OriginalObject object contains the data that was originally retrieved from the data source. The NewObject object contains the data that will be saved in the data source during the update operation.

If the object that represents the data source throws a validation exception before updating the data, the Exception property contains an instance of the LinqDataSourceValidationException class. You can retrieve all the validation exceptions through the InnerExceptions property. If no validation exception is thrown, the Exception property contains null. If you handle the validation exceptions and do not want the exception to be re-thrown, set the ExceptionHandled property to true.

By default, the LinqDataSource control stores the original values from the data source in view state on the Web page, except those whose ColumnAttribute attribute is marked as UpdateCheck.Never. LINQ to SQL automatically checks the integrity of the data before updating the data. It does this by comparing the current values in the data source with the original values stored in view state. LINQ to SQL raises an exception if the values in the data source have changed. You can perform additional data validation by creating a handler for the Updating event.

Constructors

LinqDataSourceUpdateEventArgs(LinqDataSourceValidationException)

Initializes a new instance of the LinqDataSourceUpdateEventArgs class with the specified exception.

LinqDataSourceUpdateEventArgs(Object, Object)

Initializes a new instance of the LinqDataSourceUpdateEventArgs class.

Properties

Cancel

Gets or sets a value indicating whether the event should be canceled.

(Inherited from CancelEventArgs)
Exception

Gets the exception that was thrown while the data was being validated before the update operation.

ExceptionHandled

Gets or sets a value that indicates whether the exception was handled and that it should not be thrown again.

NewObject

Gets the object that contains the values that will be saved in the data source.

OriginalObject

Gets the object that contains the values that were originally retrieved from the data source.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also