DetailsView.ItemDeleting 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
發生於按一下 DetailsView 控制項內的 [刪除] 按鈕時,但是在刪除作業之前。
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
事件類型
範例
下列程式碼範例示範如果使用者嘗試從 DetailsView 控制項刪除最後一個專案,如何使用 ItemDeleting 事件來取消刪除作業。
<%@ 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 [刪除] 按鈕,但在刪除作業之前,就會引發事件。 這可讓您提供事件處理常式來執行自訂常式,例如每當發生此事件時取消刪除作業。
DetailsViewDeleteEventArgs物件會傳遞至事件處理常式,這可讓您判斷要刪除的記錄索引,並指出應該取消刪除作業。 若要取消刪除作業,請將 Cancel 屬性設定為 true
。 您也可以在值傳遞至資料來源之前,視需要操作 Keys 和 Values 集合。
如需如何處理事件的詳細資訊,請參閱 處理和引發事件。