ListViewInsertedEventArgs クラス

定義

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

public ref class ListViewInsertedEventArgs : EventArgs
public class ListViewInsertedEventArgs : EventArgs
type ListViewInsertedEventArgs = class
    inherit EventArgs
Public Class ListViewInsertedEventArgs
Inherits EventArgs
継承
ListViewInsertedEventArgs

次の例は、 イベントの ListViewInsertedEventArgs ハンドラーに渡される オブジェクトを使用する方法を ItemInserted 示しています。

重要

この例には、ユーザー入力を受け付けるテキスト ボックスがあります。これにより、セキュリティが脆弱になる可能性があります。 既定では、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">

  //<Snippet3>
  void ContactsListView_ItemInserted(Object sender, ListViewInsertedEventArgs e)
  {
    if (e.Exception != null)
    {
      if (e.AffectedRows == 0)
      {
        e.KeepInInsertMode = true;
        Message.Text = "An exception occurred inserting the new Contact. " +
          "Please verify your values and try again.";
      }
      else
        Message.Text = "An exception occurred inserting the new Contact. " +
          "Please verify the values in the newly inserted item.";

      e.ExceptionHandled = true;
    }
  }
  //</Snippet3>

  protected void Page_Load(object sender, EventArgs e)
  {
    Message.Text = "";
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView.ItemInserted Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListViewItemInserted Example</h3>
            
      <asp:Label ID="Message"
        ForeColor="Red"          
        runat="server"/>
      <br/>
      
      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        OnItemInserted="ContactsListView_ItemInserted"  
        InsertItemPosition="LastItem"
        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>&nbsp;
              <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
          </tr>
        </ItemTemplate>
        <InsertItemTemplate>
          <tr style="background-color:#D3D3D3">
            <td valign="top">
              <asp:Label runat="server" ID="FirstNameLabel" 
                AssociatedControlID="FirstNameTextBox" Text="First Name"/>
              <asp:TextBox ID="FirstNameTextBox" runat="server" 
                Text='<%#Bind("FirstName") %>' /><br />
              <asp:Label runat="server" ID="LastNameLabel" 
                AssociatedControlID="LastNameTextBox" Text="Last Name" />
              <asp:TextBox ID="LastNameTextBox" runat="server" 
                Text='<%#Bind("LastName") %>' /><br />
              <asp:Label runat="server" ID="EmailLabel" 
                AssociatedControlID="EmailTextBox" Text="Email" />
              <asp:TextBox ID="EmailTextBox" runat="server" 
                Text='<%#Bind("EmailAddress") %>' />
            </td>
            <td>
              <asp:LinkButton ID="InsertButton" runat="server" 
                CommandName="Insert" Text="Insert" />
            </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, '', '');
          SELECT @ContactID = SCOPE_IDENTITY()">
        <InsertParameters>
          <asp:Parameter Name="FirstName" />
          <asp:Parameter Name="LastName" />
          <asp:Parameter Name="EmailAddress" />
          <asp:Parameter Name="ContactID" Type="Int32" Direction="Output" />
        </InsertParameters>
      </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">

  '<Snippet3>
  Sub ContactsListView_ItemInserted(ByVal sender As Object, ByVal e As ListViewInsertedEventArgs)

    If e.Exception IsNot Nothing Then

      If e.AffectedRows = 0 Then
        e.KeepInInsertMode = True
        Message.Text = "An exception occurred inserting the new Contact. " & _
          "Please verify your values and try again."
      Else
        Message.Text = "An exception occurred inserting the new Contact. " & _
          "Please verify the values in the newly inserted item."
      End If

      e.ExceptionHandled = True
    End If
  End Sub
  '</Snippet3>

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Message.Text = ""
  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView.ItemInserted Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListViewItemInserted Example</h3>
            
      <asp:Label ID="Message"
        ForeColor="Red"          
        runat="server"/>
      <br/>
      
      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        OnItemInserted="ContactsListView_ItemInserted"  
        InsertItemPosition="LastItem"
        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>&nbsp;
              <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
          </tr>
        </ItemTemplate>
        <InsertItemTemplate>
          <tr style="background-color:#D3D3D3">
            <td valign="top">
              <asp:Label runat="server" ID="FirstNameLabel" 
                AssociatedControlID="FirstNameTextBox" Text="First Name"/>
              <asp:TextBox ID="FirstNameTextBox" runat="server" 
                Text='<%#Bind("FirstName") %>' /><br />
              <asp:Label runat="server" ID="LastNameLabel" 
                AssociatedControlID="LastNameTextBox" Text="Last Name" />
              <asp:TextBox ID="LastNameTextBox" runat="server" 
                Text='<%#Bind("LastName") %>' /><br />
              <asp:Label runat="server" ID="EmailLabel" 
                AssociatedControlID="EmailTextBox" Text="Email" />
              <asp:TextBox ID="EmailTextBox" runat="server" 
                Text='<%#Bind("EmailAddress") %>' />
            </td>
            <td>
              <asp:LinkButton ID="InsertButton" runat="server" 
                CommandName="Insert" Text="Insert" />
            </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, '', '');
          SELECT @ContactID = SCOPE_IDENTITY()">
        <InsertParameters>
          <asp:Parameter Name="FirstName" />
          <asp:Parameter Name="LastName" />
          <asp:Parameter Name="EmailAddress" />
          <asp:Parameter Name="ContactID" Type="Int32" Direction="Output" />
        </InsertParameters>
      </asp:SqlDataSource>
      
    </form>
  </body>
</html>

注釈

コントロールはListView、テンプレートの [挿入] ボタンがクリックされたときにイベントをInsertItemTemplate発生ItemInsertedさせ、コントロールがListViewデータ ソースのレコードを更新した後に発生します。 ([挿入] ボタンは、プロパティが CommandName "Insert" に設定されているボタンです)。イベント ItemInserted を使用すると、挿入された項目のデータベースから自動生成された値を取得するなど、カスタム アクションを実行できます。

ListViewInsertedEventArgsオブジェクトはイベント処理メソッドに渡されます。これにより、挿入された項目の数と発生した可能性のある例外を特定できます。 挿入操作の影響を受ける項目の数を確認するには、 プロパティを AffectedRows 使用します。 例外が Exception 発生したかどうかを判断するには、 プロパティを使用します。 また、 プロパティを設定 ExceptionHandled することで、イベント処理メソッドで例外が処理されたかどうかを示すこともできます。 データ ソースに送信された挿入された項目のフィールド値にアクセスする必要がある場合は、 プロパティを Values 使用します。

既定では、コントロールは ListView 挿入操作の後にテンプレートをクリア InsertItemTemplate します。これにより、ユーザーは新しい項目の値を追加して挿入できます。 挿入操作中に例外が発生した場合は、 プロパティを ListViewtrue設定することで、コントロールを挿入モードのままにKeepInInsertModeすることができます。 これにより、項目を InsertItemTemplate 挿入する前の試行の値にテンプレートが再バインドされます。

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

コンストラクター

ListViewInsertedEventArgs(Int32, Exception)

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

プロパティ

AffectedRows

挿入操作の影響を受けた行の数を取得します。

Exception

挿入操作中に例外が発生した場合は、その例外を取得します。

ExceptionHandled

挿入操作中に発生した例外がイベント ハンドラーで処理されたかどうかを示す値を取得または設定します。

KeepInInsertMode

InsertItemTemplate テンプレート内のコントロールに対してユーザーの入力値が保持されているかどうかを示す値を取得または設定します。

Values

挿入されたレコードのフィールドの名前と値のペアを取得します。

メソッド

Equals(Object)

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

(継承元 Object)
GetHashCode()

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

(継承元 Object)
GetType()

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

(継承元 Object)
MemberwiseClone()

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

(継承元 Object)
ToString()

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

(継承元 Object)

適用対象

こちらもご覧ください