This is a migrated thread and some comments may be shown as answers.

Get DataKeyValue of clicked item in OnCommand client event

1 Answer 131 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nils C.
Top achievements
Rank 1
Nils C. asked on 09 Aug 2012, 09:38 AM
Hi everyone,
I'm trying to do something I thought was quite simple. I have a RadGrid bound to a webservice (working fine!). For different reasons, I need a custom edit command for the grid. With Serverside-binding this is easily achieved using a simple GridButtonColumn and a custom CommandName. On the server I was used to get the edited row via 
Dim dataItem As GridDataItem = DirectCast(e.Item, GridDataItem)
dataItem.GetDataKeyValue("id")

However, due to the client-side binding to a webservice, that is no longer possible. So I thought I'd get it client-side instead and send it to the server separately (using RadAjaxManager for instance). However, the client-side OnCommand event does not seem to offer any way of getting a dataKeyValue, dataItem or even rowIndex of the gridRow that was clicked. I tried something like this:
function OnCommand(sender, args) {
     if (args.get_commandName() == "Edit1") {
          var CommandArg = args.getDataKeyValue("id");
     }
}

Obviously to no avail...

I guess my question boils down to:
Is there any practical way of getting a reference to the dataItem or even a rowIndex to the server on ItemCommand when the RadGrid is bound to a webservice?

Thanks in advance!

1 Answer, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 10 Aug 2012, 11:46 AM

Hi Hans,

There is actually a way to get the row index of the item clicked. Just trap on the client the click event of the button. Than through dom operations or better jQuery find the first parent row that has its id attribute set (i.e. is not empty). This will be the row of the grid item. The id contains the ID of the table view + double underscore + the row index. Parse the id string and get the row index: id.split("__")[1] should give you the row Index. Now that you have the row index, you can easily get the field values for the grid row. For example:

var fieldValues = $find("RadGrid1").get_masterTableView().get_dataItems()[rowIndex].get_dataItem(); //RadGrid1 is the ID of the grid, rowIndex is the row index found above.

Other than that, with client-side data-binding the getDataKeyValue() method will not work on the client as those data keys are passed from the server and with client-side data-binding you don't have that.

Hope it helps.

Greetings,
Tsvetoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Nils C.
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Share this question
or