ListView.UpdateItem(Int32, Boolean) 메서드

정의

데이터 소스에서 지정된 인덱스 위치에 있는 레코드를 업데이트합니다.

public:
 virtual void UpdateItem(int itemIndex, bool causesValidation);
public virtual void UpdateItem (int itemIndex, bool causesValidation);
abstract member UpdateItem : int * bool -> unit
override this.UpdateItem : int * bool -> unit
Public Overridable Sub UpdateItem (itemIndex As Integer, causesValidation As Boolean)

매개 변수

itemIndex
Int32

업데이트할 항목의 인덱스입니다.

causesValidation
Boolean

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

예외

itemIndex 가 0보다 작습니다.

또는

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

예제

다음 예제에서는 메서드를 사용하여 UpdateItem 데이터 원본의 항목을 프로그래밍 방식으로 업데이트하는 ListView 방법을 보여 줍니다.

<%@ 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">

  // <Snippet2>
  protected void PreferredCheckBox_CheckedChanged(object sender, EventArgs e)
  {
    // Gets the CheckBox object that fired the event.
    CheckBox chkBox = (CheckBox) sender;

    // Gets the item that contains the CheckBox object. 
    ListViewDataItem item = (ListViewDataItem) chkBox.Parent.Parent;
    
    // Update the database with the changes.
    VendorsListView.UpdateItem(item.DisplayIndex, false);
  }
  // </Snippet2>
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView UpdateItem Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListView UpdateItem Example</h3>
      
      <asp:ListView ID="VendorsListView" 
        DataSourceID="VendorsDataSource"
        DataKeyNames="VendorID"
        runat="server">
        <LayoutTemplate>
          Select your preferred vendors:
          <ul runat="server" id="lstVendors" style="text-align:left">
            <li runat="server" id="itemPlaceholder" />
          </ul>
          <asp:DataPager ID="VendorsDataPager" runat="server" PageSize="15">
            <Fields>
              <asp:NumericPagerField ButtonCount="10" />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <li runat="server">
            <asp:CheckBox runat="server" ID="PreferredCheckBox" AutoPostBack="True" 
              Checked='<%# Bind("PreferredVendorStatus") %>' 				        
              OnCheckedChanged="PreferredCheckBox_CheckedChanged" />
            <asp:Label runat="server" ID="NameLabel" text='<%# Eval("Name") %>' /><br/>
          </li>
        </ItemTemplate>
      </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="VendorsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT VendorID, Name, PreferredVendorStatus
          FROM Purchasing.Vendor WHERE (ActiveFlag = 1)" 
        UpdateCommand="UPDATE Purchasing.Vendor 
          SET PreferredVendorStatus = @PreferredVendorStatus 
          WHERE (VendorID = @VendorID)" >
      </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">

   ' <Snippet2>
  Protected Sub PreferredCheckBox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) 
  
    ' Gets the CheckBox object that fired the event.
    Dim chkBox As CheckBox = CType(sender, CheckBox)
    
    ' Gets the item that contains the CheckBox object. 
    Dim item As ListViewDataItem = CType(chkBox.Parent.Parent, ListViewDataItem)
    
    ' Update the database with the changes.
    VendorsListView.UpdateItem(item.DisplayIndex, False)    

  End Sub
  ' </Snippet2>
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView UpdateItem Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListView UpdateItem Example</h3>
      
      <asp:ListView ID="VendorsListView" 
        DataSourceID="VendorsDataSource"
        DataKeyNames="VendorID"
        runat="server">
        <LayoutTemplate>
          Select your preferred vendors:
          <ul runat="server" id="lstVendors" style="text-align:left">
            <li runat="server" id="itemPlaceholder" />
          </ul>
          <asp:DataPager ID="VendorsDataPager" runat="server" PageSize="15">
            <Fields>
              <asp:NumericPagerField ButtonCount="10" />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <li runat="server">
            <asp:CheckBox runat="server" ID="PreferredCheckBox" AutoPostBack="True" 
              Checked='<%# Bind("PreferredVendorStatus") %>' 				        
              OnCheckedChanged="PreferredCheckBox_CheckedChanged" />
            <asp:Label runat="server" ID="NameLabel" text='<%# Eval("Name") %>' /><br/>
          </li>
        </ItemTemplate>
      </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="VendorsDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
        SelectCommand="SELECT VendorID, Name, PreferredVendorStatus
          FROM Purchasing.Vendor WHERE (ActiveFlag = 1)" 
        UpdateCommand="UPDATE Purchasing.Vendor 
          SET PreferredVendorStatus = @PreferredVendorStatus 
          WHERE (VendorID = @VendorID)" >
      </asp:SqlDataSource>

    </form>
  </body>
</html>

설명

메서드를 UpdateItem 사용하여 데이터 원본의 지정된 인덱스에서 레코드를 프로그래밍 방식으로 업데이트합니다. 일반적으로 페이지의 다른 컨트롤과 같이 컨트롤 외부에서 ListView 레코드를 업데이트하려는 경우 이 메서드를 사용합니다.

참고

이 메서드는 양방향 데이터 바인딩된 입력 컨트롤을 포함하는 항목에 대해서만 호출할 수 있습니다. 양방향 바인딩 식에 대한 자세한 내용은 데이터베이스에 바인딩을 참조하세요.

업데이트 작업 전에 페이지 유효성 검사를 수행할지 여부를 지정하려면 매개 변수를 causesValidation 사용합니다.

이 메서드는 및 ItemUpdating 이벤트를 발생합니다ItemUpdated.

적용 대상

추가 정보