GridViewUpdatedEventArgs クラス

定義

RowUpdated イベントのデータを提供します。

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

次の例は、更新操作中に例外が発生したかどうかを判断する方法を示しています。


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

注釈

コントロール内 GridViewRowUpdated [更新] ボタンがクリックされると、コントロールはイベントを発生させますが、コントロールがレコードを GridView 更新した後に発生します。 ([更新] ボタンは、プロパティが CommandName "Update" に設定されているボタン コントロールです)。更新操作の結果を確認するなど、このイベントが発生するたびにカスタム ルーチンを実行できます。

GridViewUpdatedEventArgsオブジェクトはイベント ハンドラーに渡されます。これにより、影響を受けたレコードの数と発生した可能性のある例外を確認できます。 更新操作の影響を受けたレコードの数を確認するには、 プロパティを AffectedRows 使用します。 例外が発生したかどうかを確認するには、 プロパティを使用します Exception 。 また、 プロパティを設定 ExceptionHandled することで、イベント ハンドラーで例外が処理されたかどうかを示すこともできます。

更新されたレコードのキー フィールド値にアクセスするには、 プロパティを Keys 使用します。 プロパティを使用して、元のキー以外のフィールド値に OldValues アクセスできます。 更新されたキー以外のフィールド値には、 プロパティを NewValues 使用してアクセスできます。

既定では、コントロールは GridView 更新操作の後に読み取り専用モードに戻ります。 更新操作中に発生した例外を処理する場合は、 プロパティtrueGridView に設定することで、コントロールを編集モードにKeepInEditMode保つことができます。

イベントを処理する方法の詳細については、次を参照してください。処理とイベントの発生します。

GridViewUpdatedEventArgs クラスのインスタンスの初期プロパティ値一覧については、GridViewUpdatedEventArgs コンストラクターに関するトピックを参照してください。

コンストラクター

GridViewUpdatedEventArgs(Int32, Exception)

GridViewUpdatedEventArgs クラスの新しいインスタンスを初期化します。

プロパティ

AffectedRows

更新操作の影響を受けた行の数を取得します。

Exception

更新操作中に例外が発生した場合は、その例外を取得します。

ExceptionHandled

更新操作中に発生した例外がイベント ハンドラーで処理されたかどうかを示す値を取得または設定します。

KeepInEditMode

更新操作後に GridView コントロールを編集モードのまま維持するかどうかを示す値を取得または設定します。

Keys

更新されたレコードのキー フィールドの名前と値のペアが格納されているディクショナリを取得します。

NewValues

更新されたレコードの新しいフィールドの名前と値のペアが格納されているディクショナリを取得します。

OldValues

更新されたレコードの元のフィールドの名前と値のペアが格納されているディクショナリを取得します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください