GridViewUpdateEventArgs.OldValues 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个字典,该字典包含要更新的行中的原始字段名称/值对。
public:
property System::Collections::Specialized::IOrderedDictionary ^ OldValues { System::Collections::Specialized::IOrderedDictionary ^ get(); };
public System.Collections.Specialized.IOrderedDictionary OldValues { get; }
member this.OldValues : System.Collections.Specialized.IOrderedDictionary
Public ReadOnly Property OldValues As IOrderedDictionary
属性值
一个 IOrderedDictionary 对象,包含要更新的行中的字段名称/值对的原始值。
示例
以下示例演示如何使用 OldValues 属性访问要更新的行中字段的原始值。 然后,这些值将写入更新记录的日志文件。
<%@ Page language="C#" %>
<%@ import namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void EmployeesGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
// Record the update operation in a log file.
// Create the log text.
String logText = "";
// Append the original field values to the log text.
foreach (DictionaryEntry valueEntry in e.OldValues)
{
logText += valueEntry.Key + "=" + valueEntry.Value + ";";
}
// Append the text to a log file.
StreamWriter sw;
sw = File.AppendText(Server.MapPath(null) + "\\updatelog.txt");
sw.WriteLine(logText);
sw.Flush();
sw.Close();
}
void EmployeesGridView_RowUpdated(Object sender, GridViewUpdatedEventArgs e)
{
if (e.Exception == null)
{
// The update operation succeeded. Clear the message label.
Message.Text = "";
}
else
{
// The update operation failed. Display an error message.
Message.Text = e.AffectedRows.ToString() + " rows updated. " + e.Exception.Message;
e.ExceptionHandled = true;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>GridViewUpdateEventArgs OldValues Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>GridViewUpdateEventArgs OldValues Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<!-- The GridView control automatically sets the columns -->
<!-- specified in the datakeynames attribute as read-only. -->
<!-- No input controls are rendered for these columns in -->
<!-- edit mode. -->
<asp:gridview id="EmployeesGridView"
datasourceid="EmployeesSqlDataSource"
DataKeyNames="EmployeeID"
autogenerateeditbutton="True"
onrowupdating="EmployeesGridView_RowUpdating"
onrowupdated="EmployeesGridView_RowUpdated"
runat="server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. -->
<asp:sqldatasource id="EmployeesSqlDataSource"
selectcommand="SELECT [EmployeeID], [LastName], [FirstName], [HireDate] FROM [Employees]"
updatecommand="UPDATE [Employees] SET [LastName] = @LastName, [FirstName] = @FirstName, [HireDate] = @HireDate WHERE [EmployeeID] = @EmployeeID"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
runat="server" >
</asp:sqldatasource>
</form>
</body>
</html>
<%@ Page language="VB" %>
<%@ import namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub EmployeesGridView_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
' Record the update operation in a log file.
' Create the log text.
Dim logText As String = ""
' Append the original field values to the log text.
Dim i As Integer
For i = 0 To e.OldValues.Count - 1
logText += e.OldValues(i) & ";"
Next
' Append the text to a log file.
Dim sw As StreamWriter
sw = File.AppendText(Server.MapPath(Nothing) & "\updatelog.txt")
sw.WriteLine(logText)
sw.Flush()
sw.Close()
End Sub
Sub EmployeesGridView_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)
If e.Exception Is Nothing Then
' The update operation succeeded. Clear the message label.
Message.Text = ""
Else
' The update operation failed. Display an error message.
Message.Text = e.AffectedRows.ToString() & " rows updated. " & e.Exception.Message
e.ExceptionHandled = True
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridViewUpdateEventArgs OldValues Example</title>
</head>
<body>
<form id="Form1" runat="server">
<h3>GridViewUpdateEventArgs OldValues Example</h3>
<asp:label id="Message"
forecolor="Red"
runat="server"/>
<br/>
<!-- The GridView control automatically sets the columns -->
<!-- specified in the datakeynames attribute as read-only. -->
<!-- No input controls are rendered for these columns in -->
<!-- edit mode. -->
<asp:gridview id="EmployeesGridView"
datasourceid="EmployeesSqlDataSource"
DataKeyNames="EmployeeID"
autogenerateeditbutton="True"
onrowupdating="EmployeesGridView_RowUpdating"
onrowupdated="EmployeesGridView_RowUpdated"
runat="server">
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. -->
<asp:sqldatasource id="EmployeesSqlDataSource"
selectcommand="SELECT [EmployeeID], [LastName], [FirstName], [HireDate] FROM [Employees]"
updatecommand="UPDATE [Employees] SET [LastName] = @LastName, [FirstName] = @FirstName, [HireDate] = @HireDate WHERE [EmployeeID] = @EmployeeID"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
runat="server" >
</asp:sqldatasource>
</form>
</body>
</html>
注解
OldValues使用属性 (字典) 访问行中字段的原始值以更新。 此字典包含行中的所有字段,键字段除外。 键字段在控件的属性GridView中DataKeyNames定义。
该 OldValues 属性会自动填充行中所有字段名称/值对的原始值。 将单独的条目添加到 OldValues 行中每个字段的属性中。
若要确定条目的字段名称,请使用DictionaryEntry.Key字典中包含的OldValues对象的属性System.Collections.DictionaryEntry。 若要确定条目的值,请使用 DictionaryEntry.Value 该属性。