473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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
nj********@hotmail.com
http://www.nathansokalski.com/
Sep 13 '07 #1
1 6726
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" <nj********@hotmail.comwrote in message
news:Oj****************@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
nj********@hotmail.com
http://www.nathansokalski.com/

Sep 13 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joel Lyons | last post by:
The help pages make it look like the DataGridItem.DataItem property would return a row from the bound table. Like so: //(during setup) myGrid.DataSource = myDataset.MyTable; myGrid.DataBind();...
0
by: Chris Fink | last post by:
I am having a problem highlighting a row in a datalist during the itemdatabound event. What is occurring is that a new td row is being wrapped around the datalist item which is highlighted (very...
0
by: aboivin | last post by:
Hi, I've seen this posted many times but still can't find any answer. I have a number of ddl in my form and only one of them has the SelectedIndexChanged Event fired correctly even if they all...
1
by: Jack | last post by:
Hello, I have a dropdown list on a user control and the AutoPostBack property is set to True. I want to use the SelectedIndexChanged event to populate some text boxes based on what the user...
6
by: msnews.microsoft.com | last post by:
Hi All, I am creating a control with ASP "ListBox" control. Also I have added necessary code to handle the ListBox.SelectedItemChanged Event. But surprisingly the control is not comng to...
1
by: Art | last post by:
I'm subscribing to the RowCreated event during GridView render process to modify properties of individual rows. All works great when there is no paging involved but once I specify that I'd like to...
11
by: J055 | last post by:
Hi I have a dropdown control which is constructed in another dropdown control SelectedIndexChanged event protected void ddlParamType_SelectedIndexChanged(object sender, EventArgs e) { //...
6
by: tbrown | last post by:
I have a combobox with items like this: {one,two,three}. The selected index is 0, so "one" appears in the combobox text. When the user drops down the list, and selects "two", for example, I...
4
by: aamir | last post by:
Hello, I guess the subject line says it all. I have put Trace.Warn() in the SelectedIndexChanged event handler, but it does not get listed when the trace is on for that page. I have AutoPostback =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.