Retrieving the DataItem property in DataList's ItemCommand or SelectedIndexChanged event

Discussion in 'ASP .Net' started by Nathan Sokalski, Sep 13, 2007.

  1. I have a custom Control that I have made that contains a DataList. In either
    the ItemCommand or SelectedIndexChanged event I need to retrieve a value
    from the DataItem of the SelectedItem. I have tried the following two
    techniques:

    Private Sub datProductList_ItemCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataListCommandEventArgs) Handles
    datProductList.ItemCommand
    Me.selectedproduct = CInt(e.Item.DataItem("productid"))
    End Sub

    Private Sub datProductList_SelectedIndexChanged(ByVal sender As Object,
    ByVal e As System.EventArgs) Handles datProductList.SelectedIndexChanged
    Me.selectedproduct = CInt(CType(sender,
    DataList).SelectedItem.DataItem("productid"))
    End Sub

    Both attempts returned DataItem as Nothing. How can I get the value I need?
    Am I forgetting something in my customcontrol? Am I supposed to be using
    some other technique? Thanks.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, Sep 13, 2007
    #1
    1. Advertising

  2. Nathan Sokalski

    Teemu Keiski Guest

    Hi,

    DataItem is non-null only in ItemDataBound e.g when dataBind() is called,
    and list is being databound. But if you need the id, you can use DataKeys
    for that. E.g basically set DataKeyField="productid" on the DataList

    Then in ItemCommand

    Me.selectedproduct = CInt( datProductList.DataKeys( e.Item.ItemIndex ) )

    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net


    "Nathan Sokalski" <> wrote in message
    news:Oj%...
    >I have a custom Control that I have made that contains a DataList. In
    >either the ItemCommand or SelectedIndexChanged event I need to retrieve a
    >value from the DataItem of the SelectedItem. I have tried the following two
    >techniques:
    >
    > Private Sub datProductList_ItemCommand(ByVal source As Object, ByVal e As
    > System.Web.UI.WebControls.DataListCommandEventArgs) Handles
    > datProductList.ItemCommand
    > Me.selectedproduct = CInt(e.Item.DataItem("productid"))
    > End Sub
    >
    > Private Sub datProductList_SelectedIndexChanged(ByVal sender As Object,
    > ByVal e As System.EventArgs) Handles datProductList.SelectedIndexChanged
    > Me.selectedproduct = CInt(CType(sender,
    > DataList).SelectedItem.DataItem("productid"))
    > End Sub
    >
    > Both attempts returned DataItem as Nothing. How can I get the value I
    > need? Am I forgetting something in my customcontrol? Am I supposed to be
    > using some other technique? Thanks.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
    Teemu Keiski, Sep 13, 2007
    #2
    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. Eric Newton
    Replies:
    3
    Views:
    9,355
    Brock Allen
    Apr 4, 2005
  2. Nathan Sokalski
    Replies:
    1
    Views:
    379
    Eliyahu Goldin
    May 28, 2007
  3. Nathan Sokalski
    Replies:
    1
    Views:
    989
    Teemu Keiski
    Sep 13, 2007
  4. dataitem in SelectedIndexChanged not recognized

    , Mar 9, 2007, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    1,014
  5. Nathan Sokalski
    Replies:
    1
    Views:
    209
    Teemu Keiski
    Sep 13, 2007
Loading...

Share This Page