ListView.InsertNewItem(Boolean) メソッド

定義

現在のレコードをデータ ソースに挿入します。

public:
 virtual void InsertNewItem(bool causesValidation);
public virtual void InsertNewItem (bool causesValidation);
abstract member InsertNewItem : bool -> unit
override this.InsertNewItem : bool -> unit
Public Overridable Sub InsertNewItem (causesValidation As Boolean)

パラメーター

causesValidation
Boolean

このメソッドが呼び出されたときにページの検証を実行する場合は true。それ以外の場合は false

例外

ListView コントロールには挿入項目がありません。

- または -

DataSourceView コントロールに関連付けられている ListView オブジェクトが null です。

次の例では、 メソッドを InsertNewItem 使用して、コントロールの挿入項目 ListView の内容をプログラムによってデータ ソースに挿入する方法を示します。

重要

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

  protected void InsertButton_Click(object sender, EventArgs e)
  {
    // Clears any existing message.
    MessageLabel.Text = "";
    
    // Use the InsertNewItem method to programmatically insert
    // the current record in the ListView control.
    DepartmentsListView.InsertNewItem(true);
  }

  protected void DepartmentsListView_ItemInserted(object sender, 
    ListViewInsertedEventArgs e)
  {
    // Handles exceptions that might occur
    // during the insert operation.
    if (e.Exception != null)
    {
      if (e.AffectedRows == 0)
      {
        e.KeepInInsertMode = true;
        MessageLabel.Text = "An exception occurred inserting the new department. " +
                            "Please verify your values and try again.";
      }
      else
        MessageLabel.Text = "An exception occurred inserting the new department. " +
                            "Please verify the values in the newly inserted item.";

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

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView InsertNewItem Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListView InsertNewItem Example</h3>
      
      <h5>Departments</h5>

      <asp:ListView ID="DepartmentsListView" 
        DataSourceID="DepartmentsDataSource" 
        DataKeyNames="DepartmentID"
        InsertItemPosition="FirstItem"
        OnItemInserted="DepartmentsListView_ItemInserted"
        runat="server" >
        <LayoutTemplate>
          <table runat="server" id="tblDepartments" width="640px" border="1">
            <tr runat="server" id="itemPlaceholder" />
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:Label ID="NameLabel" runat="Server" Text='<%#Eval("Name") %>' />
            </td>
            <td>
              <asp:Label ID="GroupNameLabel" runat="Server" Text='<%#Eval("GroupName") %>' />
            </td>
          </tr>
        </ItemTemplate>
        <InsertItemTemplate>
          <tr style="background-color:#00BFFF">
            <td>
              <asp:Label runat="server" ID="NameLabel" AssociatedControlID="NameTextBox" 
                Text="Name:" Font-Bold="true"/><br />
              <asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' />
            </td>
            <td>
              <asp:Label runat="server" ID="GroupNameLabel" AssociatedControlID="GroupNameTextBox" 
                Text="Group Name:" Font-Bold="true" /><br />
              <asp:TextBox ID="GroupNameTextBox" runat="server" 
                Text='<%#Bind("GroupName") %>' MaxLength="50" />
            </td>
          </tr>
        </InsertItemTemplate>
      </asp:ListView><br />
              
      <asp:Label ID="MessageLabel"
        ForeColor="Red"
        runat="server" /> <br />

      <asp:Button ID="InsertButton"
        Text="Insert new record"
        OnClick="InsertButton_Click"
        runat="server"  />
    
      <!-- 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="DepartmentsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT DepartmentID, Name, GroupName FROM HumanResources.Department"
        InsertCommand="INSERT INTO HumanResources.Department(Name, GroupName) 
                VALUES (@Name, @GroupName)">
      </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">

  Protected Sub InsertButton_Click(ByVal sender As Object, _
                                   ByVal e As EventArgs)
    
    ' Clears any existing message.
    MessageLabel.Text = ""
    
    ' Use the InsertNewItem method to programmatically insert
    ' the current record in the ListView control.
    DepartmentsListView.InsertNewItem(True)
    
  End Sub

  Protected Sub DepartmentsListView_ItemInserted(ByVal sender As Object, _
                                                 ByVal e As ListViewInsertedEventArgs)
  
    ' Handles exceptions that might occur
    ' during the insert operation.
    If Not (e.Exception Is Nothing) Then
      If e.AffectedRows = 0 Then
        e.KeepInInsertMode = True
        MessageLabel.Text = "An exception occurred inserting the new department. " & _
              "Please verify your values and try again."
      Else
        MessageLabel.Text = "An exception occurred inserting the new department. " & _
              "Please verify the values in the newly inserted item."
      End If
        
      e.ExceptionHandled = True
    End If

  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView InsertNewItem Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListView InsertNewItem Example</h3>
      
      <h5>Departments</h5>

      <asp:ListView ID="DepartmentsListView" 
        DataSourceID="DepartmentsDataSource" 
        DataKeyNames="DepartmentID"
        InsertItemPosition="FirstItem"
        OnItemInserted="DepartmentsListView_ItemInserted"
        runat="server" >
        <LayoutTemplate>
          <table runat="server" id="tblDepartments" width="640px" border="1">
            <tr runat="server" id="itemPlaceholder" />
          </table>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:Label ID="NameLabel" runat="Server" Text='<%#Eval("Name") %>' />
            </td>
            <td>
              <asp:Label ID="GroupNameLabel" runat="Server" Text='<%#Eval("GroupName") %>' />
            </td>
          </tr>
        </ItemTemplate>
        <InsertItemTemplate>
          <tr style="background-color:#00BFFF">
            <td>
              <asp:Label runat="server" ID="NameLabel" AssociatedControlID="NameTextBox" 
                Text="Name:" Font-Bold="true"/><br />
              <asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' />
            </td>
            <td>
              <asp:Label runat="server" ID="GroupNameLabel" AssociatedControlID="GroupNameTextBox" 
                Text="Group Name:" Font-Bold="true" /><br />
              <asp:TextBox ID="GroupNameTextBox" runat="server" 
                Text='<%#Bind("GroupName") %>' MaxLength="50" />
            </td>
          </tr>
        </InsertItemTemplate>
      </asp:ListView><br />
              
      <asp:Label ID="MessageLabel"
        ForeColor="Red"
        runat="server" /> <br />

      <asp:Button ID="InsertButton"
        Text="Insert new record"
        OnClick="InsertButton_Click"
        runat="server"  />
    
      <!-- 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="DepartmentsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT DepartmentID, Name, GroupName FROM HumanResources.Department"
        InsertCommand="INSERT INTO HumanResources.Department(Name, GroupName) 
                VALUES (@Name, @GroupName)">
      </asp:SqlDataSource>
      
    </form>
  </body>
</html>

注釈

プログラムによって InsertNewItem データ ソースに項目を挿入するには、 メソッドを使用します。 このメソッドは、通常、ページ上の別のコントロールなど、コントロールの ListView 外部から項目を挿入するために使用されます。

メソッドを InsertNewItem 使用するには、コントロールでテンプレートを InsertItemTemplate 定義する ListView 必要があります。 また、 プロパティを InsertItemPosition とは InsertItemPosition.None異なる値に設定する必要があります。 挿入操作の前にページ検証を実行するかどうかを指定するには、 パラメーターを causesValidation 使用します。

このメソッドはItemInsertedItemInsertingイベント。

適用対象

こちらもご覧ください