DetailsViewUpdatedEventArgs 类

定义

ItemUpdated 事件提供数据。

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

示例

下面的代码示例演示如何使用 DetailsViewUpdatedEventArgs 传递给 事件的事件处理程序 ItemUpdated 的对象来确定更新操作期间是否发生了异常。


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

注解

DetailsView当“更新”按钮 (属性设置为“Update”的按钮CommandName时,控件将引发 ItemUpdated 事件,) 在控件内单击,但在控件更新记录之后DetailsView。 这允许你提供一个事件处理程序,用于执行自定义例程,例如,每当发生此事件时检查更新操作的结果。

对象 DetailsViewUpdatedEventArgs 将传递给事件处理程序,这样就可以确定受影响的记录数以及可能发生的任何异常。 若要确定受更新操作影响的记录数,请使用 AffectedRows 属性。 Exception使用 属性可确定是否发生任何异常。 还可以通过设置 属性来指示是否在事件处理程序中处理了异常 ExceptionHandled 。 如果需要访问已更新记录的键字段值,请使用 Keys 属性。 可以分别使用 OldValuesNewValues 属性访问原始和更新的非键字段值。

默认情况下,控件在 DetailsView 更新操作后返回到 属性指定的 DefaultMode 模式。 若要使 DetailsView 控件保持编辑模式,请将 KeepInEditMode 属性设置为 true

有关如何处理事件的详细信息,请参阅 处理和引发事件

有关 DetailsViewUpdatedEventArgs 类的实例的初始属性值列表,请参见 DetailsViewUpdatedEventArgs 构造函数。

构造函数

DetailsViewUpdatedEventArgs(Int32, Exception)

初始化 DetailsViewUpdatedEventArgs 类的新实例。

属性

AffectedRows

获取受更新操作影响的行数。

Exception

获取更新操作过程中引发的异常(如果有)。

ExceptionHandled

获取或设置一个值,该值指示在更新操作过程中所引发的异常是否已在事件处理程序中得到处理。

KeepInEditMode

获取或设置一个值,该值指示在更新操作完成后 DetailsView 控件是否仍保持在编辑模式下。

Keys

获取一个字典,其中包含被更新记录的键字段名称/值对。

NewValues

获取一个字典,该字典包含已更新记录的新字段的名称/值对。

OldValues

获取一个字典,其中包含被更新记录的原始键字段名称/值对。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅