GridViewUpdatedEventArgs GridViewUpdatedEventArgs GridViewUpdatedEventArgs GridViewUpdatedEventArgs Class

定義

RowUpdated イベントのデータを提供します。Provides data for the RowUpdated event.

public ref class GridViewUpdatedEventArgs : EventArgs
public class GridViewUpdatedEventArgs : EventArgs
type GridViewUpdatedEventArgs = class
    inherit EventArgs
Public Class GridViewUpdatedEventArgs
Inherits EventArgs
継承
GridViewUpdatedEventArgsGridViewUpdatedEventArgsGridViewUpdatedEventArgsGridViewUpdatedEventArgs

次の例では、更新操作中に例外が発生したかどうかを判断する方法を示します。The following example shows how to determine whether an exception occurred during an update operation.


<%@ 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">

  void CustomersGridView_RowUpdated(Object sender, GridViewUpdatedEventArgs e)
  {
    // Use the Exception property to determine whether an exception
    // occurred during the update operation.
    if (e.Exception == null)
    {
      // Sometimes an error might occur that does not raise an 
      // exception, but prevents the update operation from 
      // completing. Use the AffectedRows property to determine 
      // whether the record was actually updated. 
      if (e.AffectedRows == 1)
      {
        // Use the Keys property to get the value of the key field.
        String keyFieldValue = e.Keys["CustomerID"].ToString();

        // Display a confirmation message.
        Message.Text = "Record " + keyFieldValue +
          " updated successfully. ";

        // Display the new and original values.
        DisplayValues((OrderedDictionary)e.NewValues, (OrderedDictionary)e.OldValues);
      }
      else
      {
        // Display an error message.
        Message.Text = "An error occurred during the update operation.";

        // When an error occurs, keep the GridView
        // control in edit mode.
        e.KeepInEditMode = true;
      }
    }
    else
    {
      // Insert the code to handle the exception.
      Message.Text = e.Exception.Message;

      // Use the ExceptionHandled property to indicate that the 
      // exception is already handled.
      e.ExceptionHandled = true;

      e.KeepInEditMode = true;
    }
  }

  void DisplayValues(OrderedDictionary newValues, OrderedDictionary oldValues)
  {

    Message.Text += "<br/></br>";

    // Iterate through the new and old values. Display the
    // values on the page.
    for (int i = 0; i < oldValues.Count; i++)
    {
      Message.Text += "Old Value=" + oldValues[i].ToString() +
        ", New Value=" + newValues[i].ToString() + "<br/>";
    }

    Message.Text += "</br>";

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridViewUpdatedEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridViewUpdatedEventArgs Example</h3>
            
      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogenerateeditbutton="true"
        allowpaging="true" 
        datakeynames="CustomerID"
        onrowupdated="CustomersGridView_RowUpdated" 
        runat="server">
      </asp:gridview>
      
      <br/>
      
      <asp:label id="Message"
        forecolor="Red"          
        runat="server"/>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>


<%@ 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">

  Sub CustomersGridView_RowUpdated(sender As Object, e As GridViewUpdatedEventArgs)
  
    ' Use the Exception property to determine whether an exception
    ' occurred during the update operation.
    If e.Exception Is Nothing Then
    
      ' Sometimes an error might occur that does not raise an 
      ' exception, but prevents the update operation from 
      ' completing. Use the AffectedRows property to determine 
      ' whether the record was actually updated. 
      If e.AffectedRows = 1 Then
      
        ' Use the Keys property to get the value of the key field.
        Dim keyFieldValue As String = e.Keys("CustomerID").ToString()

        ' Display a confirmation message.
        Message.Text = "Record " & keyFieldValue & _
          " updated successfully. "

        ' Display the new and original values.
        DisplayValues(CType(e.NewValues, OrderedDictionary), CType(e.OldValues, OrderedDictionary))

      Else
      
        ' Display an error message.
        Message.Text = "An error occurred during the update operation."

        ' When an error occurs, keep the GridView
        ' control in edit mode.
        e.KeepInEditMode = True
        
      End If
    
    Else
    
      ' Insert the code to handle the exception.
      Message.Text = e.Exception.Message

      ' Use the ExceptionHandled property to indicate that the 
      ' exception is already handled.
      e.ExceptionHandled = True

      e.KeepInEditMode = True
    
    End If
  
  End Sub

  Sub DisplayValues(ByVal newValues As OrderedDictionary, ByVal oldValues As OrderedDictionary)

    Message.Text &= "<br/></br>"

    ' Iterate through the new and old values. Display the
    ' values on the page.
    Dim i As Integer
    For i = 0 To oldValues.Count - 1
    
      Message.Text &= "Old Value=" & oldValues(i).ToString() & _
        ", New Value=" & newValues(i).ToString() & "<br/>"
    Next

    Message.Text &= "</br>"

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridViewUpdatedEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridViewUpdatedEventArgs Example</h3>
            
      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogenerateeditbutton="true"
        allowpaging="true" 
        datakeynames="CustomerID"
        onrowupdated="CustomersGridView_RowUpdated" 
        runat="server">
      </asp:gridview>
      
      <br/>
      
      <asp:label id="Message"
        forecolor="Red"          
        runat="server"/>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

注釈

GridView生成を制御、RowUpdatedした後、イベントと、コントロールの Update ボタンがクリックされた、GridViewコントロールは、レコードを更新します。The GridView control raises the RowUpdated event when an Update button in the control is clicked, but after the GridView control updates the record. (Update ボタンがいるボタン コントロールCommandNameプロパティが「更新」に設定します)。更新操作の結果を確認するなど、このイベントが発生するたびに、カスタムのルーチンを実行できます。(An Update button is a button control whose CommandName property is set to "Update".) You can perform a custom routine whenever this event occurs, such as checking the results of an update operation.

AGridViewUpdatedEventArgsオブジェクトは、イベント ハンドラーと発生した例外の影響を受けたレコードの数を決定することができます。A GridViewUpdatedEventArgs object is passed to the event handler, which enables you to determine the number of records that were affected and any exceptions that might have occurred. 更新操作によって影響を受けたレコードの数を調べるには、AffectedRowsプロパティ。To determine the number of records that were affected by the update operation, use the AffectedRows property. 任意の例外が発生したかどうかを調べるには、Exceptionプロパティ。To determine whether any exceptions occurred, use the Exception property. かどうか、例外によって処理されたイベント ハンドラーの設定を指定することも、ExceptionHandledプロパティ。You can also indicate whether the exception was handled in the event handler by setting the ExceptionHandled property.

更新されたレコードのキー フィールドの値にアクセスするには、使用、Keysプロパティ。To access the key field values for the updated record, use the Keys property. 元のキー以外のフィールド値を使用してアクセスすることができます、OldValuesプロパティ。You can access the original non-key field values by using the OldValues property. 更新された非キー フィールドの値を使用してアクセスすることができます、NewValuesプロパティ。You can access the updated non-key field values by using the NewValues properties.

既定で、GridView更新操作の後読み取り専用モードに制御が戻ります。By default, the GridView control returns to read-only mode after an update operation. 更新操作中に発生した例外を処理する場合、保持できます、GridView設定の編集モードでコントロール、KeepInEditModeプロパティをtrueします。When you handle an exception that occurred during the update operation, you can keep the GridView control in edit mode by setting the KeepInEditMode property to true.

イベントを処理する方法の詳細については、次を参照してください。処理とイベントの発生します。For more information about how to handle events, see Handling and Raising Events.

GridViewUpdatedEventArgs クラスのインスタンスの初期プロパティ値一覧については、GridViewUpdatedEventArgs コンストラクターに関するトピックを参照してください。For a list of initial property values for an instance of the GridViewUpdatedEventArgs class, see the GridViewUpdatedEventArgs constructor.

コンストラクター

GridViewUpdatedEventArgs(Int32, Exception) GridViewUpdatedEventArgs(Int32, Exception) GridViewUpdatedEventArgs(Int32, Exception) GridViewUpdatedEventArgs(Int32, Exception)

GridViewUpdatedEventArgs クラスの新しいインスタンスを初期化します。Initializes a new instance of the GridViewUpdatedEventArgs class.

プロパティ

AffectedRows AffectedRows AffectedRows AffectedRows

更新操作の影響を受けた行の数を取得します。Gets the number of rows that were affected by the update operation.

Exception Exception Exception Exception

更新操作中に例外が発生した場合は、その例外を取得します。Gets the exception (if any) that was raised during the update operation.

ExceptionHandled ExceptionHandled ExceptionHandled ExceptionHandled

更新操作中に発生した例外がイベント ハンドラーで処理されたかどうかを示す値を取得または設定します。Gets or sets a value that indicates whether an exception that was raised during the update operation was handled in the event handler.

KeepInEditMode KeepInEditMode KeepInEditMode KeepInEditMode

更新操作後に GridView コントロールを編集モードのまま維持するかどうかを示す値を取得または設定します。Gets or sets a value that indicates whether the GridView control should remain in edit mode after an update operation.

Keys Keys Keys Keys

更新されたレコードのキー フィールドの名前と値のペアが格納されているディクショナリを取得します。Gets a dictionary that contains the key field name/value pairs for the updated record.

NewValues NewValues NewValues NewValues

更新されたレコードの新しいフィールドの名前と値のペアが格納されているディクショナリを取得します。Gets a dictionary that contains the new field name/value pairs for the updated record.

OldValues OldValues OldValues OldValues

更新されたレコードの元のフィールドの名前と値のペアが格納されているディクショナリを取得します。Gets a dictionary that contains the original field name/value pairs for the updated record.

メソッド

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

指定したオブジェクトが、現在のオブジェクトと等しいかどうかを判断します。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

既定のハッシュ関数として機能します。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

現在のインスタンスの Type を取得します。Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

現在のオブジェクトを表す文字列を返します。Returns a string that represents the current object.

(Inherited from Object)

適用対象

こちらもご覧ください