Performing Delete Operations
With the OpenAccessLinqDataSource control, you can create ASP.NET Web pages that enable users to insert, update, and delete data. You don't have to write any additional code, because the OpenAccessLinqDataSource control dynamically performs these operations. To let users modify data, you can enable insert, update, and delete operations on the OpenAccessLinqDataSource control. You can then connect the control to a data-bound control that lets the users insert, update, and delete data, such as the RadGrid control.
This topic describes how to enable the OpenAccessLinqDataSource control to automatically handle delete operations. Finally, the topic makes an overview of the Deleting/Deleted events, exposed by the OpenAccessLinqDataSource control.
How to: Enable Automatic Deletes
To enable a data-bound control to automatically perform delete operations, you must do the following:
-
Connect the OpenAccessLinqDataSource control to a context class and specify the target table. To do that you need to set values for the ContextTypeName, and ResourceSetName properties:
<telerik:OpenAccessLinqDataSource ID="OpenAccessLinqDataSourceCategory" runat="server" ContextTypeName="SofiaCarRental.Model.FluentModel" ResourceSetName="Categories"> </telerik:OpenAccessLinqDataSource>
-
Set the EnableDelete property to True:
<telerik:OpenAccessLinqDataSource ID="OpenAccessLinqDataSourceCategory" runat="server" EnableDelete="True" ContextTypeName="SofiaCarRental.Model.FluentModel" ResourceSetName="Categories"> </telerik:OpenAccessLinqDataSource>
The control should not be set up to execute any GroupBy operations, or to retrieve a subset of the class properties.
Using the Deleting/Deleted Events
The Deleting event occurs before a delete operation. You could handle the Deleting event to validate the object to be deleted, to change a value before the delete operation, or to cancel the delete operation. The OpenAccessLinqDataSource control passes an OpenAccessLinqDataSourceDeleteEventArgs object to the event handler for the Deleting event. The OriginalObject property contains the data that will be deleted. The OpenAccessLinqDataSourceDeleteEventArgs derives from CancelEventArgs, i.e. it is a cancelable event. You can cancel the delete operation by setting the Cancel property to True.
protected void OpenAccessLinqDataSourceCategory_Deleting(object sender,
Telerik.OpenAccess.Web.OpenAccessLinqDataSourceDeleteEventArgs e)
{
Category categoryToDelete = e.OriginalObject as Category;
if (categoryToDelete.CategoryName == "SUV")
{
e.Cancel = true;
}
}
Protected Sub OpenAccessLinqDataSourceCategory_Deleting(ByVal sender As Object, _
ByVal e As Telerik.OpenAccess.Web.OpenAccessLinqDataSourceDeleteEventArgs) _
Handles OpenAccessLinqDataSource.Deleting
Dim categoryToDelete As Category = TryCast(e.OriginalObject, Category)
If categoryToDelete.CategoryName = "SUV" Then
e.Cancel = True
End If
End Sub
The Deleted event occurs when a delete operation has finished. You could handle the Deleted event to catch and process any exceptions from the delete operation.