This one is driving me mad....
I have several subgrids in a form, all of the same entity and filtered to different fields. A button in the grid's ribbon calls a function that needs to know the exact grid "instance" to make a decision - and this is where I am completely lost.
In my ribbon definition, I pass the CrmParameter SelectedControl to the called function. This is supposed to be the active grid - and it apparently is, in a way at least.
What I would like to do in my JavaScript code is get one of my grids via its name and then compare it to the object that's passed into my function - only that this is some completely different object, and you can do next to nothing with it. You can't get its name, ID, label whatever; of all the methods listed for controls in the SDK, only four work: getVisible(), setVisible(), setFocus() and refresh(). These are not very useful for what I need to do.
A colleague then told me to try Xrm.Page.ui.getCurrentControl() - but that shows the exact same behavior, although funnily the two objects aren't even equal.
I found something vague through Google that used the .control property of a control retrieved via .getControl(), so I compared the one obtained via .getCurrentControl() to that - and they matched. Unfortunately, the "current control" matches the .control properties of all subgrids.
Some code to make it clearer what is what:
function ribbonAction(param) // param is the SelectedControl parameter
{
var current = Xrm.Page.ui.getCurrentControl();
var grid1 = Xrm.Page.getControl("grid1");
var grid2 = Xrm.Page.getControl("grid2");
alert(param == current); // false
alert(param == grid1); // false
alert(param == grid1.control); // false
alert(current == grid1); // false
alert(current == grid1.control); // true
alert(current == grid2.control); // true
alert(current.getName()); // throws an error
alert(param.getName()); // throws an error
current.refresh(); // refreshes the correct grid
param.refresh(); // refreshes the correct grid
}
The comments tell what happens when the ribbon button is clicked while grid1 is active.
I would be very grateful for any hints on how to really identify the active subgrid control in that situation.