DetailsViewUpdatedEventArgs 类

定义

ItemUpdated 事件提供数据。Provides data for the ItemUpdated event.

public ref class DetailsViewUpdatedEventArgs : EventArgs
public class DetailsViewUpdatedEventArgs : EventArgs
type DetailsViewUpdatedEventArgs = class
    inherit EventArgs
Public Class DetailsViewUpdatedEventArgs
Inherits EventArgs
继承
DetailsViewUpdatedEventArgs

示例

下面的代码示例演示如何使用DetailsViewUpdatedEventArgs传递给ItemUpdated事件的事件处理程序的对象来确定是否在更新操作过程中发生了异常。The following code example demonstrates how to use the DetailsViewUpdatedEventArgs object passed to the event handler for the ItemUpdated event 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 CustomerDetailsView_ItemUpdated(Object sender, 
    DetailsViewUpdatedEventArgs e)
  {
    // Use the Exception property to determine whether an exception
    // occurred during the insert operation.
    if (e.Exception == null)
    {
      // Use the Values property to get the value entered by 
      // the user for the CompanyName field.
      String keyFieldValue = e.Keys["CustomerID"].ToString();

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

      // Display the old and new values.
      DisplayValues(e);

      if (e.AffectedRows == 1)
      {
        MessageLabel.Text += e.AffectedRows.ToString() + 
          " record updated.";
      }
      else
      {
        MessageLabel.Text += e.AffectedRows.ToString() + 
          " records updated.";
      }
    }
    else
    {
      // Insert the code to handle the exception.
      MessageLabel.Text = e.Exception.Message;

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

      // When an exception occurs, keep the DetailsView
      // control in edit mode.
      e.KeepInEditMode = true;
    }
  }

  void DisplayValues(DetailsViewUpdatedEventArgs e)
  {
    
    MessageLabel.Text += "<br/></br>";
    
    // Iterate through the OldValue and NewValues
    // properties and display the values.
    for (int i = 0; i < e.OldValues.Count; i++)
    {
      MessageLabel.Text += "Old Value=" + e.OldValues[i].ToString() +
        ", New Value=" + e.NewValues[i].ToString() + "<br/>";
    }

    MessageLabel.Text += "</br>";
    
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

  <head runat="server">
    <title>DetailsViewUpdatedEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DetailsViewUpdatedEventArgs Example</h3>
                       
        <asp:detailsview id="CustomerDetailsView"
          datasourceid="DetailsViewSource"
          autogeneraterows="true"
          autogenerateeditbutton="true"  
          allowpaging="true"
          datakeynames="CustomerID" 
          onitemupdated="CustomerDetailsView_ItemUpdated"
          runat="server">
            
          <pagersettings position="Bottom"/> 
                    
        </asp:detailsview>
        
        <br/>
        
        <asp:label id="MessageLabel"
          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="DetailsViewSource"
          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"/>
            
      </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 CustomerDetailsView_ItemUpdated(ByVal sender As Object, ByVal e As DetailsViewUpdatedEventArgs)
  
    ' Use the Exception property to determine whether an exception
    ' occurred during the insert operation.
    If e.Exception Is Nothing Then
    
      ' Use the Values property to get the value entered by 
      ' the user for the CompanyName field.
      Dim keyFieldValue As String = e.Keys("CustomerID").ToString()

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

      ' Display the old and new values.
      DisplayValues(e)

      If e.AffectedRows = 1 Then

        MessageLabel.Text &= e.AffectedRows.ToString() & _
          " record updated."
      
      Else
      
        MessageLabel.Text &= e.AffectedRows.ToString() & _
          " records updated."
      
      End If
    
    Else
    
      ' Insert the code to handle the exception.
      MessageLabel.Text = e.Exception.Message

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

      ' When an exception occurs, keep the DetailsView
      ' control in edit mode.
      e.KeepInEditMode = True
    
    End If
    
  End Sub

  Sub DisplayValues(ByVal e As DetailsViewUpdatedEventArgs)
    
    MessageLabel.Text &= "<br/></br>"
    
    ' Iterate through the OldValue and NewValues
    ' properties and display the values.
    Dim i As Integer
        
    For i = 0 To e.OldValues.Count - 1
    
      MessageLabel.Text &= "Old Value=" & e.OldValues(i).ToString() & _
        ", New Value=" & e.NewValues(i).ToString() & "<br/>"
    
    Next

    MessageLabel.Text &= "</br>"
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >

  <head runat="server">
    <title>DetailsViewUpdatedEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>DetailsViewUpdatedEventArgs Example</h3>
                       
        <asp:detailsview id="CustomerDetailsView"
          datasourceid="DetailsViewSource"
          autogeneraterows="true"
          autogenerateeditbutton="true"  
          allowpaging="true"
          datakeynames="CustomerID" 
          onitemupdated="CustomerDetailsView_ItemUpdated"
          runat="server">
            
          <pagersettings position="Bottom"/> 
                    
        </asp:detailsview>
        
        <br/>
        
        <asp:label id="MessageLabel"
          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="DetailsViewSource"
          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"/>
            
      </form>
  </body>
</html>

注解

当单击控件中ItemUpdated的 "更新" 按钮 (其CommandName属性设置为 "Update") 时, DetailsView控件将引发事件, 但在控件更新记录后。 DetailsViewThe DetailsView control raises the ItemUpdated event when an Update button (a button with its CommandName property set to "Update") within the control is clicked, but after the DetailsView control updates the record. 这使您可以提供一个事件处理程序, 以便在每次发生此事件时执行自定义例程, 如检查更新操作的结果。This allows you to provide an event handler that performs a custom routine, such as checking the results of an update operation, whenever this event occurs.

DetailsViewUpdatedEventArgs对象传递到事件处理程序, 该处理程序允许您确定受影响的记录数以及可能已发生的任何异常。A DetailsViewUpdatedEventArgs object is passed to the event handler, which allows you to determine the number of records affected and any exceptions that might have occurred. 若要确定受更新操作影响的记录数, 请使用AffectedRows属性。To determine the number of records affected by the update operation, use the AffectedRows property. Exception使用属性可确定是否发生了异常。Use the Exception property to determine whether any exceptions occurred. 还可以通过设置ExceptionHandled属性, 指示是否已在事件处理程序中处理异常。You can also indicate whether the exception was handled in the event handler by setting the ExceptionHandled property. 如果需要访问已更新记录的键字段值, 请使用Keys属性。If you need to access the key field values for the updated record, use the Keys property. 可以分别使用OldValuesNewValues属性访问原始的和更新的非键字段值。The original and updated non-key field values can be accessed using the OldValues and NewValues properties, respectively.

默认情况下, DetailsView该控件返回到更新操作后DefaultMode由属性指定的模式。By default, the DetailsView control returns to the mode specified by the DefaultMode property after an update operation. 若要使DetailsView控件处于编辑模式, 请KeepInEditMode将属性设置true为。To keep the DetailsView control in edit mode, set the KeepInEditMode property to true.

有关如何处理事件的详细信息, 请参阅处理和引发事件For more information about how to handle events, see Handling and Raising Events.

有关 DetailsViewUpdatedEventArgs 类的实例的初始属性值列表,请参见 DetailsViewUpdatedEventArgs 构造函数。For a list of initial property values for an instance of the DetailsViewUpdatedEventArgs class, see the DetailsViewUpdatedEventArgs constructor.

构造函数

DetailsViewUpdatedEventArgs(Int32, Exception)

初始化 DetailsViewUpdatedEventArgs 类的新实例。Initializes a new instance of the DetailsViewUpdatedEventArgs class.

属性

AffectedRows

获取受更新操作影响的行数。Gets the number of rows affected by the update operation.

Exception

获取更新操作过程中引发的异常(如果有)。Gets the exception (if any) that was raised during the update operation.

ExceptionHandled

获取或设置一个值,该值指示在更新操作过程中所引发的异常是否已在事件处理程序中得到处理。Gets or sets a value indicating whether an exception that was raised during the update operation was handled in the event handler.

KeepInEditMode

获取或设置一个值,该值指示在更新操作完成后 DetailsView 控件是否仍保持在编辑模式下。Gets or sets a value indicating whether the DetailsView control should remain in edit mode after an update operation.

Keys

获取一个字典,其中包含被更新记录的键字段名称/值对。Gets a dictionary that contains the key field name/value pairs for the updated record.

NewValues

获取一个字典,该字典包含已更新记录的新字段的名称/值对。Gets a dictionary that contains the new field name/value pairs for the updated record.

OldValues

获取一个字典,其中包含被更新记录的原始键字段名称/值对。Gets a dictionary that contains the original field name/value pairs for the updated record.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅