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 웹 페이지는 사용자 입력 내용에 스크립트 또는 HTML 요소가 포함되어 있지 않은지 확인합니다. 자세한 내용은 Script Exploits Overview를 참조하세요.

<%@ 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 사용하려면 컨트롤에서 ListView 템플릿을 InsertItemTemplate 정의해야 합니다. 또한 속성을 값과 InsertItemPosition 다른 InsertItemPosition.None값으로 설정해야 합니다. 삽입 작업 전에 페이지 유효성 검사를 수행할지 여부를 지정하려면 매개 변수를 causesValidation 사용합니다.

이 메서드는 및 ItemInserting 이벤트를 발생합니다ItemInserted.

적용 대상

추가 정보