DataList Help

  • Thread starter Thread starter TCORDON
  • Start date Start date
T

TCORDON

I have a DataList with a bunch of items (eg. Shoppiong Cart) each Item has a
Remove button, in the code Behind page I have a Sub called Remove Item which
calls a Function like this:

Public Shared Function RemoveItem (t_ItemIndex as Integer) as String

This function removes the item from the DB.

My question is, first how do I tie up the command button for each item so
that when it loads it knows that it represents Item "X" this being the Id of
the item, and second, how do I make him call that function?

TIA!
 
Eliyahu said:
Look in the help for ItemCommand event.

Since it's about deleting, I would rather use the DeleteCommand event.

For the button set CommandName="Delete".

Add the DeleteCommand event handler to the DataList.

For the DataList, also set the DataKeyField property to "ID" (or the name of
the field).

Use code like this:

Sub DataList1_DeleteCommand(Sender As Object, E As DataListCommandEventArgs)
' e.Item is the item that was clicked on
RemoveItem(e.Item.ItemIndex))
BindDataList() ' rebind the datalist
End Sub
 
Didn't know even about this event. Looks like the only difference between
these 2 events is that in the case of ItemCommand you have to check
CommandName in the event handler.

Eliyahu
 
Thanks, I am still getting an error, please let me know if I missed
something.

This is what I have done so far.

1. In the Item Template, I named the Button "cmdDelete" and set its
CommandName Property to: Delete
2. I placed all the code to delete the record inside: Private Sub
lstItems_DeleteCommand(ByVal source.... )

What I am seeing is that that event is not getting fired.

TIA!
 
You've checked that lstItems_DeleteCommand appears as DeleteCommand event
handler in the datalist property panel, haven't you?

Eliyahu
 
Back
Top