GridViewUpdateEventArgs.OldValues 속성

정의

업데이트할 행에 있는 필드의 원래 이름/값 쌍이 들어 있는 사전을 가져옵니다.

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

업데이트할 행에 있는 필드 이름/값 쌍의 원래 값이 들어 있는 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 업데이트할 행에 있는 필드의 원래 값에 액세스 하려면 속성 (사전)입니다. 이 사전의 키 필드를 제외한 행의 모든 필드를 포함 합니다. 키 필드에 정의 되어는 DataKeyNames 의 속성을 GridView 제어 합니다.

참고

행의 키 필드를 사용 하 여 액세스할 수 있습니다는 Keys 속성입니다. 행 키가 아닌 필드의 수정된 된 값에 액세스 하려면 사용 된 NewValues 속성입니다.

OldValues 행의 모든 필드 이름/값 쌍의 원래 값을 사용 하 여 속성이 자동으로 채워집니다. 별도 항목에 추가 되는 OldValues 행의 각 필드에 대 한 속성입니다.

항목의 필드 이름을 확인 하려면 사용 합니다 DictionaryEntry.Key 의 속성을 System.Collections.DictionaryEntry 에 포함 된 개체는 OldValues 사전. 항목의 값을 확인 하려면 사용 된 DictionaryEntry.Value 속성입니다.

적용 대상

추가 정보