ObjectDataSourceStatusEventArgs.ExceptionHandled ObjectDataSourceStatusEventArgs.ExceptionHandled ObjectDataSourceStatusEventArgs.ExceptionHandled ObjectDataSourceStatusEventArgs.ExceptionHandled Property

定義

ビジネス オブジェクトによりスローされた例外が処理されたかどうか示す値を取得または設定します。Gets or sets a value indicating whether an exception that was thrown by the business object has been handled.

public:
 property bool ExceptionHandled { bool get(); void set(bool value); };
public bool ExceptionHandled { get; set; }
member this.ExceptionHandled : bool with get, set
Public Property ExceptionHandled As Boolean

プロパティ値

ビジネス オブジェクトによりスローされた例外が処理され、ObjectDataSource がその例外をスローする必要がない場合は true が返されます。それ以外の場合は false が返されます。true if an exception thrown by the business object has been handled and should not be thrown by the ObjectDataSource; otherwise, false.

ビジネスオブジェクトObjectDataSource GridViewとコントロールを使用してデータを削除する方法を次のコード例に示します。The following code example demonstrates how to use an ObjectDataSource control with a business object and a GridView control to delete data. GridViewは、 EmployeeLogicオブジェクトからデータを取得するために、 SelectMethodプロパティによって指定されたメソッドを使用して、すべての従業員のセットが最初に表示されます。The GridView initially displays a set of all employees, using the method that is specified by the SelectMethod property to retrieve the data from the EmployeeLogic object. プロパティがAutoGenerateDeleteButtontrue設定されているGridViewため、コントロールには自動的に [削除] ボタンが表示されます。Because the AutoGenerateDeleteButton property is set to true, the GridView control automatically displays a Delete button.

[削除] ボタンDeleteをクリックした場合、アクションは、 DeleteMethodプロパティによって指定されたメソッドと、 DeleteParametersコレクションで指定されているすべてのパラメーターを使用して実行されます。If you click the Delete button, the Delete action is performed using the method that is specified by the DeleteMethod property and any parameters that are specified in the DeleteParameters collection. このコード例では、一部の前処理と後処理の手順も実行します。In this code example, some preprocessing and post-processing steps are also performed. Delete Deleted Deleting Deleteデリゲートは、アクションが実行NorthwindEmployeeDeletedされる前にイベントを処理するために呼び出され、アクションの完了後にイベントを処理するためにデリゲートが呼び出され、任意のものを処理します。 NorthwindEmployeeDeleting発生した可能性のある例外。The NorthwindEmployeeDeleting delegate is called to handle the Deleting event before the Delete action is performed, and the NorthwindEmployeeDeleted delegate is called to handle the Deleted event after the Delete action has completed, to handle any exceptions that might have occurred. この例では、 NorthwindDataExceptionがスローされた場合、 NorthwindEmployeeDeletedデリゲートによって処理されます。In this example, if a NorthwindDataException is thrown, it is handled by the NorthwindEmployeeDeleted delegate.

このコード例で使用するEmployeeLogic中間層ビジネスオブジェクトの実装を確認するには、「」のObjectDataSourceStatusEventArgsクラスの概要を参照してください。To examine the implementation of the EmployeeLogic middle-tier business object that this code example uses, see the class overview in ObjectDataSourceStatusEventArgs.

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %>
<%@ Import namespace="Samples.AspNet.CS" %>
<%@ 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">
private void NorthwindEmployeeDeleting(object source, ObjectDataSourceMethodEventArgs e)
{
  // The GridView passes the ID of the employee
  // to be deleted. However, the buisiness object, EmployeeLogic,
  // requires a NorthwindEmployee parameter, named "ne". Create
  // it now and add it to the parameters collection.
  IDictionary paramsFromPage = e.InputParameters;
  if (paramsFromPage["EmpID"] != null) {
    NorthwindEmployee ne
      = new NorthwindEmployee( Int32.Parse(paramsFromPage["EmpID"].ToString()));
    // Remove the old EmpID parameter.
    paramsFromPage.Clear();
    paramsFromPage.Add("ne", ne);
  }
}

private void NorthwindEmployeeDeleted(object source, ObjectDataSourceStatusEventArgs e)
{
  // Handle the Exception if it is a NorthwindDataException
  if (e.Exception != null)
  {

    // Handle the specific exception type. The ObjectDataSource wraps
    // any Exceptions in a TargetInvokationException wrapper, so
    // check the InnerException property for expected Exception types.
    if (e.Exception.InnerException is NorthwindDataException)
    {
      Label1.Text = e.Exception.InnerException.Message;
      // Because the exception is handled, there is
      // no reason to throw it.
      e.ExceptionHandled = true;
    }
  }
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ObjectDataSource - C# Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"
          datasourceid="ObjectDataSource1"
          autogeneratedeletebutton="true"
          autogeneratecolumns="false"
          datakeynames="EmpID">
          <columns>
            <asp:boundfield headertext="EmpID" datafield="EmpID" />
            <asp:boundfield headertext="First Name" datafield="FirstName" />
            <asp:boundfield headertext="Last Name" datafield="LastName" />
          </columns>
        </asp:gridview>

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetAllEmployees"
          deletemethod="DeleteEmployee"
          ondeleting="NorthwindEmployeeDeleting"
          ondeleted="NorthwindEmployeeDeleted"
          typename="Samples.AspNet.CS.EmployeeLogic">
          <deleteparameters>
            <asp:parameter name="EmpID" type="Int32" />
          </deleteparameters>
        </asp:objectdatasource>

        <asp:label id="Label1" runat="server" />

    </form>
  </body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %>
<%@ Import namespace="Samples.AspNet.VB" %>
<%@ 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">
' Called before a Delete operation.
    Private Sub NorthwindEmployeeDeleting(ByVal source As Object, ByVal e As ObjectDataSourceMethodEventArgs)

        ' The GridView passes the ID of the employee
        ' to be deleted. However, the business object, EmployeeLogic,
        ' requires a NorthwindEmployee parameter, named "ne". Create
        ' it now and add it to the parameters collection.
        Dim paramsFromPage As IDictionary = e.InputParameters
  
        If Not paramsFromPage("EmpID") Is Nothing Then
    
            Dim ne As New NorthwindEmployee(paramsFromPage("EmpID").ToString())
            ' Remove the old EmpID parameter.
            paramsFromPage.Clear()
            paramsFromPage.Add("ne", ne)
    
    
        End If
    End Sub ' NorthwindEmployeeDeleting

    ' Called after a Delete operation.
    Private Sub NorthwindEmployeeDeleted(ByVal source As Object, ByVal e As ObjectDataSourceStatusEventArgs)
        ' Handle the Exception if it is a NorthwindDataException.
        If Not e.Exception Is Nothing Then

            ' Handle the specific exception type. The ObjectDataSource wraps
            ' any Exceptions in a TargetInvokationException wrapper, so
            ' check the InnerException property for the expected Exception types.
            If e.Exception.InnerException.GetType().Equals(GetType(NorthwindDataException)) Then

                Label1.Text = e.Exception.InnerException.Message
                ' Because the exception is handled, there is
                ' no reason to throw it.
                e.ExceptionHandled = True
      
            End If
        End If
    End Sub ' NorthwindEmployeeDeleted
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ObjectDataSource - VB Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

        <asp:gridview
          id="GridView1"
          runat="server"
          datasourceid="ObjectDataSource1"
          autogeneratedeletebutton="true"
          autogeneratecolumns="false"
          datakeynames="EmpID">
          <columns>
            <asp:boundfield headertext="EmpID" datafield="EmpID" />
            <asp:boundfield headertext="First Name" datafield="FirstName" />
            <asp:boundfield headertext="Last Name" datafield="LastName" />
          </columns>
        </asp:gridview>

        <asp:objectdatasource
          id="ObjectDataSource1"
          runat="server"
          selectmethod="GetAllEmployees"
          deletemethod="DeleteEmployee"
          ondeleting="NorthwindEmployeeDeleting"
          ondeleted="NorthwindEmployeeDeleted"
          typename="Samples.AspNet.VB.EmployeeLogic">
          <deleteparameters>
            <asp:parameter name="EmpID" type="Int32" />
          </deleteparameters>
        </asp:objectdatasource>

        <asp:label id="Label1" runat="server" />

    </form>
  </body>
</html>

注釈

ビジネスオブジェクトが例外をスローした場合ExceptionHandled 、プロパティはにfalse設定され、 Exception例外はプロパティによってラップされます。If the business object throws an exception, the ExceptionHandled property is set to false and the exception is wrapped by the Exception property. ObjectDataSourceStatusEventHandlerオブジェクトを使用する場合は、 Exceptionプロパティを確認し、例外を処理することができます。If you use an ObjectDataSourceStatusEventHandler object, you can check the Exception property and handle the exception. 例外を処理する場合は、 ExceptionHandledプロパティをに設定するObjectDataSource trueか、コントロールが例外をスローします。If you handle the exception, set the ExceptionHandled property to true or the ObjectDataSource control will throw an exception.

適用対象

こちらもご覧ください