SqlDataSourceView.DeleteParameters Proprietà

Definizione

Ottiene l'insieme di parametri che contiene i parametri utilizzati dalla proprietà 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

Valore della proprietà

ParameterCollection

Oggetto ParameterCollection contenente i parametri utilizzati dalla proprietà DeleteCommand.

Attributi

Esempio

Nell'esempio di codice seguente viene illustrato come impostare il DeleteCommand testo per eliminare un ordine dal database Northwind. Inizialmente, i dati vengono recuperati dalla tabella Orders e visualizzati in un DropDownList controllo . È necessario dichiarare in modo esplicito la DeleteParameters proprietà e chiamare il Delete metodo quando si usano controlli associati a dati, ad esempio DropDownList ( a differenza dei controlli , ad esempio GridView e DetailsView, che popolano automaticamente i parametri e chiamano il Delete metodo su un controllo origine dati). In questo esempio l'evento OnClick viene delegato al gestore eventi privato OnDeleted , che chiama in modo esplicito il Delete metodo del SqlDataSource controllo.

<%@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>

Commenti

Se la DeleteCommand proprietà contiene una query con parametri SQL, l'insieme DeleteParameters contiene tutti Parameter gli oggetti che corrispondono ai segnaposto dei parametri nella stringa SQL.

I nomi dei parametri possono essere interessati dalla OldValuesParameterFormatString proprietà; in particolare, se il nome identifica una chiave primaria, ad esempio una chiave specificata utilizzando la DataKeyNames proprietà di un controllo associato a dati oppure in scenari di eliminazione e aggiornamento in cui la ConflictDetection proprietà è impostata sul CompareAllValues valore e un set di viene passato al metodo di oldValues dati corrispondente. In questo caso, la stringa di formato viene applicata a ogni nome di parametro nella oldValues raccolta.

A seconda del provider ADO.NET, l'ordine dei parametri nella DeleteParameters raccolta potrebbe essere importante. I System.Data.OleDb provider e System.Data.Odbc associano i parametri nella raccolta in base all'ordine in cui i parametri vengono visualizzati nella query con parametri SQL. Il System.Data.SqlClient provider, ovvero il provider di ADO.NET predefinito per il SqlDataSource controllo, associa i parametri nella raccolta associando il nome del parametro al segnaposto nella query SQL. Per altre informazioni sui comandi e le query con parametri di SQL, vedere Uso di parametri con il controllo SqlDataSource.

Si applica a

Vedi anche