How to get the datakeynames value in the rowupdating event

I suppose my eyes got big when I saw all the new properties and methods, and wanted to try them all out. Most articles I found were for hardcoded datasources (especially on MSDN, ugh) and didn’t really help me much, so this post will be referring to Grid Views with dynamically bound datasources.

First, create a Grid View with three bound columns and an Update/Edit/Cancel (Command Field) column: Nothing in there should be shocking to anyone: all your events are handled, you have a Hyper Link Field that uses the Data Navigate URLFields property to insert a Product ID, and you have set your Data Key Names property to your hidden field which holds your Product ID.

Finally, to pull your hidden value that you’ve dubbed a Data Key in the Grid View’s subroutine.

Remember that my first column was a Hyper Link Field, so in order to pull that value, I must cast that column as a Hyper Link.

Open Visual Studio 2008 and create a new Web Application.

This example won’t hook into a database, but we will create a class to mock a table and retrieve data from it instead. This enables you to not display the ID to the user, yet still being able to retrieve it.

This seems to work, but I don't like the cast to Link Button (what if some other command is firing the event? I'm also aware that it's possible to use a hidden field for the ID, but I consider that a hack that I don't want to use.

To String but ddl Conf is always = "S" - the first item in dropdownlist Where am I wrong? I think you should add the Is Post Back property in Page_Load event if you bind your Grid Vew in Page_Load Event. Edit Index = -1 'set to no selection Bind Grid View() End Sub One issue I ran into while attempting to figure this all out was if I didn’t rebind my Grid View at the end of each event, I was required to click twice to get it to select the correct row, and if I clicked around, it would always be a row behind. This blog is for nothing else than to try and help me help you and help you help me and us help everyone else try to fix anything and everything we can’t figure out with this language. Hope this saves someone a full day of research (and possibly some hair)! To enhance this process, be sure to include your Grid View in an AJAX Update Panel. Everything should be sorted out when the “Update” link is pressed.

Leave a Reply