Finding datakey in datalist during item_databound event

Posted on 2008-11-02
Medium Priority
Last Modified: 2012-08-13

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!

Question by:peternordberg
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 11

Expert Comment

by:Salim Fayad
ID: 22862969
You have to get its "Value"


Open in new window


Author Comment

ID: 22863753
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.

LVL 11

Expert Comment

by:Salim Fayad
ID: 22867446
Here is how you convert it to int


Open in new window


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 22868102
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!
LVL 22

Accepted Solution

prairiedog earned 2000 total points
ID: 22868317
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

Author Closing Comment

ID: 31512524
Thanks! That worked perfectly!


Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 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