GridViewUpdateEventArgs.NewValues 屬性

定義

取得包含要更新之資料列中非索引鍵欄位名稱/值組修訂值的字典。Gets a dictionary containing the revised values of the non-key field name/value pairs in the row to update.

public:
 property System::Collections::Specialized::IOrderedDictionary ^ NewValues { System::Collections::Specialized::IOrderedDictionary ^ get(); };
public System.Collections.Specialized.IOrderedDictionary NewValues { get; }
member this.NewValues : System.Collections.Specialized.IOrderedDictionary
Public ReadOnly Property NewValues As IOrderedDictionary

屬性值

IOrderedDictionary

IOrderedDictionary 物件,包含要更新之資料列中非索引鍵欄位名稱/值組的修訂值。An IOrderedDictionary object containing the revised values of the non-key field name/value pairs in the row to update.

範例

下列範例示範如何使用 NewValues 屬性,以 HTML 編碼使用者所提供的所有值,然後更新資料來源。The following example demonstrates how to use the NewValues property to HTML-encode all values supplied by the user before updating the data source.


<%@ 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 CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
  {
     
    // Iterate through the NewValues collection and HTML encode all 
    // user-provided values before updating the data source.
    foreach (DictionaryEntry entry in e.NewValues)
    {
    
      e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString());
    
    }
        
  }
       
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView RowUpdating Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView RowUpdating Example</h3>
            
      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogenerateeditbutton="true"
        allowpaging="true" 
        datakeynames="CustomerID"
        onrowupdating="CustomersGridView_RowUpdating"  
        runat="server">
      </asp:gridview>
            
      <!-- 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="CustomersSqlDataSource"  
        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">
      </asp:sqldatasource>
            
    </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 CustomersGridView_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    
    ' Use the CopyTo method to copy the DictionaryEntry objects in the 
    ' NewValues collection to an array.
    Dim records(e.NewValues.Count - 1) As DictionaryEntry
    e.NewValues.CopyTo(records, 0)
    
    ' Iterate through the array and HTML encode all user-provided values 
    ' before updating the data source.
    Dim entry As DictionaryEntry
    For Each entry In records
            
      e.NewValues(entry.Key) = Server.HtmlEncode(entry.Value.ToString())
      
    Next
        
  End Sub
       
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView RowUpdating Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView RowUpdating Example</h3>
            
      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogenerateeditbutton="true"
        allowpaging="true" 
        datakeynames="CustomerID"
        onrowupdating="CustomersGridView_RowUpdating"  
        runat="server">
      </asp:gridview>
            
      <!-- 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="CustomersSqlDataSource"  
        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">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

備註

您可以使用 NewValues 屬性 (字典) 來存取資料列中修改過之非索引鍵欄位的值以進行更新。Use the NewValues property (dictionary) to access the values of the revised non-key fields in the row to update.

注意

主要索引鍵欄位不包含在此字典中。The primary key field or fields are not included in this dictionary. 若要存取主要索引鍵欄位的值,請使用 Keys 屬性。To access the values of the primary key field or fields, use the Keys property. 若要存取資料列中非索引鍵欄位的原始值,請使用 OldValues 屬性。To access the original values of the non-key fields in the row, use the OldValues property.

NewValues屬性會自動填入資料列中修改過之非索引鍵欄位的名稱/值組。The NewValues property is automatically populated with the name/value pairs of the revised non-key fields in the row. NewValues針對資料列中的每個非索引鍵欄位,將個別的專案加入至屬性。A separate entry is added to the NewValues property for each non-key field in the row.

若要判斷專案的功能變數名稱,請使用 DictionaryEntry.Key System.Collections.DictionaryEntry 字典中包含之物件的屬性 NewValuesTo determine the field name of an entry, use the DictionaryEntry.Key property of a System.Collections.DictionaryEntry object contained in the NewValues dictionary. 若要判斷專案的值,請使用 DictionaryEntry.Value 屬性。To determine the value of an entry, use the DictionaryEntry.Value property.

適用於

另請參閱