ASPXGridView ClientSideEvents How to Get Selected Row's KeyField Value
Asked Answered
L

3

16

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?

Lattimore answered 15/1, 2012 at 3:7 Comment(0)
S
21

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);
    }
}" />
Saylor answered 15/1, 2012 at 9:10 Comment(6)
Thank you, but this is not the answer that i'm expecting. I'm asking how can i gather newly selected row's Keyfield value at Clientside RowClick event? And ASPxClientGridView.GetSelectedKeysOnPage[0] is changing when the selection change, its not same.Lattimore
FYI if you want to use Mikhail's solution; the problem with using the SelectionChanged event: when user sort or filter; the stored key is not updated (SelectionChanged event is not triggered)Muddle
oops sorry; I mean the visibleIndex will be changed after sorting, etc so when you have to call ASPxClientGridView.GetRowValues with the cached visibleIndex, your will get the wrong rowMuddle
This is correct, because the total number of rows is changed.Saylor
Its been a while I omited the problem somehow, and as "kite" mentioned, using Mikhail solution is correct but untrustable for sorting, filtering conditions. I find the obvious solution via quitting usage of DevEx controls.. They're fine but very poor for customizing. Such as, Grid control hasn't "RowDataBound" event. DevEx developers advising some other events for specific requirements but their advices aren't fit for all situations. RowDataBound is an event that is what, not any other event. And so on. I changed the route to MVC+jQuery now and this is a brilliant world to developing web.Lattimore
@Lattimore What MVC are you using? There are MVC controls out there.Cinerary
M
1

How to in 3 easy steps.

In my case, I want to get contents of a field ('ID') from ASPxGridView when the user clicks on the row...

  1. Create the ClientSideEvent for row click and put "RowClick(s, e);" in the function.
  2. Create the actual function the event will call as shown below - and here is the tricky part; do not use GetFocusedRowIndex() to get the index because it is the FOCUSED index. Use e.visibleIndex

    function RowClick(s, e) {
        // Do callback to get the row data for 'ID' using current row.
        MyAspxGridView.GetRowValues(e.visibleIndex, 'ID', OnGetRowId);
    }
    
  3. Create your call back to get the field you want. I'm getting 'ID'.

    function OnGetRowId(idValue) {
        alert('ID: ' + idValue.toString());
    }
    
Mcmorris answered 18/12, 2014 at 18:16 Comment(1)
There is a slight delay here though. How can you work around the delay?Spillman
P
0
function OnbtnOkClick(s, e) {
    grid.GetRowValues(grid.GetFocusedRowIndex(), 'FieldName1;FieldName2', OnGetRowValues);
}

function OnGetRowValues(values) {
    var fName1 = values[0];
    var fName2 = values[1];
    txt1.SetText(fName1);
}
Postremogeniture answered 23/10, 2013 at 7:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.