Solved

Finding datakey in datalist during item_databound event

Posted on 2008-11-02
6
4,074 Views
Last Modified: 2012-08-13
Hi,

I'm trying to get hold of the datakey value of each row in a datalist control. This is the code I'm using:

    Protected Sub dtSmartMenu_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dtSmartMenu.ItemDataBound
        Dim HyperLink1 As HyperLink = e.Item.FindControl("HyperLink1")
        Dim smartMenuID As Integer = dtSmartMenu.DataKeys(e.Item.ItemIndex)("smartMenuID")
   
           End Sub

---------------------------------------------
That doesn't work though. Can anyone help me to get hold of that value?!

Thanks for help!

Peter
0
Comment
Question by:peternordberg
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:Salim Fayad
Comment Utility
You have to get its "Value"

dtSmartMenu.DataKeys(e.Item.ItemIndex).Value

Open in new window

0
 

Author Comment

by:peternordberg
Comment Utility
Hi, and thanks for answer!

How can I get that value to be assigned to an integer variable? It complains that it can't find the public member for the type Value for integer.

Peter
0
 
LVL 11

Expert Comment

by:Salim Fayad
Comment Utility
Here is how you convert it to int

CInt(dtSmartMenu.DataKeys(e.Item.ItemIndex).Value)

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:peternordberg
Comment Utility
Hi and thatnks for your help!
Yes, I've already tried that, still I get an error that says>
--------------------------
"Missing Member exeption unhandled by user code"
"Couldn't find the public member Vakue for the type Integer."
---------------------------------
This is the code I'm using

Dim smartMenuID As Integer = CInt(dtSmartMenu.DataKeys(e.Item.ItemIndex).Value)
Thanks for help!
 
Peter
0
 
LVL 22

Accepted Solution

by:
prairiedog earned 500 total points
Comment Utility
Hi peternordberg:
1. DataList is diffrent from GridView as far as DataKeys is concerned. Here is the right syntax for retrieving DataKey value from DataList:
Dim smartMenuID As Integer = CInt(dtSmartMenu.DataKeys.Item.(e.Item.ItemIndex).ToString())   'although without CInt the above will still work because VB.NET will do the conversion for you, it is always a good idea to use explicilty conversion.
2. Header row and Footer row will also considered "Item", and the above code will throw exception error when it is a header row or a footer row. So you need to wrap your code with a row type check like this:

If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
   Dim HyperLink1 As HyperLink = e.Item.FindControl("HyperLink1")
   Dim smartMenuID As Integer = Me.dtSmartMenu.DataKeys.Item(e.Item.ItemIndex)
End If
 
0
 

Author Closing Comment

by:peternordberg
Comment Utility
Thanks! That worked perfectly!

Peter
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now