SqlDataSourceView.Deleting 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在刪除作業之前發生。
public:
event System::Web::UI::WebControls::SqlDataSourceCommandEventHandler ^ Deleting;
public event System.Web.UI.WebControls.SqlDataSourceCommandEventHandler Deleting;
member this.Deleting : System.Web.UI.WebControls.SqlDataSourceCommandEventHandler
Public Custom Event Deleting As SqlDataSourceCommandEventHandler
事件類型
範例
下列程式碼範例示範如何處理 Deleting 在刪除作業發生之前引發的事件。 因為這個範例會從 Northwind 資料庫刪除資料, OnDeleting 所以會新增處理常式,以在執行刪除作業之前嘗試備份資料庫到磁片。
<%@Page Language="C#" %>
<%@Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private void OnRecordDeleting(Object source, SqlDataSourceCommandEventArgs e) {
// Cancel the delete operation if the checkbox is not checked.
if (! CheckBox1.Checked) {
e.Cancel = true;
Label1.Text = "The command was cancelled because the CheckBox was not checked.";
}
}
private void OnRecordDeleted(object source, SqlDataSourceStatusEventArgs e) {
Label1.Text = e.AffectedRows + " row(s) were deleted";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT * FROM Orders"
DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;"
OnDeleting="OnRecordDeleting"
OnDeleted="OnRecordDeleted">
</asp:SqlDataSource>
<br />
<asp:CheckBox
id="CheckBox1"
runat="server"
autopostback="true"
text="Check To Delete Data" />
<br />
<br />
<asp:GridView
id="GridView1"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="OrderID"
AutoGenerateDeleteButton="True"
AllowPaging="True"
PageSize="20"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField HeaderText="Order ID" DataField="OrderID" />
<asp:BoundField HeaderText="Customer" DataField="CustomerID" />
<asp:BoundField HeaderText="Order Placed" DataField="OrderDate" />
<asp:BoundField HeaderText="Order Shipped" DataField="ShippedDate" />
</Columns>
</asp:GridView>
<asp:Label
id="Label1"
runat="server">
</asp:Label>
</form>
</body>
</html>
<%@Page Language="VB" %>
<%@Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub On_Record_Deleting(ByVal source As Object, ByVal e As SqlDataSourceCommandEventArgs)
' Cancel the delete operation if the checkbox is not checked.
If Not CheckBox1.Checked
e.Cancel = True
Label1.Text = "The command was cancelled because the CheckBox was not checked."
End If
End Sub 'On_Record_Deleting
Sub On_Record_Deleted(ByVal source As Object, ByVal e As SqlDataSourceStatusEventArgs)
Label1.Text = e.AffectedRows & " row(s) were deleted"
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
SelectCommand="SELECT * FROM Orders"
DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;"
OnDeleting="On_Record_Deleting"
OnDeleted="On_Record_Deleted">
</asp:SqlDataSource>
<br />
<asp:CheckBox
id="CheckBox1"
runat="server"
autopostback="true"
text="Check To Delete Data" />
<br />
<br />
<asp:GridView
id="GridView1"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="OrderID"
AutoGenerateDeleteButton="True"
AllowPaging="True"
PageSize="20"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField HeaderText="Order ID" DataField="OrderID" />
<asp:BoundField HeaderText="Customer" DataField="CustomerID" />
<asp:BoundField HeaderText="Order Placed" DataField="OrderDate" />
<asp:BoundField HeaderText="Order Shipped" DataField="ShippedDate" />
</Columns>
</asp:GridView>
<asp:Label
id="Label1"
runat="server">
</asp:Label>
</form>
</body>
</html>
備註
Deleting處理 事件以執行應用程式特有的其他初始化作業、驗證參數的值,或變更控制項執行刪除作業之前 SqlDataSource 的參數值。
呼叫事件處理常式委派時,基礎資料來源的連接尚未開啟。 因此,您無法直接在 物件所 SqlDataSourceCommandEventArgs 公開的物件上 DbCommand 呼叫 Cancel 方法來取消 Delete 作業。 不過,您可以將 的 SqlDataSourceCommandEventArgs 屬性設定 Cancel 為 true
來取消作業。
如需如何處理事件的詳細資訊,請參閱 處理和引發事件。