ListViewUpdateEventArgs クラス

定義

ItemUpdating イベントのデータを提供します。

public ref class ListViewUpdateEventArgs : System::ComponentModel::CancelEventArgs
public class ListViewUpdateEventArgs : System.ComponentModel.CancelEventArgs
type ListViewUpdateEventArgs = class
    inherit CancelEventArgs
Public Class ListViewUpdateEventArgs
Inherits CancelEventArgs
継承
ListViewUpdateEventArgs

次の例は、 プロパティを NewValues 使用して、データ ソースが更新される前にすべての値をユーザーが指定したことを確認する方法を示しています。

重要

この例には、ユーザー入力を受け付けるテキスト ボックスがあります。これにより、セキュリティが脆弱になる可能性があります。 既定では、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 ContactsListView_ItemUpdating(Object sender, ListViewUpdateEventArgs e)
  {
    // Cancel the update operation if any of the fields is empty
    // or null.
    foreach (DictionaryEntry de in e.NewValues)
    {
      // Check if the value is null or empty.
      if (de.Value == null || de.Value.ToString().Trim().Length == 0)
      {
        Message.Text = "Cannot set a field to an empty value.";
        e.Cancel = true;
      }
    }
    
    // Convert the email address to lowercase.
    String emailValue = e.NewValues["EmailAddress"].ToString();
    e.NewValues["EmailAddress"] = emailValue.ToLower();

  }
  //</Snippet2>
  
</script>

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

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        OnItemUpdating="ContactsListView_ItemUpdating"  
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" border="1" runat="server" id="tblContacts" width="640px">
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
            <Fields>
              <asp:NextPreviousPagerField 
                ShowFirstPageButton="true" ShowLastPageButton="true"
                FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                NextPageText=" &gt; " PreviousPageText=" &lt; " />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td valign="top">
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
            </td>
            <td>
              <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
            <td>
              <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            </td>
          </tr>
        </ItemTemplate>
        <EditItemTemplate>
          <tr style="background-color:#ADD8E6">
            <td valign="top">
              <asp:Label runat="server" ID="FirstNameLabel" 
                AssociatedControlID="FirstNameTextBox" Text="First Name"/>
              <asp:TextBox ID="FirstNameTextBox" runat="server" Width="200px"
                Text='<%#Bind("FirstName") %>' MaxLength="50" /><br />
              <asp:Label runat="server" ID="LastNameLabel" 
                AssociatedControlID="LastNameTextBox" Text="Last Name" />
              <asp:TextBox ID="LastNameTextBox" runat="server" Width="200px"
                Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
              <asp:Label runat="server" ID="EmailLabel"
                AssociatedControlID="EmailTextBox" Text="Email" />
              <asp:TextBox ID="EmailTextBox" runat="server" Width="200px"
                Text='<%#Bind("EmailAddress") %>' MaxLength="50" />
            </td>
            <td colspan="2" valign="top">
              <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
              <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
            </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="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress] 
          FROM Person.Contact"
        UpdateCommand="UPDATE Person.Contact
          Set [FirstName] = @FirstName, [LastName] = @LastName, [EmailAddress] = @EmailAddress 
          WHERE [ContactID] = @ContactID">
      </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
  
  '<Snippet2>
  Sub ContactsListView_ItemUpdating(ByVal sender As Object, ByVal e As ListViewUpdateEventArgs)
    
    ' Cancel the update operation if any of the fields is empty
    ' or null.
    For Each de As DictionaryEntry In e.NewValues
      ' Check if the value is null or empty
      If de.Value Is Nothing OrElse de.Value.ToString().Trim().Length = 0 Then
        Message.Text = "Cannot set a field to an empty value."
        e.Cancel = True
      End If
    Next
    
    ' Convert the email address to lowercase.
    Dim emailValue As String = e.NewValues("EmailAddress").ToString()    
    e.NewValues("EmailAddress") = emailValue.ToLower()
    
  End Sub
  '</Snippet2>
  
</script>

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

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        OnItemUpdating="ContactsListView_ItemUpdating"  
        runat="server">
        <LayoutTemplate>
          <table cellpadding="2" border="1" runat="server" id="tblContacts" width="640px">
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="PeopleDataPager" PageSize="12">
            <Fields>
              <asp:NextPreviousPagerField 
                ShowFirstPageButton="true" ShowLastPageButton="true"
                FirstPageText="|&lt;&lt; " LastPageText=" &gt;&gt;|"
                NextPageText=" &gt; " PreviousPageText=" &lt; " />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td valign="top">
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
            </td>
            <td>
              <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
            <td>
              <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            </td>
          </tr>
        </ItemTemplate>
        <EditItemTemplate>
          <tr style="background-color:#ADD8E6">
            <td valign="top">
              <asp:Label runat="server" ID="FirstNameLabel" 
                AssociatedControlID="FirstNameTextBox" Text="First Name"/>
              <asp:TextBox ID="FirstNameTextBox" runat="server" Width="200px"
                Text='<%#Bind("FirstName") %>' MaxLength="50" /><br />
              <asp:Label runat="server" ID="LastNameLabel" 
                AssociatedControlID="LastNameTextBox" Text="Last Name" />
              <asp:TextBox ID="LastNameTextBox" runat="server" Width="200px"
                Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
              <asp:Label runat="server" ID="EmailLabel"
                AssociatedControlID="EmailTextBox" Text="Email" />
              <asp:TextBox ID="EmailTextBox" runat="server" Width="200px"
                Text='<%#Bind("EmailAddress") %>' MaxLength="50" />
            </td>
            <td colspan="2" valign="top">
              <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
              <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
            </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="ContactsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT [ContactID], [FirstName], [LastName], [EmailAddress] 
          FROM Person.Contact"
        UpdateCommand="UPDATE Person.Contact
          Set [FirstName] = @FirstName, [LastName] = @LastName, [EmailAddress] = @EmailAddress 
          WHERE [ContactID] = @ContactID">
      </asp:SqlDataSource>
      
    </form>
  </body>
</html>

注釈

コントロールは ListViewItemUpdating メソッドが呼び出されたとき UpdateItem 、またはアイテムの [更新] ボタンがクリックされたときに、コントロールがアイテムを更新する前にイベントを ListView 発生させます。 ([更新] ボタンは、プロパティが CommandName "Update" に設定されているボタンです)。これにより、更新操作のキャンセルなど、このイベントが発生するたびにカスタム ルーチンを実行するイベント処理メソッドを提供できます。

ListViewUpdateEventArgsオブジェクトは、イベント処理メソッドに渡されます。 このオブジェクトを使用すると、現在のアイテムのインデックスを決定し、更新操作を取り消す必要があることを指定できます。 更新操作を取り消すには、 オブジェクトの プロパティを CancelListViewUpdateEventArgstrue設定します。 値がデータ ソースにKeysOldValues渡される前に、および NewValues コレクションを操作することもできます。 これらのコレクションを使用する一般的な方法は、これらの値がデータ ソースに格納される前に、ユーザーが指定した値を検証または HTML エンコードすることです。 これは、スクリプトインジェクション攻撃を防ぐのに役立ちます。

ListViewUpdateEventArgs クラスのインスタンスの初期プロパティ値一覧については、ListViewSelectEventArgs コンストラクターに関するトピックを参照してください。

コンストラクター

ListViewUpdateEventArgs(Int32)

ListViewUpdateEventArgs クラスの新しいインスタンスを初期化します。

プロパティ

Cancel

イベントをキャンセルするかどうかを示す値を取得または設定します。

(継承元 CancelEventArgs)
ItemIndex

更新されているデータ項目のインデックスを取得します。

Keys

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

NewValues

更新する項目の改訂後の値が格納されているディクショナリを取得します。

OldValues

更新する項目の元の値が格納されているディクショナリを取得します。

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください