[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.buildingcontrols

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

Nathan Sokalski

9/13/2007 4:31:00 PM

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
njsokalski@hotmail.com
http://www.nathansok...


1 Answer

Teemu Keiski

9/13/2007 4:45:00 PM

0

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....
http://teemu...


"Nathan Sokalski" <njsokalski@hotmail.com> wrote in message
news:Oj%23iIOi9HHA.4712@TK2MSFTNGP04.phx.gbl...
>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
> njsokalski@hotmail.com
> http://www.nathansok...
>