Exception thrown by ASP.Net Javascript code persists despite being commented out!

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;
    } 

Answer №1

Incorrect quotation marks have caused nesting issues in the following code snippet:

"<%# fileGridGrouping.GetRowValues(fileGridGrouping.FocusedRowIndex, "ID").ToString() %>";
//                                   Pay attention to syntax highlighting ^

To fix this issue, replace the problematic line with:

'<%# fileGridGrouping.GetRowValues(fileGridGrouping.FocusedRowIndex, "ID").ToString() %>';

(You'll notice the use of single quotes (') in the corrected line)

The error persists even when the line is commented out because any code within <%# %> blocks gets executed on the server side, disregarding JavaScript comments.

Answer №2

It seems that the issue lies in the presence of Null values:

fileGridGrouping.GetRowValues(fileGridGrouping.FocusedRowIndex, "ID") //returns Null
(Null).ToString() //throws an Exception

You may want to consider using this line instead:

'<%# (fileGridGrouping.GetRowValues(fileGridGrouping.FocusedRowIndex, "ID") ?? "").ToString() %>';

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on how to create a promise with entity type in Nest Js

I am currently working on a function that is designed to return a promise with a specific data type. The entity I am dealing with is named Groups and my goal is to return an array of Groups Groups[]. Below is the function I have been working on: async filt ...

Unusual CSS hierarchy observed post AJAX content load

Currently, I am facing a puzzling issue where my CSS rules seem to be losing precedence on a page loaded via AJAX. Despite placing my custom CSS file last in the main page, allowing it to take precedence over any bootstrap styles, after loading new content ...

Looking for guidance on implementing explicit waits in Protractor for non-angular applications

I have noticed that automating non-angular applications with Protractor can be challenging. Currently, I am using some methods to add an explicit wait to my existing Serenity click and enter functions. However, I am curious if there is a way to automatic ...

I'm encountering problems when attempting to display the image/png response from an xmlHTTPRequest. Instead of the correct data, I

I have implemented the following code to integrate a captcha generating web service. The response data is successfully obtained, but when I attempt to display the result within a div element, the image appears as distorted text. var xmlHttp = new XMLHtt ...

Looking to replace a background image using javascript?

(apologies for any language mistakes) I have multiple divs with a common background image. I assigned them the same class and set the background image using CSS in style.css which worked perfectly fine. Now, I want to change the background image dynamical ...

Arranging CouchDB/Couchbase view based on the quantity of keys

Looking to create a view that displays the top 10 tags used in my system. While it's simple to get the count using _count in the reduce function, the list is not sorted by the numbers. Is there a way to accomplish this? function(doc, meta) { if(doc ...

What is the best way to deactivate select option(s) based on the JSON data retrieved through AJAX?

I am facing an issue with a dropdown menu that contains several options. My goal is to loop through these options and set the disabled attribute on specific ones based on the result of an Ajax call. https://i.sstatic.net/9k3f9.png This is what I am tryin ...

Concealing and revealing an element using the CSS property visibility:hidden/visible

There are two div-boxes that should show/hide when clicking on another two div-boxes. I want the divs to maintain their space so as not to disrupt the DOM, ruling out the use of .toggle(). I attempted this approach without success: $('#red, #pink&ap ...

What is the best way to manually decrease the speed of an object's rotation using javascript?

Can anyone assist me with slowing down the mouse-controlled rotation of a 3D object in javascript? The current rotation is too sensitive and difficult to control manually. Below is the code I am using: <html> <head> <script src="js/thr ...

Frequent running of jQuery scripts

In my jQuery ajax code, I created a FitnessPlanDay: // Add Day ajax $('#addDay').on("click", function() { $.ajax({ url: '@Url.Action("AddDay")', type: 'POST', ...

What could be causing the state to not update as anticipated?

I am currently in the process of developing a TicTacToe game and have a requirement to maintain the current player in state under the name currentPlayer. The idea is that after one player makes a move, I need to update currentPlayer to represent the opposi ...

Using ASP.NET MVC with JQuery to find the closest HiddenFor value

My current table structure is as follows: <table class="table table-bordered"> @for (var i = 0; i < Model.Count; i++) { <tr> <td width="25px"> @if (!Model[i].Letter.Equ ...

The internal style and script specified within the <head> section are not being rendered

Within my Joomla website using the T3 template, I inserted the following "Custom Code" just before the closing </head> tag: <style type="text/stylesheet"> div.t3-sidebar.t3-sidebar-right{ background: #F8F8F8 none repeat scroll 0% 0%; ...

Combining the .net FIFO "Queue" with the latest known Topic

Currently searching for a FIFO Queue that can also handle overrides based on specific topics, as outlined below: struct QueueItem { string Topic; .... other data } If items are added in the following order: q.Add( new QueueItem() { topic = A, ... } ); ...

Setting the focus on an input when updating a property in the Pinia store using Vue

When a component I have is clicked, it triggers a function in the store: <button @click="this.store.foo()"></button> Within the store, I am updating a specific property: state: () => ({ focusIn: false, }), actions: { foo() { ...

Tips for displaying JSON data by formatting it into separate div elements for the result object

Having recently started using the Amadeus REST API, I've been making AJAX calls to retrieve data. However, I'm facing a challenge when it comes to representing this data in a specific format and looping through it effectively. function showdataf ...

Issue with React Google Maps Api: Error occurs while trying to access undefined properties for reading 'emit'

I'm trying to integrate a map using the google-map-react API, but I keep encountering the following error: google_map.js:428 Uncaught TypeError: Cannot read properties of undefined (reading 'emit') at o.r.componentDidUpdate (google_map.js: ...

What is the method for acquiring a dynamic segment in the router of a Next.js 13 application?

Currently in my project, I am using the new App Router in Next.js 13 and MongoDB as the DBMS to fetch data via API. When trying to retrieve all data from a collection, it is successful. However, fetching only one data results in failure. The error message ...

Ways to determine if a device possesses a specific capability

I am currently in the process of developing a web application and am exploring how to determine the capabilities of an Android device. Is there a central registry or database where I can look up the various capabilities and features of an Android device? ...

I'm having trouble managing state properly in Safari because of issues with the useState hook

Encountering Safari compatibility issues when updating a component's state. Though aware of Safari's stricter mode compared to Chrome, the bug persists. The problem arises with the inputs: https://i.sstatic.net/WSOJr.png Whenever an option is ...