Share via


FormView.InsertItem(Boolean) 메서드

정의

데이터 소스에 현재 레코드를 삽입합니다.

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

매개 변수

causesValidation
Boolean

메서드가 호출될 때 페이지 유효성 검사를 수행하려면 true이고, 그렇지 않으면 false입니다.

예외

FormView 컨트롤이 삽입 모드에 있지 않은 상태에서 이 메서드를 호출한 경우

또는

DataSourceView 컨트롤과 연결된 FormView 개체가 null인 경우

예제

다음 예제에서는 메서드를 사용 하 여 InsertItem 프로그래밍 방식으로 현재 레코드를 삽입 하는 FormView 방법을 보여 줍니다는 데이터 원본의 컨트롤입니다.


<%@ 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 InsertButton_Click(Object sender, EventArgs e)
  {

    try
    {
      // Use the InsertItem method to programmatically insert
      // the current record in the FormView control. 
      EmployeeFormView.InsertItem(true);
      MessageLabel.Text = "";
    }
    catch (HttpException ex)
    {
      MessageLabel.Text = "The FormView control must be in insert mode to insert a record.";
    }

  }

  void CancelButton_Click(Object sender, EventArgs e)
  {
    
    // Return the FormView control to read-only
    // mode.
    EmployeeFormView.ChangeMode(FormViewMode.ReadOnly);
    MessageLabel.Text = "";

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>FormView InsertItem Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>FormView InsertItem Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID"
        emptydatatext="No employees found."
        runat="server">

        <itemtemplate>
          <table>
            <tr>
              <td rowspan="5">
                <asp:image id="CompanyLogoImage"
                  imageurl="~/Images/Logo.jpg"
                  alternatetext="Company logo"
                  runat="server"/>
              </td>
              <td colspan="2">
                    
              </td>
            </tr>
            <tr>
              <td>
                <b>Name:</b>
              </td>
              <td>
                <%# Eval("FirstName") %> <%# Eval("LastName") %>
              </td>
            </tr>
            <tr>
              <td>
                <b>Title:</b>
              </td>
              <td>
                <%# Eval("Title") %>
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:linkbutton id="NewButton"
                  text="New"
                  commandname="New"
                  runat="server"/> 
              </td>
            </tr>
          </table>       
        </itemtemplate>
        <insertitemtemplate>
          <table>
            <tr>
              <td rowspan="4">
                <asp:image id="CompanyLogoEditImage"
                  imageurl="~/Images/Logo.jpg"
                  alternatetext="Company logo"
                  runat="server"/>
              </td>
              <td colspan="2">
                    
              </td>
            </tr>
            <tr>
              <td>
                <b><asp:Label runat="server" 
                  AssociatedControlID="FirstNameInsertTextBox" 
                  Text="Name" />:</b>
              </td>
              <td>
                <asp:textbox id="FirstNameInsertTextBox"
                  text='<%# Bind("FirstName") %>'
                  runat="server"/>
                <asp:textbox id="LastNameInsertTextBox"
                  text='<%# Bind("LastName") %>'
                  runat="server"/>
              </td>
            </tr>
            <tr>
              <td>
                <b><asp:Label runat="server" 
                  AssociatedControlID="TitleInsertTextBox" 
                  Text="Title" />:</b>
              </td>
              <td>
                <asp:textbox id="TitleInsertTextBox"
                  text='<%# Bind("Title") %>'
                  runat="server"/> 
              </td>
            </tr>
          </table>       
        </insertitemtemplate> 
                  
      </asp:formview>
      
      <hr/>
      
      <asp:Button id="InsertButton"
        text="Insert Record"
        onclick="InsertButton_Click" 
        runat="server"/>
        
      <asp:Button id="CancelButton"
        text="Cancel"
        onclick="CancelButton_Click" 
        runat="server"/>
        
      <br/><br/>
      
      <asp:label id="MessageLabel"
        forecolor="Red"
        runat="server"/>
     
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="EmployeeSource"
        selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
        insertcommand="Insert Into [Employees] ([LastName], [FirstName], [Title]) VALUES (@LastName, @FirstName, @Title)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </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 InsertButton_Click(ByVal sender As Object, ByVal e As EventArgs)

    Try
    
      ' Use the InsertItem method to programmatically insert
      ' the current record in the FormView control. 
      EmployeeFormView.InsertItem(True)
      MessageLabel.Text = ""
    
    Catch ex As HttpException
    
      MessageLabel.Text = "The FormView control must be in insert mode to insert a record."
    
    End Try

  End Sub

  Sub CancelButton_Click(ByVal sender As Object, ByVal e As EventArgs)
    
    ' Return the FormView control to read-only
    ' mode.
    EmployeeFormView.ChangeMode(FormViewMode.ReadOnly)
    MessageLabel.Text = ""

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>FormView InsertItem Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>FormView InsertItem Example</h3>
                       
      <asp:formview id="EmployeeFormView"
        datasourceid="EmployeeSource"
        allowpaging="true"
        datakeynames="EmployeeID"
        emptydatatext="No employees found."
        runat="server">

        <itemtemplate>
          <table>
            <tr>
              <td rowspan="5">
                <asp:image id="CompanyLogoImage"
                  imageurl="~/Images/Logo.jpg"
                  alternatetext="Company logo"
                  runat="server"/>
              </td>
              <td colspan="2">
                    
              </td>
            </tr>
            <tr>
              <td>
                <b>Name:</b>
              </td>
              <td>
                <%# Eval("FirstName") %> <%# Eval("LastName") %>
              </td>
            </tr>
            <tr>
              <td>
                <b>Title:</b>
              </td>
              <td>
                <%# Eval("Title") %>
              </td>
            </tr>
            <tr>
              <td colspan="2">
                <asp:linkbutton id="NewButton"
                  text="New"
                  commandname="New"
                  runat="server"/> 
              </td>
            </tr>
          </table>       
        </itemtemplate>
        <insertitemtemplate>
          <table>
            <tr>
              <td rowspan="4">
                <asp:image id="CompanyLogoEditImage"
                  imageurl="~/Images/Logo.jpg"
                  alternatetext="Company logo"
                  runat="server"/>
              </td>
              <td colspan="2">
                    
              </td>
            </tr>
            <tr>
              <td>
                <b><asp:Label runat="server" 
                  AssociatedControlID="FirstNameInsertTextBox" 
                  Text="Name" />:</b>
              </td>
              <td>
                <asp:textbox id="FirstNameInsertTextBox"
                  text='<%# Bind("FirstName") %>'
                  runat="server"/>
                <asp:textbox id="LastNameInsertTextBox"
                  text='<%# Bind("LastName") %>'
                  runat="server"/>
              </td>
            </tr>
            <tr>
              <td>
                <b><asp:Label runat="server" 
                  AssociatedControlID="TitleInsertTextBox" 
                  Text="Title" />:</b>
              </td>
              <td>
                <asp:textbox id="TitleInsertTextBox"
                  text='<%# Bind("Title") %>'
                  runat="server"/> 
              </td>
            </tr>
          </table>       
        </insertitemtemplate> 
                  
      </asp:formview>
      
      <hr/>
      
      <asp:Button id="InsertButton"
        text="Insert Record"
        onclick="InsertButton_Click" 
        runat="server"/>
        
      <asp:Button id="CancelButton"
        text="Cancel"
        onclick="CancelButton_Click" 
        runat="server"/>
        
      <br/><br/>
      
      <asp:label id="MessageLabel"
        forecolor="Red"
        runat="server"/>
     
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="EmployeeSource"
        selectcommand="Select [EmployeeID], [LastName], [FirstName], [Title], [PhotoPath] From [Employees]"
        insertcommand="Insert Into [Employees] ([LastName], [FirstName], [Title]) VALUES (@LastName, @FirstName, @Title)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>
            
    </form>
  </body>
</html>

설명

컨트롤이 FormView 삽입 모드인 경우 메서드를 InsertItem 사용하여 데이터 원본에 현재 레코드를 프로그래밍 방식으로 삽입합니다. 이 메서드는 일반적으로 페이지의 다른 컨트롤과 같이 컨트롤 외부에서 FormView 현재 레코드를 삽입해야 하는 경우에 사용됩니다.

참고

FormView 메서드가 호출될 때 컨트롤이 삽입 모드여야 합니다. 그렇지 않으면 가 HttpException throw됩니다.

삽입 작업 전에 페이지 유효성 검사를 수행할지 여부를 지정하려면 매개 변수를 causesValidation 사용합니다. 이 메서드를 호출하면 및 ItemInserting 이벤트도 발생합니다ItemInserted.

적용 대상

추가 정보