e.Item.ItemIndex returns wrong value

S

Stephan

Hi,

I'm working on a page with multiple Datalist-controls on it. The
problem I'm facing is that when I try to update a record, the
ItemIndex returns a value which is one lower than the Item I want to
edit, so when I update the record, the previous record is changed. I
put some code here, but not all. Adding, of
course, is no problem because then the index is set to -1. I tried to
use the DataKeyField property in my controls, but that doesn't work
either. I've the feeling that I terribly overlooking something. What
am I doing wrong?

Thx in advance

If Not Page.IsPostBack Then
BindData()
End If


Private Function BindData()
Dim dsSubDoelen As DataSet = objDoelen.SelectSTBSubDoelen("")
Dim dsDoelen As DataSet = objDoelen.SelectSTBDoelen("")
Dim dsGedrag As DataSet = objDoelen.SelectGedrag("")
Dim dsMethodiek As DataSet = objDoelen.SelectMethodiek("")

dlSTBDoelen.DataSource = dsDoelen
dlSTBDoelen.DataBind()

dlSTBSubDoelen.DataSource = dsSubDoelen
dlSTBSubDoelen.DataBind()

dlGedrag.DataSource = dsGedrag
dlGedrag.DataBind()

dlMethodiek.DataSource = dsMethodiek
dlMethodiek.DataBind()

End Function

Private Sub dlSTBSubDoelen_EditCommand(ByVal source As Object,
ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles
dlSTBSubDoelen.EditCommand
Mode.Value = "Edit"
dlSTBSubDoelen.EditItemIndex = CInt(e.Item.ItemIndex) <--
return a value which is one to low
BindData()
End Sub
 
C

chanmmn

Just don't forget your array index start from 0 not 1. Hope that might help
you.

chanmm
 
S

Stephan Bussing

Hi,

Thx for your reply, but sorry that I forgot to mention. I know that
ItemIndex starts at 0. So what I tried is to add 1 to the index, but
then not the current, but the next row is in editmode.

I tried: dlSTBDoelen.EditItemIndex = CInt(e.Item.ItemIndex) + 1

I figured out that this caused the next row in editmode problem. So
maybe I'd better add +1 when updating the record. That of course should
do the trick. I'm only wondering how I can be sure that the right ID is
used. I mean what does the purpose does the DataKeyField of the Datalist
serve. How can I use it in my code. When I update I still user de
EditItemIndex which in my case has a wrong value.

Anyone?
 
S

Stephan Bussing

I actually forgot to mention that I tried several things, ie adding 1 to
the EditItemIndex when updating, this is not a good thing since the
recordId doesn't necessarely has to follow the index. I also tried to
put the data in a labelcontrol and before updating, reading it's value
through the findControl-statement. This works best, but I just want to
know why the e.Item.ItemIndex method doesn't work or how I can use the
DataKeyField properly so I always have the right ID for an update.

Thx
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top