In my ASP website, I have incorporated Javascript code in the HEAD section to create a custom context menu. Here is the logic that I initially used:
function fileGridGrouping_ContextMenu(s, e) {
if(e.objectType != "row") return;
fileGridGrouping.SetFocusedRowIndex(e.index);
lastFileId = "<%# fileGridGrouping.GetRowValues(fileGridGrouping.FocusedRowIndex, "ID").ToString() %>";
}
While trying to fetch the ID of the selected row using some C# code in the background, I faced an exception due to FocusedRowIndex not being initialized. To troubleshoot this, I decided to modify the code to always get the first row:
function fileGridGrouping_ContextMenu(s, e) {
if(e.objectType != "row") return;
fileGridGrouping.SetFocusedRowIndex(e.index);
lastFileId = "<%# fileGridGrouping.GetRowValues(0, "ID").ToString() %>";
}
Despite changing the code, I still encountered the same exception. Further adjustments led me to check for initialization of FocusedRowIndex:
function fileGridGrouping_ContextMenu(s, e) {
if(e.objectType != "row") return;
fileGridGrouping.SetFocusedRowIndex(e.index);
if ("<%# fileGridGrouping.FocusedRowIndex %>" <= 0) return;
lastFileId = "<%# fileGridGrouping.GetRowValues(0, "ID").ToString() %>";
}
To my surprise, the issue persisted even after commenting out the problematic line. It seemed like the commented code was being executed during page load before any user interaction.
This unexpected behavior left me puzzled and unsure about how to resolve it. However, after revisiting the code structure, I managed to rectify the problem by optimizing the functions used:
function fileGridGrouping_ContextMenu(s, e) {
if(e.objectType != "row") return;
fileGridGrouping.SetFocusedRowIndex(e.index);
fileGridGrouping.GetRowValues(fileGridGrouping.GetFocusedRowIndex(), "ID", OnGetRowValues);
}
function OnGetRowValues(value) {
lastFileId = value;
}