LinqDataSource.Inserting 事件

定義

在插入作業之前發生。Occurs before an insert operation.

public:
 event EventHandler<System::Web::UI::WebControls::LinqDataSourceInsertEventArgs ^> ^ Inserting;
public event EventHandler<System.Web.UI.WebControls.LinqDataSourceInsertEventArgs> Inserting;
member this.Inserting : EventHandler<System.Web.UI.WebControls.LinqDataSourceInsertEventArgs> 
Public Custom Event Inserting As EventHandler(Of LinqDataSourceInsertEventArgs) 

事件類型

EventHandler<LinqDataSourceInsertEventArgs>

範例

下列範例顯示在 Inserting 插入作業之前修改資料之事件的事件處理常式。The following example shows an event handler for the Inserting event that modifies data before the insert operation. 屬性中的物件 NewObject 會轉換成名為的型別 ProductThe object from the NewObject property is cast to a type named Product. DateModified物件的屬性 Product 會設定為目前的日期和時間。The DateModified property of the Product object is set to the current date and time.

protected void LinqDataSource_Inserting(object sender, LinqDataSourceInsertEventArgs e)
{
    Product product = (Product)e.NewObject;
    product.DateModified = DateTime.Now;
}
Protected Sub LinqDataSource_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceInsertEventArgs)
    Dim product As Product
    product = CType(e.NewObject, Product)
    product.DateModified = DateTime.Now
End Sub

下列範例顯示可取得驗證例外狀況之事件的事件處理常式 InsertingThe following example shows an event handler for the Inserting event that retrieves validation exceptions.

Protected Sub LinqDataSource_Inserting(ByVal sender As Object, _  
        ByVal e As LinqDataSourceInsertEventArgs)  
    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_Inserting(object sender,   
        LinqDataSourceInsertEventArgs 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. OnAgeChanging方法會檢查屬性的編號 Age 是否不是負數。The OnAgeChanging method checks that the number for the Age property is not negative.

partial void  OnAgeChanging(int? value)  
{  
    if (value < 0)  
    {  
        throw new Exception("Age cannot be a negative number.");  
    }  
}  
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  

備註

處理 Inserting 事件以驗證要插入的物件、檢查資料類別中的資料驗證錯誤、變更插入作業之前的值,或取消插入作業。Handle the Inserting event to validate the object to be inserted, to examine data validation errors from the data class, to change a value before the insert operation, or to cancel the insert operation. LinqDataSourceInsertEventArgs傳遞給這個事件之事件處理常式的物件包含要在資料來源中插入的新物件。The LinqDataSourceInsertEventArgs object passed to event handlers for this event contains the new object to insert in the data source.

如果在插入作業期間發生驗證錯誤,物件就會 LinqDataSourceInsertEventArgs 包含資料類別所擲回的驗證例外狀況。If a validation error occurs during the insert operation, the LinqDataSourceInsertEventArgs object contains the validation exceptions that are thrown by the data class. 如果要插入的值與資料類別中的屬性類型不相符,或是未通過自訂驗證檢查,就會發生驗證錯誤。A validation error occurs if a value to be inserted does not match the type of the property in the data class, or if it does not pass a custom validation check. 在事件的事件處理常式中 Inserting ,您可以取出驗證例外狀況,並採取適當的動作。In an event handler for the Inserting event, you can retrieve the validation exceptions and take appropriate action.

如果事件的事件處理常式中擲回例外狀況 Inserting ,您必須在該事件處理常式中處理例外狀況。If an exception is thrown in an event handler for the Inserting event, you must handle the exception in that event handler. 例外狀況不會 Inserted 透過物件) 的屬性,傳遞給事件 (的事件處理常式 Exception LinqDataSourceStatusEventArgsThe exception will not be passed to an event handler for the Inserted event (through the Exception property of the LinqDataSourceStatusEventArgs object). Exception屬性只包含事件之後擲回的例外狀況 InsertingThe Exception property contains only the exceptions that are thrown after the Inserting event.

適用於