FormViewDeleteEventArgs.Keys 屬性

定義

取得要刪除資料錄中索引鍵欄位名稱/值組的排序字典。Gets an ordered dictionary of key field name/value pairs for the record to delete.

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

屬性值

IOrderedDictionary

OrderedDictionary,包含要刪除資料錄中索引鍵欄位名稱/值組。An OrderedDictionary that contains the key field name/value pairs for the record to delete.

範例

下列範例示範如何使用 Keys 屬性來存取要刪除之記錄的索引鍵欄位值。The following example demonstrates how to use the Keys property to access the value of the key field for the record being deleted.


<%@ 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 EmployeeFormView_ItemDeleting(Object sender, FormViewDeleteEventArgs e)
  {
    // Get the employee ID, name, and job title from the Keys and Values
    // properties.
    String keyValue = e.Keys["EmployeeID"].ToString();
    String employeeName = e.Values["FirstName"].ToString() +
      " " + e.Values["LastName"].ToString();
    String title = e.Values["Title"].ToString();

    // Cancel the delete operation if the user attempts to 
    // delete a protected record. In this example, records for
    // employees with a "Sales Manager" job title are protected.
    if (title.Equals("Sales Manager"))
    {
      e.Cancel = true;
      MessageLabel.Text = "You cannot delete record " +
        e.RowIndex.ToString() + ". " + employeeName +
        " (Employee Number " + keyValue.ToString() +
        ") is protected.";
    }

  }
   
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>FormViewDeleteEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>FormViewDeleteEventArgs Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID"
        onitemdeleting="EmployeeFormView_ItemDeleting"  
        runat="server">
        
        <itemtemplate>
        
          <table>
            <tr>
              <td>
                <asp:image id="EmployeeImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td>
                <asp:label id="FirstNameLabel"
                  text='<%#Bind("FirstName")%>'
                  font-bold="true"
                  runat="server"/>
                <asp:label id="LastNameLabel"
                  text='<%#Bind("LastName")%>'
                  font-bold="true"
                  runat="server"/>
                <br/>     
                <asp:label id="TitleLabel"
                  text='<%#Bind("Title")%>'
                  runat="server"/>        
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:button id="DeleteButton"
                  text="Delete Record"
                  commandname="Delete"
                  runat="server" />
              </td>
            </tr>
          </table>
        
        </itemtemplate>         
                  
      </asp:formview>
      
      <asp:label id="MessageLabel"
        forecolor="Red"
        runat="server"/>
          
      <!-- 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="EmployeeSource"
        selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
        deletecommand="Delete [Employees] Where [EmployeeID]=@EmployeeID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </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 EmployeeFormView_ItemDeleting(ByVal sender As Object, ByVal e As FormViewDeleteEventArgs)
  
    ' Get the employee ID, name, and job title from the Keys and Values
    ' properties.
    Dim keyValue As String = e.Keys("EmployeeID").ToString()
    Dim employeeName As String = e.Values("FirstName").ToString() & _
      " " & e.Values("LastName").ToString()
    Dim title As String = e.Values("Title").ToString()

    ' Cancel the delete operation if the user attempts to 
    ' delete a protected record. In this example, records for
    ' employees with a "Sales Manager" job title are protected.
    If Title.Equals("Sales Manager") Then
    
      e.Cancel = True
      MessageLabel.Text = "You cannot delete record " & _
        e.RowIndex.ToString() & ". " & employeeName & _
        " (Employee Number " & keyValue.ToString() & _
        ") is protected."
    
    End If

  End Sub
   
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>FormViewDeleteEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>FormViewDeleteEventArgs Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID"
        onitemdeleting="EmployeeFormView_ItemDeleting"  
        runat="server">
        
        <itemtemplate>
        
          <table>
            <tr>
              <td>
                <asp:image id="EmployeeImage"
                  imageurl='<%# Eval("PhotoPath") %>'
                  alternatetext='<%# Eval("LastName") %>' 
                  runat="server"/>
              </td>
              <td>
                <asp:label id="FirstNameLabel"
                  text='<%#Bind("FirstName")%>'
                  font-bold="true"
                  runat="server"/>
                <asp:label id="LastNameLabel"
                  text='<%#Bind("LastName")%>'
                  font-bold="true"
                  runat="server"/>
                <br/>     
                <asp:label id="TitleLabel"
                  text='<%#Bind("Title")%>'
                  runat="server"/>        
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:button id="DeleteButton"
                  text="Delete Record"
                  commandname="Delete"
                  runat="server" />
              </td>
            </tr>
          </table>
        
        </itemtemplate>         
                  
      </asp:formview>
      
      <asp:label id="MessageLabel"
        forecolor="Red"
        runat="server"/>
          
      <!-- 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="EmployeeSource"
        selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
        deletecommand="Delete [Employees] Where [EmployeeID]=@EmployeeID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

備註

Keys屬性包含控制項屬性中所列之索引鍵欄位的值 DataKeyNames FormViewThe Keys property contains the values of the key fields listed in the DataKeyNames property of a FormView control. 您可以使用 Keys 屬性來存取要刪除之記錄的索引鍵欄位值或欄位。Use the Keys property to access the values of the key field or fields for the record to be deleted. 例如,您可以使用這些值來確認記錄,然後再加以刪除、變更要刪除的記錄,或保留已刪除記錄的記錄。For example, you can use these values to verify the record before deleting it, to change the record to delete, or to keep a log of deleted records.

注意

這個屬性只包含索引鍵欄位。This property contains only the key fields. 若要存取非索引鍵欄位的名稱/值組值,請使用 Values 屬性。To access the name/value pair values for the non-key fields, use the Values property.

Keys屬性會傳回可執行 OrderedDictionary 介面的物件 System.Collections.Specialized.IOrderedDictionaryThe Keys property returns an OrderedDictionary object that implements the System.Collections.Specialized.IOrderedDictionary interface. OrderedDictionary物件包含 System.Collections.DictionaryEntry 代表索引鍵欄位的物件。The OrderedDictionary object contains System.Collections.DictionaryEntry objects that represent the key fields. 若要存取索引鍵功能變數名稱,請使用 Keys 物件的屬性 OrderedDictionaryTo access the key field names, use the Keys property of the OrderedDictionary object. 同樣地,您可以使用屬性來存取索引鍵域值 ValuesSimilarly, you can access the key field values by using the Values property.

注意

您也可以使用物件的索引子 OrderedDictionary 來存取索引鍵域值,做為快捷方式。As a shortcut, you can also use the indexer of the OrderedDictionary object to access the key field values. 使用索引子的優點是它會直接傳回索引鍵域值。The advantage in using the indexer is that it returns key field values directly. 依賴欄位順序的資料來源控制項 (例如 AccessDataSource) 只能透過索引來存取索引鍵域值。Data source controls that rely on the field order (such as AccessDataSource) can access key field values only by index.

KeysValues 屬性是唯讀的; 不過,您可以修改物件的域值 OrderedDictionaryThe Keys and Values properties are read-only; however, you can modify the field values of the OrderedDictionary object. 如果您變更索引鍵和非索引鍵域值,將會從資料來源中刪除對應的記錄。If you change the key and non-key field values, the corresponding record will be deleted from the data source.

適用於

另請參閱