GridViewUpdateEventArgs.OldValues Özellik

Tanım

Güncelleştirilecek satırdaki özgün alan adı/değer çiftlerini içeren bir sözlük alır.

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

Özellik Değeri

IOrderedDictionary Güncelleştirilecek satırdaki alan adı/değer çiftlerinin özgün değerlerini içeren bir nesne.

Örnekler

Aşağıdaki örnekte, güncelleştirilecek satırdaki alanların özgün değerlerine erişmek için özelliğinin nasıl kullanılacağı OldValues gösterilmektedir. Değerler daha sonra güncelleştirilmiş kayıtların günlük dosyasına yazılır.


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

Açıklamalar

Güncelleştirilecek satırdaki OldValues alanların özgün değerlerine erişmek için özelliğini (sözlük) kullanın. Bu sözlük, satırdaki anahtar alanları dışındaki tüm alanları içerir. Anahtar alanları bir GridView denetimin DataKeyNames özelliğinde tanımlanır.

Not

özelliğini kullanarak Keys satırın anahtar alanlarına erişebilirsiniz. Satırdaki anahtar olmayan alanların düzeltilmiş değerlerine erişmek için özelliğini kullanın NewValues .

OldValues özelliği, satırdaki tüm alan adı/değer çiftlerinin özgün değerleriyle otomatik olarak doldurulur. Satırdaki her alan için özelliğine OldValues ayrı bir giriş eklenir.

Bir girdinin alan adını belirlemek için, sözlükte OldValues bulunan bir System.Collections.DictionaryEntry nesnenin özelliğini kullanınDictionaryEntry.Key. Bir girdinin değerini belirlemek için özelliğini kullanın DictionaryEntry.Value .

Şunlara uygulanır

Ayrıca bkz.