ObjectDataSource.DeleteParameters Property
Gets the parameters collection that contains the parameters that are used by the DeleteMethod method.
Assembly: System.Web (in System.Web.dll)
<PersistenceModeAttribute(PersistenceMode.InnerProperty)> Public ReadOnly Property DeleteParameters As ParameterCollection
Property Value
Type: System.Web.UI.WebControls.ParameterCollectionA ParameterCollection that contains the parameters used by the DeleteMethod method.
The names and types of the parameters that are contained in the DeleteParameters collection must match the names and types of the parameters in the DeleteMethod method signature. The parameter names are affected by the OldValuesParameterFormatString property and are case-sensitive. The parameters in the collection depend on the data that is in the data-bound control, the parameters that are specified declaratively, and the parameters that are added programmatically. For more information, see "Parameter Merging" in Delete and Using Parameters with the ObjectDataSource Control.
The DeleteParameters property retrieves the DeleteParameters property that is contained by the ObjectDataSourceView object that is associated with the ObjectDataSource control.
For more information about parameter merging, object lifetime, and method resolution, see DeleteMethod.
This section contains two code examples. The first code example demonstrates how to use an ObjectDataSource object with a business object and a GridView control to delete data. The second code example shows the EmployeeLogic class that is used in the first code example.
The following code example demonstrates how to use the DeleteParameters property of the ObjectDataSource control with a business object and a GridView control to delete data. Initially, the GridView control displays a set of all employees, using the method that is specified by the SelectMethod property to retrieve the data from the EmployeeLogic object. Because the AutoGenerateDeleteButton property is set to true, the GridView control automatically displays a Delete button.
If you click the Delete button, the Deleting event is called with two parameters. One parameter, EmpID, comes from the DataGrid control, and is based on the DataKeyNames property. The other parameter comes from the declarative DeleteParameters property. The EmployeeLogic class overloads the DeleteEmployee method. One overload takes a NorthwindEmployee parameter, and the other takes an Int32 parameter named anID. In the Deleting event, the entries in the InputParameters property are adjusted to include only one parameter named anID with the value from original_EmpID. When the event handler exists, the matching DeleteEmployee method is called.
The following code example shows the Web page that displays the DataGrid control and handles the Deleting event.
<%@ 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"> Private Sub NorthwindEmployeeDeleting(source As Object, e As ObjectDataSourceMethodEventArgs) Dim paramsFromPage As IDictionary = e.InputParameters paramsFromPage.Remove("anID") paramsFromPage.Add("anID", CInt(paramsFromPage("EmpID"))) paramsFromPage.Remove("EmpID") End Sub ' NorthwindEmployeeDeleting </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" DataKeyNames="EmpID"> </asp:gridview> <asp:objectdatasource id="ObjectDataSource1" runat="server" selectmethod="GetAllEmployees" deletemethod="DeleteEmployee" OnDeleting="NorthwindEmployeeDeleting" typename="Samples.AspNet.VB.EmployeeLogic"> <deleteparameters> <asp:parameter name="anID" type="Int32" /> </deleteparameters> </asp:objectdatasource> </form> </body> </html>
The following code example shows the EmployeeLogic class that is used in the preceding code example.
Imports System Imports System.Collections Imports System.Configuration Imports System.Data Imports System.Data.SqlClient Imports System.Web.UI Imports System.Web.UI.WebControls Namespace Samples.AspNet.VB ' ' EmployeeLogic is a stateless business object that encapsulates ' the operations you can perform on a NorthwindEmployee object. Public Class EmployeeLogic ' Returns a collection of NorthwindEmployee objects. Public Shared Function GetAllEmployees() As ICollection Dim al As New ArrayList() ' Use the SqlDataSource class to wrap the ' ADO.NET code required to query the database. Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection") Dim sds As New SqlDataSource(cts.ConnectionString, "SELECT EmployeeID FROM Employees") Try Dim IDs As IEnumerable = sds.Select(DataSourceSelectArguments.Empty) ' Iterate through the Enumeration and create a ' NorthwindEmployee object for each ID. Dim enumerator As IEnumerator = IDs.GetEnumerator() While enumerator.MoveNext() ' The IEnumerable contains DataRowView objects. Dim row As DataRowView = CType(enumerator.Current,DataRowView) Dim id As String = row("EmployeeID").ToString() Dim nwe As New NorthwindEmployee(CInt(id)) ' Add the NorthwindEmployee object to the collection. al.Add(nwe) End While Finally ' If anything strange happens, clean up. sds.Dispose() End Try Return al End Function 'GetAllEmployees Public Shared Function GetEmployee(anID As Integer) As NorthwindEmployee Return New NorthwindEmployee(anID) End Function 'GetEmployee Public Shared Sub DeleteEmployee(ne As NorthwindEmployee) Dim retval As Boolean = ne.Delete() If Not retval Then Throw New NorthwindDataException("Employee delete failed.") End If ' Delete the object in memory. ne = Nothing End Sub 'DeleteEmployee Public Shared Sub DeleteEmployee(anID As Integer) Dim tempEmp As New NorthwindEmployee(anID) DeleteEmployee(tempEmp) End Sub 'DeleteEmployeeByID ' And so on... End Class 'EmployeeLogic Public Class NorthwindEmployee Public Sub New(anID As Integer) Me.ID = anID Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection") Dim conn As New SqlConnection(cts.ConnectionString) Dim sc As New SqlCommand(" SELECT FirstName,LastName " & _ " FROM Employees " & _ " WHERE EmployeeID = @empId", conn) ' Add the employee ID parameter and set its value. sc.Parameters.Add(New SqlParameter("@empId", SqlDbType.Int)).Value = Int32.Parse(anID.ToString()) Dim sdr As SqlDataReader = Nothing Try conn.Open() sdr = sc.ExecuteReader() ' This is not a while loop. It only loops once. If Not (sdr Is Nothing) AndAlso sdr.Read() Then ' The IEnumerable contains DataRowView objects. Me.firstName = sdr("FirstName").ToString() Me.lastName = sdr("LastName").ToString() Else Throw New NorthwindDataException("Data not loaded for employee id.") End If Finally Try If Not (sdr Is Nothing) Then sdr.Close() End If conn.Close() Catch se As SqlException ' Log an event in the Application Event Log. Throw End Try End Try End Sub 'NewNew Private ID As Object Public ReadOnly Property EmpID() As Object Get Return ID End Get End Property Private aLastName As String Public Property LastName() As String Get Return aLastName End Get Set aLastName = value End Set End Property Private aFirstName As String Public Property FirstName() As String Get Return aFirstName End Get Set aFirstName = value End Set End Property Public Function Delete() As Boolean If ID.Equals(DBNull.Value) Then ' The Employee object is not persisted. Return True Else ' The Employee object is persisted. ' Use the SqlDataSource control as a convenient wrapper for ' the ADO.NET code needed to delete a record from the database. Dim cts As ConnectionStringSettings = ConfigurationManager.ConnectionStrings("NorthwindConnection") Dim sds As New SqlDataSource() Try sds.ConnectionString = cts.ConnectionString sds.DeleteCommand = "DELETE FROM Employees WHERE EmployeeID=@empID;" Dim param As New Parameter("empID", TypeCode.Int32, CStr(Me.ID)) sds.DeleteParameters.Add(param) ' To make this sample fully functional, uncomment these lines ' int retval = sds.Delete(); ' if (retval == 1) { return true; } ' return false; Return True Finally sds.Dispose() End Try End If End Function 'Delete End Class 'NorthwindEmployee Friend Class NorthwindDataException Inherits Exception Public Sub New(msg As String) MyBase.New(msg) End Sub 'NewNew End Class 'NorthwindDataException End Namespace
Available since 2.0