DetailsView.ItemDeleting Kejadian
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Terjadi saat tombol Hapus dalam DetailsView kontrol diklik, tetapi sebelum operasi penghapusan.
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
Jenis Acara
Contoh
Contoh kode berikut menunjukkan cara menggunakan ItemDeleting peristiwa untuk membatalkan operasi penghapusan jika pengguna mencoba menghapus item terakhir dari DetailsView kontrol.
<%@ 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>
Keterangan
Peristiwa ItemDeleting dinaikkan saat tombol Hapus dalam DetailsView kontrol diklik, tetapi sebelum operasi penghapusan. Ini memungkinkan Anda untuk menyediakan penanganan aktivitas yang melakukan rutinitas kustom, seperti membatalkan operasi penghapusan, setiap kali peristiwa ini terjadi.
Objek DetailsViewDeleteEventArgs diteruskan ke penanganan aktivitas, yang memungkinkan Anda menentukan indeks rekaman yang dihapus dan untuk menunjukkan bahwa operasi penghapusan harus dibatalkan. Untuk membatalkan operasi penghapusan, atur properti ke Canceltrue
. Anda juga dapat memanipulasi Keys koleksi dan Values , jika perlu, sebelum nilai diteruskan ke sumber data.
Untuk informasi selengkapnya tentang cara menangani peristiwa, lihat Menangani dan Menaikkan Peristiwa.