SqlDataSourceView.DeleteParameters 属性

定义

获取参数集合,该集合包含由 DeleteCommand 属性使用的参数。

public:
 property System::Web::UI::WebControls::ParameterCollection ^ DeleteParameters { System::Web::UI::WebControls::ParameterCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public System.Web.UI.WebControls.ParameterCollection DeleteParameters { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.DeleteParameters : System.Web.UI.WebControls.ParameterCollection
Public ReadOnly Property DeleteParameters As ParameterCollection

属性值

ParameterCollection

ParameterCollection,它包含 DeleteCommand 属性所使用的参数。

属性

示例

下面的代码示例演示如何设置 DeleteCommand 文本以从 Northwind 数据库中删除订单。 最初,将从 Orders 表中检索数据,并在控件中 DropDownList 显示。 You must explicitly declare the DeleteParameters property and call the Delete method when using data-bound controls, such as the DropDownList (unlike controls, such as the GridView and DetailsView, which automatically populate the parameters and call the Delete method on a data source control). 在此示例中,事件 OnClick 委托给专用 OnDeleted 事件处理程序,该处理程序显式调用 Delete 控件的方法 SqlDataSource

<%@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 OnDelete(Object sender, EventArgs e) {
    SqlDataSource1.Delete();
}
</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"
                ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
                SelectCommand="SELECT OrderID FROM Orders"
                DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;">
                <DeleteParameters>
                    <asp:ControlParameter Name="OrderID" ControlId="DropDownList1" PropertyName="SelectedValue" />
                </DeleteParameters>
            </asp:SqlDataSource>

            <asp:DropDownList
                id="DropDownList1"
                runat="server"
                DataTextField="OrderID"
                DataValueField="OrderID"
                DataSourceID="SqlDataSource1">
            </asp:DropDownList>

            <asp:Button
                id="Button1"
                runat="server"
                Text="Delete Order"
                OnClick="OnDelete">
            </asp:Button>

        </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 On_Delete(ByVal sender As Object, ByVal e As EventArgs)
    SqlDataSource1.Delete()
 End Sub 'On_Delete
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>ASP.NET Example</title>
</head>

    <body>
        <form id="form1" runat="server">

            <asp:SqlDataSource
                id="SqlDataSource1"
                runat="server"
                ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
                SelectCommand="SELECT OrderID FROM Orders"
                DeleteCommand="DELETE FROM [Order Details] WHERE OrderID=@OrderID;DELETE FROM Orders WHERE OrderID=@OrderID;">
                <DeleteParameters>
                    <asp:ControlParameter Name="OrderID" ControlId="DropDownList1" PropertyName="SelectedValue" />
                </DeleteParameters>
            </asp:SqlDataSource>

            <asp:DropDownList
                id="DropDownList1"
                runat="server"
                DataTextField="OrderID"
                DataValueField="OrderID"
                DataSourceID="SqlDataSource1">
            </asp:DropDownList>

            <asp:Button
                id="Button1"
                runat="server"
                Text="Delete Order"
                OnClick="On_Delete">
            </asp:Button>

        </form>
    </body>
</html>

注解

DeleteCommand如果该属性包含参数化SQL查询,则DeleteParameters集合包含与SQL字符串中的参数占位符相对应的任何Parameter对象。

参数名称可能受OldValuesParameterFormatString属性的影响;具体而言,如果名称标识主键(例如使用DataKeyNames数据绑定控件的属性指定的键),或者在将属性设置为CompareAllValues值并传递给相应数据方法的删除和更新方案中ConflictDetection指定一组oldValues键。 在这种情况下,格式字符串将应用于集合中的每个 oldValues 参数名称。

根据 ADO.NET 提供程序,集合中的 DeleteParameters 参数顺序可能很重要。 这些System.Data.OleDb参数和System.Data.Odbc提供程序根据参数化SQL查询中显示的顺序关联集合中的参数。 该System.Data.SqlClient提供程序是控件的默认 ADO.NET 提供程序SqlDataSource,通过将参数的名称与SQL查询中的占位符匹配来关联集合中的参数。 有关参数化SQL查询和命令的详细信息,请参阅将参数与 SqlDataSource 控件配合使用

适用于

另请参阅