DetailsView.ItemDeleting DetailsView.ItemDeleting DetailsView.ItemDeleting DetailsView.ItemDeleting Event

정의

DetailsView 컨트롤의 삭제 단추를 클릭하면 삭제 작업 전에 이 이벤트가 발생합니다.Occurs when a Delete button within a DetailsView control is clicked, but before the delete operation.

public:
 event System::Web::UI::WebControls::DetailsViewDeleteEventHandler ^ ItemDeleting;
public event System.Web.UI.WebControls.DetailsViewDeleteEventHandler ItemDeleting;
member this.ItemDeleting : System.Web.UI.WebControls.DetailsViewDeleteEventHandler 
Public Custom Event ItemDeleting As DetailsViewDeleteEventHandler 

예제

다음 코드 예제를 사용 하는 방법에 설명 합니다 ItemDeleting 사용자의 마지막 항목을 삭제 하려고 시도 하는 경우 삭제 작업을 취소 하는 이벤트를 DetailsView 제어 합니다.The following code example demonstrates how to use the ItemDeleting event to cancel the delete operation if the user attempts to delete the last item from the DetailsView control.


<%@ 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 CustomerDetailView_ItemDeleting(Object sender, DetailsViewDeleteEventArgs e)
  {
    // Cancel the delete operation if the user attempts to delete the last
    // record from the data source.
      if (CustomerDetailView.DataItemCount <= 1)
    {
      e.Cancel = true;
      MessageLabel.Text = "You must keep at least one store.";
    }
    else
    {
      MessageLabel.Text = "";
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView ItemDeleting Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView ItemDeleting Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneratedeletebutton="true"  
          autogeneraterows="true"
          allowpaging="true"
          onitemdeleting="CustomerDetailView_ItemDeleting" 
          runat="server">
               
          <fieldheaderstyle backcolor="Navy"
            forecolor="White"/>
                    
        </asp:detailsview>
        
        <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="DetailsViewSource" runat="server" 
          ConnectionString=
            "<%$ ConnectionStrings:NorthWindConnectionString%>"
            InsertCommand="INSERT INTO [Customers]([CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country]) VALUES (@CustomerID, @CompanyName, @Address, @City, @PostalCode, @Country)"
            SelectCommand="Select [CustomerID], [CompanyName], 
              [Address], [City], [PostalCode], [Country] 
              From [Customers]"
            DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = @CustomerID" >
        </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 CustomerDetailView_ItemDeleting(ByVal sender As Object, ByVal e As DetailsViewDeleteEventArgs)
        ' Cancel the delete operation if the user attempts to delete the last
        ' record from the data source.
        If (CustomerDetailView.DataItemCount <= 1) Then
            e.Cancel = True
            MessageLabel.Text = "You must keep at least one store."
        Else
            MessageLabel.Text = ""
        End If
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView ItemDeleting Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView ItemDeleting Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneratedeletebutton="true"  
          autogeneraterows="true"
          allowpaging="true"
          onitemdeleting="CustomerDetailView_ItemDeleting" 
          runat="server">
               
          <fieldheaderstyle backcolor="Navy"
            forecolor="White"/>
                    
        </asp:detailsview>
        
        <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="DetailsViewSource" runat="server" 
          ConnectionString=
            "<%$ ConnectionStrings:NorthWindConnectionString%>"
            InsertCommand="INSERT INTO [Customers]([CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country]) VALUES (@CustomerID, @CompanyName, @Address, @City, @PostalCode, @Country)"
            SelectCommand="Select [CustomerID], [CompanyName], 
              [Address], [City], [PostalCode], [Country] 
              From [Customers]"
            DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = @CustomerID" >
        </asp:SqlDataSource>
    </form>
  </body>
</html>

설명

합니다 ItemDeleting 이벤트는 삭제 단추를는 DetailsView 컨트롤을 클릭할 삭제 작업 전에 합니다.The ItemDeleting event is raised when a Delete button within the DetailsView control is clicked, but before the delete operation. 이 옵션을 사용 하면이 이벤트가 발생할 때마다 삭제 작업을 취소 하는 등 사용자 지정 루틴을 수행 하는 이벤트 처리기를 제공할 수 있습니다.This allows you to provide an event handler that performs a custom routine, such as canceling the delete operation, whenever this event occurs.

DetailsViewDeleteEventArgs 개체는 삭제할 레코드의 인덱스를 확인 하 고 삭제 작업을 취소 해야 함을 나타낼 수는 이벤트 처리기에 전달 됩니다.A DetailsViewDeleteEventArgs object is passed to the event handler, which allows you to determine the index of the record being deleted and to indicate that the delete operation should be canceled. 삭제 작업을 취소 하려면 합니다 Cancel 속성을 true입니다.To cancel the delete operation, set the Cancel property to true. 조작할 수도 있습니다는 KeysValues 컬렉션 값이 데이터 원본에 전달 되기 전에 필요한 경우.You can also manipulate the Keys and Values collections, if necessary, before the values are passed to the data source.

이벤트를 처리 하는 방법에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.For more information about how to handle events, see Handling and Raising Events.

적용 대상

추가 정보