Sending various values from ASP.NET to a javascript function

I am currently attempting to pass multiple parameters (eventually 4 total) into a JavaScript function from my ASP.NET code behind.

In the ASCX file, I have defined the function as follows:

function ToggleReportOptions(filenameText, buttonText) { /*stuff happens here*/ }

And in the ASCX.cs file, I have attempted to set the onClick attributes as such:

string testing123 = "testStringOne";
string testing124 = "testStringTwo";
optReportOptionsRunRealTime.Attributes["onClick"] = "ToggleReportOptions('" + testing123 + ", " + testing124 + "')";
optReportOptionsOffline.Attributes["onClick"] = "ToggleReportOptions('" + testing123 + ", " + testing124 + "')";

However, this implementation does not seem to work correctly. The output shows that the first variable contains "testStringOne, testStringTwo" and the second variable is "undefined".

If anyone could provide assistance in resolving my issue, it would be greatly appreciated. I am relatively inexperienced with JavaScript, more accustomed to .NET development.

Answer №1

Make sure to include the necessary single quotes when passing multiple strings as parameters. If you pass a single string with a comma, it will only be recognized as one parameter. For example, instead of passing 'testStringOne, testStringTwo', you should pass 'testStringOne' and 'testStringTwo' separately.

Here's an improved version:

optReportOptionsRunRealTime.Attributes["onClick"] =
    "ToggleReportOptions('" + testName1 + "', '" + testName2 + "')";
optReportOptionsOffline.Attributes["onClick"] =
    "ToggleReportOptions('" + testName1 + "', '" + testName2 + "')";

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

Struggling with resizing a webcam feed to fit the dimensions of the iframe container?

I'm facing a challenge embedding a camera feed into a webpage that needs to display manual focus and servo controls. The issue lies in the content scaling within the iframe. Even though the iframe boundary resizes according to the window's width ...

Ways to extract a value from an HTML form in ASP.NET when using Html.EnumDropDownListFor

I am a beginner in ASP.NET and I recently set up a form that allows input from a C# enum: <form method="get"> <div class="form-group"> <label>Select Type:</label> @Html.EnumDropDownListFor(x = ...

Encountering a 403 error while attempting to retrieve an image

I'm currently facing difficulties in displaying firebase storage images on my Next.js application due to persistent 403 errors. When I add a document to Firestore, I upload an image to storage with a nested URL and use that URL to retrieve the image. ...

Adding various image files to the canvas

I am facing an issue with my code where I need to find images inserted by the user into a div and then combine them into one image in a canvas when the "snap" button is clicked. While I can successfully locate, position, and resize the images inside the ca ...

Perform a postback within a jQuery UI dialog box

Despite many solutions being available from various Stack Overflow users, none seem to be effective for me. In aspx <asp:Button ID="btnConfirm" ClientIDMode="Static" runat="server" Text="Confirm" OnClick="btnConfirm_Click"/> <script type="text/ ...

What is the best way to run a function after 10 seconds of inactivity using jquery?

Can anyone help me figure out how to run a function every 10 seconds when the system is idle? Here's an example: setInterval(function () { test(); },10000); //for every 10 Sec I really need assistance in getting this function to ...

Ensure that the JavaScript is loaded prior to refreshing the webpage

Below is the HTML code snippet: <script src="https://maps.googleapis.com/maps/api/js?key=mykey&callback=initMap&v=quarterly" defer></script> <script src="{% static '/javascript/maplanguage.js' %}">< ...

Exploring Non Blocking IO through an Example

While browsing through a node.js tutorial, I stumbled upon this informative page that uses the example of a "Restaurant service" to explain a scenario. In the context of Blocking IO, they provide the following code snippet: // requesting drinks for table ...

Creating dynamic form groups that allow for the addition and removal of forms while assigning unique identifiers and names to each input field

I am currently immersed in the development of a sophisticated CMS system. My main objective is to dynamically add and remove form inputs using JavaScript upon clicking a button, with the ultimate goal of submitting the data into a database via PHP script. ...

Is there a way to automatically refresh a webpage whenever there are changes

Currently, I have a webpage that operates like a reverse auction, complete with a javascript countdown timer that tracks the time remaining in the auction. Once the timer reaches zero, the auction ends and the page automatically refreshes. While everythin ...

Transforming the breakpoint can cause fluctuations in the jQuery UI selectable selected event

I'm running into an issue with a jQuery UI selectable div that contains child divs. The problem is that the child divs are not getting selected until I insert a breakpoint inside the selected handler. For a live example, please see my JS Fiddle: htt ...

Vuetify: Utilizing Time Range Inputs for Start and End Time

I am encountering some difficulty in identifying what I may have missed. I am dealing with 2 inputs: time, startTime, and endTime startTime <v-col cols="12" sm="6" md="2"> <v-menu ref="menu" ...

Failure of Ajax to populate dropdowns in Internet Explorer

I've been grappling with some code for the past couple of days and I'm stuck. I really need some help. Here's what I'm trying to achieve: Manually fill the first dropdown menu Retrieve the selected value from the first dropdown, use i ...

Display Vue component depending on specified attribute

I have a block of code that I am working with: <div> <b-card no-body> <b-tabs pills card vertical no-key-nav v-model="step"> <b-tab title="Subject" v-for="animal in animals" :key="animal&q ...

Looking for assistance with transforming JSON data into a three-tiered multidimensional array using JavaScript

Seeking guidance on converting JSON data into a 3-tier multidimensional array using JavaScript. The JSON data I am working with is structured as follows: [ {'City':'Philadelphia','State':'Pennsylvania','Countr ...

Techniques for eliminating text enclosed by double parentheses in javascript

I am currently working on extracting data from Wikipedia, but I am facing a challenge with removing content enclosed in multiple parentheses. While I can successfully remove single parentheses using content.replace(/\s*\(.*?\)\s*/g, &ap ...

Establishing default parameters for angular pipes

Is there a way to establish default settings for an angular pipe without creating a custom one myself? I frequently utilize the currency pipe in this manner {{ price | currency:'EUR':'symbol':'0.2-2':'de' }} I&apo ...

When an onClick event is triggered in jQuery, generate a certain number of div blocks based on the available list items, such as image source and heading text

Is it possible to generate input fields dynamically based on a dynamic list with checkboxes, labels, text, images, etc.? I currently have a working solution for checkboxes and labels using the code snippet below: let $checkboxContent = $('.checkboxes ...

Error: It is not possible to assign a value to the Request property of the Object since it only has a getter method

Encountering issues while attempting to deploy my Typescript Next.js application on Vercel. The build process fails despite functioning correctly and building without errors locally. Uncertain about the root cause of the error or how to resolve it. The f ...

Determining the positioning of a tablet with a JavaScript algorithm

We are currently working on developing an HTML5/JavaScript application specifically designed for tablet devices. Our main goal is to create different screen layouts for landscape and portrait orientations. Initially, we attempted to detect orientation cha ...