What is the best way to incorporate an asp helper within a javascript script?

I am working on a JavaScript page layout and trying to incorporate CMS helpers into it. Despite successful rendering, the helpers are not functioning as expected (resulting in "NaN" instead of the desired text). How can I integrate helpers into a JavaScript variable?

var detailsTemplate =
                    '<table cellspacing="0" cellpadding="0">' +
                         '<tr>' +
                               '<th class="info">' +
                                <%# CMS.GlobalHelper.ResHelper.GetString("ReceiptsList.ProductName") %> +
                                '</th>' +
                         '</tr>' +
                            '<tbody>' +
                                '{0}' +
                            '</tbody>' + 
                    '</table>' 

Answer №1

Enclose the ASP tags in single quotation marks:

'<th class="info">' +
  '<%# CMS.GlobalHelper.ResHelper.GetString("ReceiptsList.ProductName") %>' +
'</th>'

Once you examine the generated code, you will notice that without the quotes it will look like

'<th>' + foo + '</th>'
, leading to an error. However, with the quotation marks, it will appear as
'<th>' + 'foo' + '</th>'
and execute correctly.

Answer №2

Success! I managed to solve it by simply including the following:

<span runat="server"> <js code..> </span>

In addition, I updated the helper to (without using single quotes):

<%= CMS.GlobalHelper.ResHelper.GetString("ReceiptsList.ProductName") %>

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

EMFILE error encountered while attempting to initialize a new react-native project and watch file changes

Looking to start a new react-native project? Here are the steps: Begin with react-native init testproject then run react-native run-ios Encountering an error while watching files for changes: EMFILE {"code":"EMFILE","errno":"EMFILE","syscall":"Error watc ...

Incorporating User Verification into MVC Environment

I've been working on my first MVC 5 project, a simple Lending System with plenty of CRUD operations. Everything is almost complete, except for user authentication which I haven't implemented yet. My project consists of 4 layers: 1. App.Web - ...

Vibrant progress bar design with CSS

Could someone assist me in coding a multicolor progress bar? I would like it to display red when the progress is less than 50, green when the progress is between 50 and 90, and blue when the progress is between 90 and 100. How can I achieve this? ...

Creating an array of a specific field within a JSON string using the collect() method of objx is a straightforward process

Here is the JSON string I am working with: var data = [{"first":"3","second":"1"},{"first":"5","second":"5"},{"first":"7","second":"1"}]; Currently, I am using the following code - objx(data).collect("first") I am expecting to receive an array like [3 ...

Is there a way to set up gulp without relying on npm when using shared hosting?

While working on my shared hosting, I encountered a roadblock regarding the installation of gulp for compiling LESS assets into CSS. The hosting administrator has declined to install npm, which is essential for setting up gulp. Given this limitation, I am ...

The importance of incorporating React into the scope of functional component development

While discussing class components, it's clear that they are part of the global React object. But why is it necessary to import them with every functional component? And do bundlers play a role in this requirement? I've been coding for 5 months n ...

Incorporating a protected Grafana dashboard into a web application

I am looking to incorporate Grafana into my web application using AngularJS. The main objective is to allow users to access the Grafana UI by clicking on a button within my application. Setting up an apache reverse proxy for Grafana and ensuring proper COR ...

A guide on transferring JSON information via a POST request with C#

Looking to send JSON data in a POST request using C#? I've experimented with a few methods but encountered numerous challenges. I'm trying to make the request with a raw JSON string as well as JSON data from a separate file. How would one go ab ...

JavaScript code that involves manipulating dates within a nested loop

I'm currently developing a booking system where the pricing varies based on seasons that recur annually. The overall functionality is working smoothly, however, I am encountering difficulties with handling the recurring seasons. I have implemented mom ...

Protractor sendKeys method on Modal dialog is failing due to element visibility issues

I seem to be facing a strange issue with protractor. My challenge lies in testing a form that is situated within a modal. Although I am able to verify that the modal is indeed open, I encounter difficulties when attempting to sendKeys to the input fields. ...

Dealing with a send parameter button in ASP.NET MVC using C#: Tips and tricks for success

What is the best way to manage a submit button parameter in asp.net MVC using C#? <form id="form" name="form" action="" method="post"> <label>Name : </label> <input type="text" ...

The file size exceeds the server's upload limit, despite making changes to the php.ini file

I've encountered a problem trying to upload an .OBJ file to the server, resulting in an 'Error 1' message. The uploaded file surpasses the upload_max_filesize directive specified in php.ini This error is detailed on this page - http://ph ...

Initiating Calls from JavaScript to JavaFX

I'm facing an issue while trying to execute a JavaScript function called testCheckMate from Java. The error message I receive is: Exception in thread "JavaFX Application Thread" netscape.javascript.JSException: SyntaxError: Unexpected EOF The WebVie ...

The code snippets in the Vue3 documentation are quite peculiar

As I peruse the Vue 3 documentation, I notice a recurring pattern in how example code is presented for components: Vue.createApp({}) However, my experience with Vue 3 has been different. Instead of the above syntax, I simply use: <script> export d ...

Yep, implementing conditional logic with the `when` keyword and radio buttons

I seem to be encountering an issue with my implementation (probably something trivial). I am utilizing React Hook Form along with Yup and attempting to establish a condition based on the selection of a radio group. The scenario is as follows: if the first ...

Display recommendations as soon as a specific character is entered in the text box

Issue Description: I am looking to implement a specific feature using Javascript or JQuery. When a user types a designated character, such as the @ symbol, in a text box, I would like the values of an array to be displayed in a drop-down menu. For example, ...

Constructing items within an array literal using a constructor method

I am attempting to create an object using my Person constructor. However, I am encountering issues when I try to initialize the object directly in the array using literal notation. function Person(name, age) { this.name = name; this.age = age; } ...

Issue with ngModel value not being accurately represented by checkbox state in Angular 2

My issue lies with a checkbox that does not reflect its ngModel value. To provide some context, I have a Service managing a list of products and a component responsible for displaying this list and allowing users to select or deselect products. If a user d ...

Utilizing a CSS/HTML div grid to mirror a 2D array in JavaScript

Currently, I am working on a personal project that involves creating a grid of divs in HTML corresponding to a 2D array in JavaScript. However, I am uncertain about the most effective way to accomplish this task. Specifically, what I aim to achieve is tha ...

Strategies for handling asynchronous requests and effectively presenting the retrieved data

On my HTML page, I have a badge component that shows the number of unread messages. Here is the HTML code: <button class="font" mat-menu-item routerLink="/message"> <mat-icon>notifications</mat-icon> <span [matBadgeHidden]="newM ...