Export (0) Print
Expand All

GridViewUpdateEventArgs.Keys Property

Gets a dictionary of field name/value pairs that represent the primary key of the row to update.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public IOrderedDictionary Keys { get; }

Property Value

Type: System.Collections.Specialized.IOrderedDictionary
A System.Collections.Specialized.IOrderedDictionary object containing field name/value pairs that represent the primary key of the row to update.

When the DataKeyNames property of a GridView control is set, use the Keys property (dictionary) to access the value of the primary key in the row to update.

NoteNote

To access the values of the non-key fields, use the NewValues or OldValues properties. The NewValues property contains the updated values, while the OldValues property contains the original values.

The Keys dictionary is automatically populated with the name/value pairs of the field or fields specified in the DataKeyNames property. If multiple fields form the primary key, a separate entry is added to the Keys dictionary for each key field.

To determine the name of a key field, use the DictionaryEntry.Key property of a System.Collections.DictionaryEntry object contained in the Keys dictionary. To determine the value of a key field, use the DictionaryEntry.Value property.

The following example demonstrates how to use the Keys property to access the values of the key fields in the row to update. The values are then written to a log file of updated records.


<%@ 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 key field values to the log text.
        foreach (DictionaryEntry keyEntry in e.Keys)
        {
            logText += keyEntry.Key + "=" + keyEntry.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 Keys Example</title>
</head>
<body>
        <form id="Form1" runat="server">

            <h3>GridViewUpdateEventArgs Keys 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>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft