DataList Issues

G

Guest

I'm a bit confused by the datalist. I can put an item in the datalist in editmode by the following

dtlBooks.EditItemIndex = e.Item.ItemInde
BindBooks(

This works great, the problem is when I bind the books if less or more books get returned the ItemIndex points to a different book - so the user gets the wrong one in editmode

Is there a way to stop this? If the user deletes it's even worse :)
 
T

Teemu Keiski

Hi,

the index points to certain item in the data source (count of data items) so
basically if the list of item changes (starting from beginning), it will
select different one. So question is that why does the list change between
rebindings and do you get the index corrently (you seem to take it from
EditCommand event handler?). And do you have ViewState enabled?

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist

Ian O'Rourke said:
I'm a bit confused by the datalist. I can put an item in the datalist in editmode by the following:

dtlBooks.EditItemIndex = e.Item.ItemIndex
BindBooks()

This works great, the problem is when I bind the books if less or more
books get returned the ItemIndex points to a different book - so the user
gets the wrong one in editmode?
 
F

Fandomlife

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 = colBook
dtlBooks.DataBind(

End Su

Private Sub dtlPorts_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dtlPorts.EditComman

dtlBooks.EditItemIndex = e.Item.ItemInde
BindBooks(

End Su

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?
 
F

Fandomlife

Wasn't watching what I was doing - wherever you see Port, change it to Book :) It's not like that in the application.
 
F

Fandomlife

For Clarity Code is

If we take the simple example - the EditCommandEvent

Sub BindBooks(

colBooks = Book.GetBooksByBookNameFilter(txtBookFilter.Text
dtlBooks.DataSource = colBook
dtlBooks.DataBind(

End Su

Private Sub dtlBooks_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dtlBooks.EditComman

dtlBooks.EditItemIndex = e.Item.ItemInde
BindBooks(

End Su
 
T

Teemu Keiski

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top