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的 对象调用 Cancel 方法来DbCommand直接取消Delete操作。 但是,可以通过将 的 SqlDataSourceCommandEventArgs 属性设置为 Canceltrue
来取消操作。
有关如何处理事件的详细信息,请参阅 处理和引发事件。