Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Retrieving the DataItem property in DataList's ItemCommand or SelectedIndexChanged event

Reply
Thread Tools

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

 
 
Nathan Sokalski
Guest
Posts: n/a
 
      09-13-2007
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/


 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      09-13-2007
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/
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieving the DataItem property in DataList's ItemCommand or SelectedIndexChanged event Nathan Sokalski ASP .Net Building Controls 1 09-13-2007 04:45 PM
Retrieving the DataItem property in DataList's ItemCommand or SelectedIndexChanged event Nathan Sokalski ASP .Net 1 09-13-2007 04:45 PM
Accessing the current DataItem in the ItemCommand event Nathan Sokalski ASP .Net 1 05-28-2007 08:20 PM
dataitem in SelectedIndexChanged not recognized aamirghanchi@gmail.com ASP .Net Datagrid Control 1 03-09-2007 10:38 PM
DataBinder.Eval for an object's property property... like Eval(Container.DataItem,"Version.Major") Eric Newton ASP .Net 3 04-04-2005 10:11 PM



Advertisments
 



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