ASPXGridView ClientSideEvents How to Get Selected Row’s KeyField Value


I'm trying to get selected grid rows KeyField value on client side;

I used to try followings and get various results:

Method #1

<ClientSideEvents RowClick="function(s, e) {var key= grid.GetSelectedKeysOnPage()[0];}" />
//This gives previous selected rows value everytime

Method #2

<ClientSideEvents RowClick="function(s, e) { grid.GetRowValues(grid.GetFocusedRowIndex(), 'MyKeyFieldName', OnGetRowValues); }" />
//This gives previous selected row and also gives an error: "A primary key field specified via the KeyFieldName property is not found in the underlying data source. Make sure.. blabla" But the MyKeyFieldName is true and i dont want to make a callback, i dont want to use this method!

Method #3

<ClientSideEvents RowClick="function(s, e) { grid.GetRowValues(e.visibleIndex, 'MyKeyFieldName', OnGetRowValues); }">
//This gives the same result with Method #2

The question is: How can i gather KeyField Value of (not previous but) current selected row at client RowClick event without a callback or postback?

Best Solution

Method #2 and #3

Both these methods require a callback to the server.

Make sure that you have specified the ASPxGridView.KeyFieldName property that is required for the row selection operation.

How can i gather KeyField Value of selected row @ client without a callback or postback?

Handle the client-side ASPxClientGridView.SelectionChanged event;

Determine a row that just has been selected via the “e.isSelected” property;

Determine the row’s keyValue via the client-side ASPxClientGridView.GetRowKey method.

Pass the “e.visibleIndex” property as a parameter:

<ClientSideEvents SelectionChanged="function(s, e) {
    if (e.isSelected) {
        var key = s.GetRowKey(e.visibleIndex);
        alert('Last Key = ' + key);
}" />