ObjectDataSourceView.DeleteMethod 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置 ObjectDataSourceView 对象调用以删除数据的方法或函数的名称。
public:
property System::String ^ DeleteMethod { System::String ^ get(); void set(System::String ^ value); };
public string DeleteMethod { get; set; }
member this.DeleteMethod : string with get, set
Public Property DeleteMethod As String
属性值
一个字符串,表示 ObjectDataSourceView 用于删除数据的方法或函数的名称。 默认值为空字符串("")。
示例
下面的代码示例演示如何将 ObjectDataSource 控件与业务对象和 控件一 GridView 起使用来删除数据。
GridView最初显示一组所有员工,使用 属性指定的 SelectMethod 方法从 EmployeeLogic
对象检索数据。 由于 属性 AutoGenerateDeleteButton 设置为 true
,因此控件 GridView 会自动显示 “删除” 按钮。
如果单击“ 删除 ”按钮,将使用 Delete 属性指定的 DeleteMethod 方法和集合中指定的 DeleteParameters 任何参数执行操作。 在此代码示例中,还执行了一些预处理和后处理步骤。 调用NorthwindEmployeeDeleting
委托以在执行操作之前Delete处理Deleting事件,调用NorthwindEmployeeDeleted
委托以在操作完成后处理Deleted事件Delete,以执行异常处理。 在此示例中,如果 NorthwindDataException
引发 ,则由此委托处理。
若要检查此代码示例使用的中间层业务对象的实现 EmployeeLogic
,请参阅 ObjectDataSourceStatusEventArgs。
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %>
<%@ Import namespace="Samples.AspNet.CS" %>
<%@ 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">
private void NorthwindEmployeeDeleting(object source, ObjectDataSourceMethodEventArgs e)
{
// The GridView passes the ID of the employee
// to be deleted. However, the buisiness object, EmployeeLogic,
// requires a NorthwindEmployee parameter, named "ne". Create
// it now and add it to the parameters collection.
IDictionary paramsFromPage = e.InputParameters;
if (paramsFromPage["EmpID"] != null) {
NorthwindEmployee ne
= new NorthwindEmployee( Int32.Parse(paramsFromPage["EmpID"].ToString()));
// Remove the old EmpID parameter.
paramsFromPage.Clear();
paramsFromPage.Add("ne", ne);
}
}
private void NorthwindEmployeeDeleted(object source, ObjectDataSourceStatusEventArgs e)
{
// Handle the Exception if it is a NorthwindDataException
if (e.Exception != null)
{
// Handle the specific exception type. The ObjectDataSource wraps
// any Exceptions in a TargetInvokationException wrapper, so
// check the InnerException property for expected Exception types.
if (e.Exception.InnerException is NorthwindDataException)
{
Label1.Text = e.Exception.InnerException.Message;
// Because the exception is handled, there is
// no reason to throw it.
e.ExceptionHandled = true;
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ObjectDataSource - C# Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:gridview
id="GridView1"
runat="server"
datasourceid="ObjectDataSource1"
autogeneratedeletebutton="true"
autogeneratecolumns="false"
datakeynames="EmpID">
<columns>
<asp:boundfield headertext="EmpID" datafield="EmpID" />
<asp:boundfield headertext="First Name" datafield="FirstName" />
<asp:boundfield headertext="Last Name" datafield="LastName" />
</columns>
</asp:gridview>
<asp:objectdatasource
id="ObjectDataSource1"
runat="server"
selectmethod="GetAllEmployees"
deletemethod="DeleteEmployee"
ondeleting="NorthwindEmployeeDeleting"
ondeleted="NorthwindEmployeeDeleted"
typename="Samples.AspNet.CS.EmployeeLogic">
<deleteparameters>
<asp:parameter name="EmpID" type="Int32" />
</deleteparameters>
</asp:objectdatasource>
<asp:label id="Label1" runat="server" />
</form>
</body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %>
<%@ Import namespace="Samples.AspNet.VB" %>
<%@ 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">
' Called before a Delete operation.
Private Sub NorthwindEmployeeDeleting(ByVal source As Object, ByVal e As ObjectDataSourceMethodEventArgs)
' The GridView passes the ID of the employee
' to be deleted. However, the business object, EmployeeLogic,
' requires a NorthwindEmployee parameter, named "ne". Create
' it now and add it to the parameters collection.
Dim paramsFromPage As IDictionary = e.InputParameters
If Not paramsFromPage("EmpID") Is Nothing Then
Dim ne As New NorthwindEmployee(paramsFromPage("EmpID").ToString())
' Remove the old EmpID parameter.
paramsFromPage.Clear()
paramsFromPage.Add("ne", ne)
End If
End Sub ' NorthwindEmployeeDeleting
' Called after a Delete operation.
Private Sub NorthwindEmployeeDeleted(ByVal source As Object, ByVal e As ObjectDataSourceStatusEventArgs)
' Handle the Exception if it is a NorthwindDataException.
If Not e.Exception Is Nothing Then
' Handle the specific exception type. The ObjectDataSource wraps
' any Exceptions in a TargetInvokationException wrapper, so
' check the InnerException property for the expected Exception types.
If e.Exception.InnerException.GetType().Equals(GetType(NorthwindDataException)) Then
Label1.Text = e.Exception.InnerException.Message
' Because the exception is handled, there is
' no reason to throw it.
e.ExceptionHandled = True
End If
End If
End Sub ' NorthwindEmployeeDeleted
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ObjectDataSource - VB Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<asp:gridview
id="GridView1"
runat="server"
datasourceid="ObjectDataSource1"
autogeneratedeletebutton="true"
autogeneratecolumns="false"
datakeynames="EmpID">
<columns>
<asp:boundfield headertext="EmpID" datafield="EmpID" />
<asp:boundfield headertext="First Name" datafield="FirstName" />
<asp:boundfield headertext="Last Name" datafield="LastName" />
</columns>
</asp:gridview>
<asp:objectdatasource
id="ObjectDataSource1"
runat="server"
selectmethod="GetAllEmployees"
deletemethod="DeleteEmployee"
ondeleting="NorthwindEmployeeDeleting"
ondeleted="NorthwindEmployeeDeleted"
typename="Samples.AspNet.VB.EmployeeLogic">
<deleteparameters>
<asp:parameter name="EmpID" type="Int32" />
</deleteparameters>
</asp:objectdatasource>
<asp:label id="Label1" runat="server" />
</form>
</body>
</html>
注解
属性 DeleteMethod 标识的方法可以是实例方法,也可以是 static
Visual Basic) Shared
方法中的 (。 如果它是实例方法,则每次调用属性指定的 DeleteMethod 方法时,都会创建并销毁业务对象。 在调用 属性指定的 DeleteMethod 方法之前,可以处理 ObjectCreated 事件以使用业务对象。 还可以处理 ObjectDisposing 在调用 属性指定的 DeleteMethod 方法后引发的事件。 如果方法是 static
Visual Basic) 方法中的 (Shared
,则永远不会创建业务对象,并且无法处理这些事件。
如果控件使用的业务对象 ObjectDataSource 实现多个具有相同名称的方法或函数 (方法重载) ,则数据源控件会尝试根据一组条件(包括集合中的 DeleteParameters 参数)调用正确的方法或函数。 如果集合中的 DeleteParameters 参数与方法签名的参数 DeleteMethod 不匹配,则数据源将引发异常。
属性的值 DeleteMethod 存储在视图状态中。
有关详细信息,请参阅 DeleteMethod。