When you change to edit mode, rebinding is always needed, that's by design
(like you need to when sorting or paging with DataGrid). One solution in
delete case is to reset the index (after user has deleted an item), or get
the datasource beforehand and check that is the (item count in data source -
1) less than current EditItemIndex, if it is resetr the counter or set it to
the (item count -1)
--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
I have handlers for all my events. I have viewstate enabled.
If we take the simple example - the EditCommandEvent:
Sub BindPorts()
colBooks = Port.GetBooksByPortNameFilter(txtBookFilter.Text)
dtlBooks.DataSource = colBooks
dtlBooks.DataBind()
End Sub
Private Sub dtlPorts_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataListCommandEventArgs) Handles
dtlPorts.EditCommand
dtlBooks.EditItemIndex = e.Item.ItemIndex
BindBooks()
End Sub
As you can seen I am re-binding after every EditCommand - this is because if
I do not do this I have to press my EditCommand button twice to get it to
work (it triggers the Event the first time, but not the second, and then
goes into Edit Mode on the DataList). I'm not sure why I need the re-binding
as I have viewstate on.
So, since I'm rebinding, if a user has deleted a book, the index the user
selected will be wrong - and the user actually edits the book next in the
list (than the one he selected).
Not sure how to get around it? I could save the Collection (that is assigned
to the DataList) in session state, but this seems a bit much. Or I could get
the Commands working with just viewstate and no re-binding.
But as long as I'm re-binding there is going to be this problem?