ListViewCancelEventArgs クラス

定義

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

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

次の例では、 オブジェクトを ListViewCancelEventArgs 使用して、ユーザーがコントロールの挿入または更新操作をキャンセルしたときにメッセージを表示する方法を ListView 示します。 オブジェクトは ListViewCancelEventArgs 、イベントのイベント処理メソッドに ItemCanceling 渡されます。

重要

この例には、潜在的なセキュリティ上の脅威であるユーザー入力を受け入れるテキスト ボックスが含まれています。 既定では、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>
  protected void ContactsListView_ItemCanceling(object sender, ListViewCancelEventArgs e)
  {
    //Check the operation that raised the event
    if (e.CancelMode == ListViewCancelMode.CancelingEdit)
    {
      // The update operation was canceled. Display the 
      // primary key of the item.
      Message.Text = "Update for the ContactID " + 
        ContactsListView.DataKeys[e.ItemIndex].Value.ToString()  + " canceled.";
    }
    else
    {
      Message.Text = "Insert operation canceled."; 
    }
  }
  // </Snippet2>

  protected void ContactsListView_PagePropertiesChanging(object sender, 
    PagePropertiesChangingEventArgs e)
  {
    // Clears the edit index selection when paging.
    ContactsListView.EditIndex = -1;
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView ItemCanceling Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListView ItemCanceling Example</h3>
      
      <asp:Label ID="Message"
        ForeColor="Red"          
        runat="server"/>
      <br/>
                 
      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        ConvertEmptyStringToNull="true"
        InsertItemPosition="LastItem"
        runat="server" 
        OnItemCanceling="ContactsListView_ItemCanceling" 
        OnPagePropertiesChanging="ContactsListView_PagePropertiesChanging" >
        <LayoutTemplate>
          <table cellpadding="2" width="680px" border="1" runat="server" id="tblContacts">
            <tr runat="server">
              <th runat="server">&nbsp;</th>
              <th runat="server">ID</th>
              <th runat="server">First Name</th>
              <th runat="server">Last Name</th>
              <th runat="server">Email Address</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="ContactsDataPager" 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:LinkButton ID="EditButton" runat="server" Text="Edit" CommandName="Edit" /><br />
            </td>
            <td valign="top">
              <asp:Label ID="Label1" runat="server" Text='<%#Eval("ContactID") %>' />
            </td>
            <td valign="top">
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
            </td>
            <td valign="top">
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
            </td>
            <td valign="top">
              <asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
              &nbsp;
            </td>
          </tr>
        </ItemTemplate>
        <EditItemTemplate>
          <tr style="background-color: #ADD8E6">
            <td valign="top">
              <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" /><br />
              <asp:LinkButton ID="CancelEditButton" runat="server" CommandName="Cancel" Text="Cancel" />
            </td>
            <td>
              <asp:Label ID="Label1" runat="server" Text='<%#Eval("ContactID") %>' />
            </td>
            <td>
              <asp:TextBox ID="FirstNameTextBox" runat="server" 
                Text='<%#Bind("FirstName") %>' MaxLength="50" /><br />
            </td>
            <td>
              <asp:TextBox ID="LastNameTextBox" runat="server" 
                Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
            </td>
            <td>
              <asp:TextBox ID="EmailAddressTextBox" runat="server" 
                Text='<%#Bind("EmailAddress") %>' MaxLength="50" /><br />
            </td>
          </tr>
        </EditItemTemplate>
        <InsertItemTemplate>
          <tr style="background-color:#90EE90">
            <td colspan="2">
              <asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" /><br />
              <asp:LinkButton ID="CancelInsertButton" runat="server" CommandName="Cancel" Text="Cancel" />
            </td>              
            <td>
              <asp:TextBox ID="FirstNameITextBox" runat="server" 
                Text='<%#Bind("FirstName") %>' MaxLength="50" />
            </td>
            <td>
              <asp:TextBox ID="LastNameITextBox" runat="server" 
                Text='<%#Bind("LastName") %>' MaxLength="50" />
            </td>
            <td>
              <asp:TextBox ID="EmailAddressITextBox" runat="server" 
                Text='<%#Bind("EmailAddress") %>' MaxLength="50" />
           </td>
          </tr>
        </InsertItemTemplate>
      </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"
        InsertCommand="INSERT INTO Person.Contact
                         ([FirstName], [LastName], [EmailAddress], [PasswordHash], [PasswordSalt]) 
                         Values(@FirstName, @LastName, @EmailAddress, '', '')"
        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>
  Protected Sub ContactsListView_ItemCanceling(ByVal sender As Object, _
                                               ByVal e As ListViewCancelEventArgs)
    'Check the operation that raised the event
    If (e.CancelMode = ListViewCancelMode.CancelingEdit) Then
      ' The update operation was canceled. Display the 
      ' primary key of the item.
      Message.Text = "Update for the ContactID " & _
        ContactsListView.DataKeys(e.ItemIndex).Value.ToString() & " canceled."
    Else
      Message.Text = "Insert operation canceled."
    End If

  End Sub
  ' </Snippet2>
  
  Protected Sub ContactsListView_PagePropertiesChanging(ByVal sender As Object, _
                                               ByVal e As PagePropertiesChangingEventArgs)
    ' Clears the edit index selection when paging.
    ContactsListView.EditIndex = -1
  End Sub
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView ItemCanceling Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListView ItemCanceling Example</h3>
      
      <asp:Label ID="Message"
        ForeColor="Red"          
        runat="server"/>
      <br/>
                 
      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        ConvertEmptyStringToNull="true"
        InsertItemPosition="LastItem"
        runat="server" 
        OnItemCanceling="ContactsListView_ItemCanceling" 
        OnPagePropertiesChanging="ContactsListView_PagePropertiesChanging" >
        <LayoutTemplate>
          <table cellpadding="2" width="680px" border="1" runat="server" id="tblContacts">
            <tr runat="server">
              <th runat="server">&nbsp;</th>
              <th runat="server">ID</th>
              <th runat="server">First Name</th>
              <th runat="server">Last Name</th>
              <th runat="server">Email Address</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="ContactsDataPager" 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:LinkButton ID="EditButton" runat="server" Text="Edit" CommandName="Edit" /><br />
            </td>
            <td valign="top">
              <asp:Label ID="Label1" runat="server" Text='<%#Eval("ContactID") %>' />
            </td>
            <td valign="top">
              <asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName") %>' />
            </td>
            <td valign="top">
              <asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName") %>' />
            </td>
            <td valign="top">
              <asp:Label ID="EmailAddressLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
              &nbsp;
            </td>
          </tr>
        </ItemTemplate>
        <EditItemTemplate>
          <tr style="background-color: #ADD8E6">
            <td valign="top">
              <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" /><br />
              <asp:LinkButton ID="CancelEditButton" runat="server" CommandName="Cancel" Text="Cancel" />
            </td>
            <td>
              <asp:Label ID="Label1" runat="server" Text='<%#Eval("ContactID") %>' />
            </td>
            <td>
              <asp:TextBox ID="FirstNameTextBox" runat="server" 
                Text='<%#Bind("FirstName") %>' MaxLength="50" /><br />
            </td>
            <td>
              <asp:TextBox ID="LastNameTextBox" runat="server" 
                Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
            </td>
            <td>
              <asp:TextBox ID="EmailAddressTextBox" runat="server" 
                Text='<%#Bind("EmailAddress") %>' MaxLength="50" /><br />
            </td>
          </tr>
        </EditItemTemplate>
        <InsertItemTemplate>
          <tr style="background-color:#90EE90">
            <td colspan="2">
              <asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" /><br />
              <asp:LinkButton ID="CancelInsertButton" runat="server" CommandName="Cancel" Text="Cancel" />
            </td>              
            <td>
              <asp:TextBox ID="FirstNameITextBox" runat="server" 
                Text='<%#Bind("FirstName") %>' MaxLength="50" />
            </td>
            <td>
              <asp:TextBox ID="LastNameITextBox" runat="server" 
                Text='<%#Bind("LastName") %>' MaxLength="50" />
            </td>
            <td>
              <asp:TextBox ID="EmailAddressITextBox" runat="server" 
                Text='<%#Bind("EmailAddress") %>' MaxLength="50" />
           </td>
          </tr>
        </InsertItemTemplate>
      </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"
        InsertCommand="INSERT INTO Person.Contact
                         ([FirstName], [LastName], [EmailAddress], [PasswordHash], [PasswordSalt]) 
                         Values(@FirstName, @LastName, @EmailAddress, '', '')"
        UpdateCommand="UPDATE Person.Contact
                         SET FirstName = @FirstName, LastName = @LastName,
                         EmailAddress = @EmailAddress
                         WHERE ContactID = @ContactID">
      </asp:SqlDataSource>
      
    </form>
  </body>
</html>

注釈

コントロールは ListView 、[キャンセル] ボタンがクリックされたときに、挿入または編集モードを終了する前にイベントを発生 ItemCanceling させます。 ([キャンセル] ボタンは、プロパティが "Cancel" に設定されているボタン CommandName です)これにより、このイベントが発生するたびにカスタム ルーチンを実行するイベント処理メソッドを提供できます。たとえば、アイテムが不要な状態になった場合にキャンセル操作を停止するなどです。

ListViewCancelEventArgsオブジェクトは、イベント処理メソッドに渡されます。 このオブジェクトを使用すると、イベントを発生させた [キャンセル] ボタンを含むアイテムのインデックスを決定できます。 取り消された操作を確認することもできます。 取り消し操作を停止するには、 プロパティを Canceltrue設定します。

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

コンストラクター

ListViewCancelEventArgs(Int32, ListViewCancelMode)

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

プロパティ

Cancel

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

(継承元 CancelEventArgs)
CancelMode

Cancel ボタンがクリックされたときの ListView コントロールのデータ入力モードを取得します。

ItemIndex

イベントを発生させた Cancel ボタンが格納されている項目のインデックスを取得します。

メソッド

Equals(Object)

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

(継承元 Object)
GetHashCode()

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

(継承元 Object)
GetType()

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

(継承元 Object)
MemberwiseClone()

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

(継承元 Object)
ToString()

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

(継承元 Object)

適用対象

こちらもご覧ください