Updating ListView from Business Logic Layer Method
I have a shopping cart ListView
control backed by a custom object that has methods for selecting and deleting. In the ListView
, one of the columns is an ImageButton
that is essentially a delete icon, with the CommandName
parameter set to CommandName="Delete"
.
My issue is related to updating the shopping cart ListView
on delete; as the delete method is stored in the custom business entity object, I am having trouble in terms of updating the calculations made inside the cart (we have deleted an item, so I need to recalc shipping, taxes, etc).
Right now, I have the cart ListView
control calling it's calculate method in page load on Not Page.IsPostBack
, but obviously this isn't triggered by 开发者_开发百科the delete method.
Any recommendations on where to handle calling the calculate method on deletion when deletion is happening outside of the scope of the control in my custom business logic?
Consider binding your recalculation method to your ListView
's ItemDeleted Event. For instance:
Sub ShoppingCartListView_ItemDeleted(sender As Object, e As ListViewDeletedEventArgs) Handles ShoppingCartListView.ItemDeleted
' Determine whether an exception occurred
If e.Exception Is Nothing Then
' Ensure that a record was deleted.
If e.AffectedRows > 0 Then
' Call recalculation method
CalculateCharges()
End If
Else
' Handle e.Exception
HandleMyException(e.Exception)
End If
End Sub
Honestly, the error checking for AffectedRows
might be a bit extraneous for your use case, but it never hurts to know what information is available to you in the ListViewDeletedEventArgs
.
EDIT:
If you need to add support to your business logic for information regarding the number of rows affected by the transaction, ensure first that the method called by the ObjectDataSource.DeleteMethod
property has a return value that is either an integer or has a property or function that will return one. Next, add a handler to your ObjectDataSource.Deleted event. Here, you can assign the return value from the method that is contained in the event arguments to another event argument member similarly called AffectedRows
. For instance:
Sub BusinessObjectDataSource_Deleted(sender as Object, e As ObjectDataSourceStatusEventArgs) Handles BusinessObjectDataSource.Deleted
e.AffectedRows = CInt(e.ReturnValue)
End Sub
Since e.ReturnValue
is passed in as an Object
, you can cast it as needed and access any particular property that has the information required to update e.AffectedRows
.
The value provided to these arguments will be passed on to your ListView.ItemDeleted
event.
精彩评论