e.Item.ItemIndex returns wrong value

Discussion in 'ASP .Net' started by Stephan, Jul 26, 2004.

  1. Stephan

    Stephan Guest

    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
     
    Stephan, Jul 26, 2004
    #1
    1. Advertising

  2. Stephan

    chanmmn Guest

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

    chanmm

    "Stephan" <> wrote in message
    news:...
    > 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
     
    chanmmn, Jul 26, 2004
    #2
    1. Advertising

  3. 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?



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Stephan Bussing, Jul 26, 2004
    #3
  4. 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


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Stephan Bussing, Jul 26, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred Dag
    Replies:
    1
    Views:
    2,029
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=
    Jan 20, 2006
  2. Andy B
    Replies:
    1
    Views:
    994
    bruce barker
    Nov 15, 2008
  3. Joel Finkel

    DataGrid1.DataKeys[e.Item.ItemIndex] array index out of bounds

    Joel Finkel, Sep 4, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    324
    Joel Finkel
    Sep 4, 2003
  4. Scott M.

    e.Item.ItemIndex and e.Item.DataSetIndex

    Scott M., Nov 1, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    7
    Views:
    263
    Steven Cheng[MSFT]
    Apr 13, 2005
  5. RG

    ASP.NET 2.0 equivalent for e.Item.ItemIndex ?

    RG, Mar 27, 2006, in forum: ASP .Net Web Controls
    Replies:
    5
    Views:
    362
Loading...

Share This Page