Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Building Controls (http://www.velocityreviews.com/forums/f59-asp-net-building-controls.html)
-   -   Retrieving the DataItem property in DataList's ItemCommand or SelectedIndexChanged event (http://www.velocityreviews.com/forums/t758885-retrieving-the-dataitem-property-in-datalists-itemcommand-or-selectedindexchanged-event.html)

Nathan Sokalski 09-13-2007 04:31 PM

Retrieving the DataItem property in DataList's ItemCommand or SelectedIndexChanged event
 
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.nathansokalski.com/



Teemu Keiski 09-13-2007 04:45 PM

Re: Retrieving the DataItem property in DataList's ItemCommand or SelectedIndexChanged event
 
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" <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.nathansokalski.com/
>





All times are GMT. The time now is 02:39 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57