ListView.ItemUpdating 이벤트

정의

업데이트 작업이 요청될 때 ListView 컨트롤에서 항목을 업데이트하기 전에 발생합니다.

public:
 event EventHandler<System::Web::UI::WebControls::ListViewUpdateEventArgs ^> ^ ItemUpdating;
public event EventHandler<System.Web.UI.WebControls.ListViewUpdateEventArgs> ItemUpdating;
member this.ItemUpdating : EventHandler<System.Web.UI.WebControls.ListViewUpdateEventArgs> 
Public Custom Event ItemUpdating As EventHandler(Of ListViewUpdateEventArgs) 

이벤트 유형

예제

다음 예제에서는 이벤트에 대 한 이벤트 처리기를 추가 하는 방법을 보여 입니다 ItemUpdating .

중요

이 예제에는 사용자 입력을 허용하는 텍스트 상자가 있으므로 보안상 위험할 수 있습니다. 기본적으로 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">
    
  void Page_Load()
  {
    Message.Text = String.Empty;
  }
    
  //<Snippet2>
  void ContactsListView_ItemUpdating(Object sender, ListViewUpdateEventArgs e)
  {
    // Cancel the update operation if any of the fields is empty
    // or null.
    foreach (DictionaryEntry de in e.NewValues)
    {
      // Check if the value is null or empty.
      if (de.Value == null || de.Value.ToString().Trim().Length == 0)
      {
        Message.Text = "Cannot set a field to an empty value.";
        e.Cancel = true;
      }
    }
    
    // Convert the email address to lowercase.
    String emailValue = e.NewValues["EmailAddress"].ToString();
    e.NewValues["EmailAddress"] = emailValue.ToLower();

  }
  //</Snippet2>
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView.ItemUpdating Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListView.ItemUpdating Example</h3>
            
      <asp:Label ID="Message"
        ForeColor="Red"          
        runat="server"/>
      <br/>

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        OnItemUpdating="ContactsListView_ItemUpdating"  
        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>
              <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
            <td>
              <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            </td>
          </tr>
        </ItemTemplate>
        <EditItemTemplate>
          <tr style="background-color:#ADD8E6">
            <td valign="top">
              <asp:Label runat="server" ID="FirstNameLabel" 
                AssociatedControlID="FirstNameTextBox" Text="First Name"/>
              <asp:TextBox ID="FirstNameTextBox" runat="server" Width="200px"
                Text='<%#Bind("FirstName") %>' MaxLength="50" /><br />
              <asp:Label runat="server" ID="LastNameLabel" 
                AssociatedControlID="LastNameTextBox" Text="Last Name" />
              <asp:TextBox ID="LastNameTextBox" runat="server" Width="200px"
                Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
              <asp:Label runat="server" ID="EmailLabel"
                AssociatedControlID="EmailTextBox" Text="Email" />
              <asp:TextBox ID="EmailTextBox" runat="server" Width="200px"
                Text='<%#Bind("EmailAddress") %>' MaxLength="50" />
            </td>
            <td colspan="2" valign="top">
              <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
              <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
            </td>
          </tr>
        </EditItemTemplate>
      </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"
        UpdateCommand="UPDATE Person.Contact
          Set [FirstName] = @FirstName, [LastName] = @LastName, [EmailAddress] = @EmailAddress 
          WHERE [ContactID] = @ContactID">
      </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">
    
  Sub Page_Load()
    Message.Text = String.Empty
  End Sub
  
  '<Snippet2>
  Sub ContactsListView_ItemUpdating(ByVal sender As Object, ByVal e As ListViewUpdateEventArgs)
    
    ' Cancel the update operation if any of the fields is empty
    ' or null.
    For Each de As DictionaryEntry In e.NewValues
      ' Check if the value is null or empty
      If de.Value Is Nothing OrElse de.Value.ToString().Trim().Length = 0 Then
        Message.Text = "Cannot set a field to an empty value."
        e.Cancel = True
      End If
    Next
    
    ' Convert the email address to lowercase.
    Dim emailValue As String = e.NewValues("EmailAddress").ToString()    
    e.NewValues("EmailAddress") = emailValue.ToLower()
    
  End Sub
  '</Snippet2>
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>ListView.ItemUpdating Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
        
      <h3>ListView.ItemUpdating Example</h3>
            
      <asp:Label ID="Message"
        ForeColor="Red"          
        runat="server"/>
      <br/>

      <asp:ListView ID="ContactsListView" 
        DataSourceID="ContactsDataSource" 
        DataKeyNames="ContactID"
        OnItemUpdating="ContactsListView_ItemUpdating"  
        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>
              <asp:Label ID="EmailLabel" runat="server" Text='<%#Eval("EmailAddress") %>' />
            </td>
            <td>
              <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
            </td>
          </tr>
        </ItemTemplate>
        <EditItemTemplate>
          <tr style="background-color:#ADD8E6">
            <td valign="top">
              <asp:Label runat="server" ID="FirstNameLabel" 
                AssociatedControlID="FirstNameTextBox" Text="First Name"/>
              <asp:TextBox ID="FirstNameTextBox" runat="server" Width="200px"
                Text='<%#Bind("FirstName") %>' MaxLength="50" /><br />
              <asp:Label runat="server" ID="LastNameLabel" 
                AssociatedControlID="LastNameTextBox" Text="Last Name" />
              <asp:TextBox ID="LastNameTextBox" runat="server" Width="200px"
                Text='<%#Bind("LastName") %>' MaxLength="50" /><br />
              <asp:Label runat="server" ID="EmailLabel"
                AssociatedControlID="EmailTextBox" Text="Email" />
              <asp:TextBox ID="EmailTextBox" runat="server" Width="200px"
                Text='<%#Bind("EmailAddress") %>' MaxLength="50" />
            </td>
            <td colspan="2" valign="top">
              <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
              <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
            </td>
          </tr>
        </EditItemTemplate>
      </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"
        UpdateCommand="UPDATE Person.Contact
          Set [FirstName] = @FirstName, [LastName] = @LastName, [EmailAddress] = @EmailAddress 
          WHERE [ContactID] = @ContactID">
      </asp:SqlDataSource>
      
    </form>
  </body>
</html>

설명

이벤트는 ItemUpdating 항목의 업데이트 단추를 클릭하거나 메서드가 UpdateItem 호출될 때 발생하지만 컨트롤이 ListView 항목을 업데이트하기 전에 발생합니다. (업데이트 단추는 속성이 CommandName "업데이트"로 설정된 단추 컨트롤입니다.) 이렇게 하면 업데이트 작업 취소와 같은 이 이벤트가 발생할 때마다 사용자 지정 루틴을 수행할 수 있습니다.

ListViewUpdateEventArgs 개체가 이벤트 처리기에 전달되므로 현재 항목의 인덱스를 확인할 수 있습니다. 또한 업데이트 작업을 취소해야 함을 나타낼 수 있습니다. 업데이트 작업을 취소 하려면 합니다 Cancel 의 속성을 ListViewUpdateEventArgs 개체를 true입니다.

값이 데이터 원본에 Keys전달되기 전에 , OldValuesNewValues 컬렉션으로 작업할 수 있습니다. 이러한 컬렉션을 사용하는 일반적인 방법은 사용자가 제공한 값을 데이터 원본에 저장하기 전에 HTML로 인코딩하는 것입니다. 이렇게 하면 스크립트 삽입 공격을 방지할 수 있습니다.

이벤트를 처리 하는 방법에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.

적용 대상

추가 정보