ListViewUpdateEventArgs.Keys プロパティ

定義

更新する項目の主キーを表すフィールドの名前と値のペアのディクショナリを取得します。

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

プロパティ値

更新する項目の主キーを表すフィールドの名前と値のペア。

次の例では、 プロパティを Keys 使用して、更新するレコードのキー フィールドの値にアクセスする方法を示します。

重要

この例には、ユーザー入力を受け付けるテキスト ボックスがあります。これにより、セキュリティが脆弱になる可能性があります。 既定では、ASP.NET Web ページによって、ユーザー入力にスクリプトまたは HTML 要素が含まれていないかどうかが検証されます。 詳細については、「スクリプトによる攻略の概要」を参照してください。

<%@ 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 Page_Load()
  {
    Message.Text = String.Empty;
  }
    
  //<Snippet2>
  void UnitMeasureListView_ItemUpdating(Object sender, ListViewUpdateEventArgs e)
  {
    // Use the Keys property to retrieve the key field value
    String keyValue = e.Keys[0].ToString().Trim();

    // Cancel the update operation if the user attempts to 
    // edit a protected record. In this example, unit measure
    // codes with 3 letters are protected.
    if (keyValue.Length == 3)
    {
      Message.Text = "You cannot update this record. " +
        " Unit Measure Code " + keyValue + " is protected.";
        e.Cancel = true;
    }    
  }
  //</Snippet2>
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListViewUpdateEventArgs Keys Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListViewUpdateEventArgs Keys Example</h3>
            
      <asp:Label ID="Message"
        ForeColor="Red"          
        runat="server"/>
      <br/>

      <asp:ListView ID="UnitMeasureListView" 
        DataSourceID="UnitMeasureDataSource" 
        DataKeyNames="UnitMeasureCode"
        OnItemUpdating="UnitMeasureListView_ItemUpdating"  
        runat="server">
        <LayoutTemplate>
          <table width="400px" border="1" id="tblUnit">
            <tr runat="server">
              <th runat="server">&nbsp;</th>
              <th runat="server">Unit Measure Code</th>
              <th runat="server">Name</th>
            </tr>
            <tr id="itemPlaceholder" runat="server"></tr>
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            </td>
            <td>
              <asp:Label ID="CodeLabel" runat="server" Text='<%#Eval("UnitMeasureCode") %>' />
            </td>
            <td>
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("Name") %>' />
            </td>
          </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
          <tr style="background-color:#B0C4DE" runat="server">
            <td>
              <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            </td>
            <td>
              <asp:Label ID="CodeLabel" runat="server" Text='<%#Eval("UnitMeasureCode") %>' />
            </td>
            <td>
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("Name") %>' />
            </td>
          </tr>
        </AlternatingItemTemplate>
        <EditItemTemplate>
          <tr style="background-color:#4682B4">
            <td>
              <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
              <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
            </td>
            <td>
              <asp:Label ID="CodeLabel" runat="server" Text='<%#Eval("UnitMeasureCode") %>' />
            </td>
            <td>
              <asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' MaxLength="50" /><br />
            </td>
          </tr>
        </EditItemTemplate>
      </asp:ListView>

      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="UnitMeasureDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [UnitMeasureCode], [Name] FROM Production.UnitMeasure"
        UpdateCommand="UPDATE Production.UnitMeasure
                       SET [Name] = @Name
                       WHERE [UnitMeasureCode] = @UnitMeasureCode">
      </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 Page_Load() 
      Message.Text = String.Empty
  End Sub 'Page_Load

  Sub UnitMeasureListView_ItemUpdating(ByVal sender As Object, ByVal e As ListViewUpdateEventArgs)
    ' Use the Keys property to retrieve the key field value
    Dim keyValue As String = e.Keys(0).ToString().Trim()
    
    ' Cancel the update operation if the user attempts to 
    ' edit a protected record. In this example, unit measure
    ' codes with 3 letters are protected.
    If keyValue.Length = 3 Then
      Message.Text = "You cannot update this record. " & _
        " Unit Measure Code " & keyValue & " is protected."
      e.Cancel = True
    End If

  End Sub 'UnitMeasureListView_ItemUpdating

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListViewUpdateEventArgs Keys Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListViewUpdateEventArgs Keys Example</h3>
            
      <asp:Label ID="Message"
        ForeColor="Red"          
        runat="server"/>
      <br/>

      <asp:ListView ID="UnitMeasureListView" 
        DataSourceID="UnitMeasureDataSource" 
        DataKeyNames="UnitMeasureCode"
        OnItemUpdating="UnitMeasureListView_ItemUpdating"  
        runat="server">
        <LayoutTemplate>
          <table width="400px" border="1" id="tblUnit">
            <tr runat="server">
              <th runat="server">&nbsp;</th>
              <th runat="server">Unit Measure Code</th>
              <th runat="server">Name</th>
            </tr>
            <tr id="itemPlaceholder" runat="server"></tr>
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            </td>
            <td>
              <asp:Label ID="CodeLabel" runat="server" Text='<%#Eval("UnitMeasureCode") %>' />
            </td>
            <td>
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("Name") %>' />
            </td>
          </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
          <tr style="background-color:#B0C4DE" runat="server">
            <td>
              <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            </td>
            <td>
              <asp:Label ID="CodeLabel" runat="server" Text='<%#Eval("UnitMeasureCode") %>' />
            </td>
            <td>
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("Name") %>' />
            </td>
          </tr>
        </AlternatingItemTemplate>
        <EditItemTemplate>
          <tr style="background-color:#4682B4">
            <td>
              <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
              <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
            </td>
            <td>
              <asp:Label ID="CodeLabel" runat="server" Text='<%#Eval("UnitMeasureCode") %>' />
            </td>
            <td>
              <asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' MaxLength="50" /><br />
            </td>
          </tr>
        </EditItemTemplate>
      </asp:ListView>

      <!-- This example uses Microsoft SQL Server and connects      -->
      <!-- to the AdventureWorks sample database. Use an ASP.NET    -->
      <!-- expression to retrieve the connection string value       -->
      <!-- from the Web.config file.                                -->
      <asp:SqlDataSource ID="UnitMeasureDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [UnitMeasureCode], [Name] FROM Production.UnitMeasure"
        UpdateCommand="UPDATE Production.UnitMeasure
                       SET [Name] = @Name
                       WHERE [UnitMeasureCode] = @UnitMeasureCode">
      </asp:SqlDataSource>
      
    </form>
  </body>
</html>

注釈

コントロールの プロパティがDataKeyNamesListView設定されている場合は、 プロパティ (ディクショナリ) をKeys使用して、更新するアイテムの主キーの値にアクセスします。

注意

キー以外のフィールドの値にアクセスするには、 プロパティまたは OldValues プロパティをNewValues使用します。 プロパティには NewValues 更新された値が含まれており OldValues 、プロパティには元の値が含まれています。

ディクショナリには Keys 、 プロパティで指定されたフィールドの名前と値のペアが自動的に DataKeyNames 設定されます。 複数のフィールドが主キーを形成する場合、各キー フィールドのディクショナリに個別の Keys エントリが追加されます。

キー フィールドの名前を確認するには、ディクショナリに DictionaryEntry.Key 含まれているオブジェクトの DictionaryEntry プロパティを Keys 使用します。 キー フィールドの値を確認するには、 プロパティを DictionaryEntry.Value 使用します。

適用対象

こちらもご覧ください