Having trouble sending eval to JavaScript function

I have the following image button inside a gridview:

<asp:TemplateField HeaderText="Edit" ControlStyle-CssClass="smallTxt"
                                HeaderStyle-CssClass="smallTxt">
                                <ItemTemplate>

                                <asp:ImageButton runat="server" ImageUrl="../images/yellow_arrow.gif" ID="imgbtnsearch"
                     OnClientClick="javascript:setCustID('<% Eval(idCustomer)%>');return false;" />


                                </ItemTemplate>
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:TemplateField>

Using this image button triggers a JavaScript function. The function is as follows:

 function setCustID(cid) {
        alert(cid);
        ShowHideControl('ctl04_divEdit');
        document.getElementById('ctl04_hdnIdCustomer').value = cid;


    }

However, when I run this code, instead of receiving the result of Eval(idCustomer) as a number in the alert box, I get:

<% Eval(idCustomer).toString()%>

When I attempt to pass the parameter using <%#Eval(idCustomer)%>

OnClientClick="javascript:setCustID('<%#Eval(idCustomer)%>');return false;" 

The function alerts:

<%#Eval(idCustomer)%>

Answer №1

The correct move involved using #. To avoid confusion with quotation marks (as I did previously), it is important to utilize string.Format when dealing with JavaScript expressions:

OnClientClicking='<%# string.Format("javascript:setCustID({0});return false;", Eval("idCustomer")) %>'

Additionally, remember to enclose function parameters in single quotes if they are strings. However, if the parameter is an integer, it should remain unchanged.

Answer №2

Apologies, I am not very knowledgeable in ASP, but regarding Eval: It is recommended to use Eval("string") instead of passing it as String: "Eval(string)".

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

Mastering the Art of Managing AJAX Requests with EXTJS

I have a code to send an AJAX request that appears to be functioning correctly under firebug, sending the correct parameters: Ext.Ajax.request({ url: 'test', params:{name: "bunya"}, success: function(resp){ ...

Not every item in the array has been allocated to the model

I am encountering an issue with loading all the elements of an array from my ExpressJS backend to my frontend framework OpenUI5. Despite having an array of 520 elements, only the first 100 elements are being loaded into the model. How can I ensure that all ...

The browser is preventing files from being accessed through Express because they do not have the text/html MIME type

Currently, I am attempting to set up a nodejs express web server with a static frontend. In order to handle the GET requests made to /, I have implemented myServer.use(express.static("public"));. Within the public folder are HTML, JavaScript, CSS, and im ...

What could be causing my AdRotator to not show images even though the image paths are accurate?

I am currently developing a straightforward web application using C# and have reached the stage where I need to incorporate an AdRotator object and link four images to it. Despite my efforts, the images refuse to display; only the alternate text is visible ...

What is the disposal process for asp.net controls?

I'm currently investigating memory leaks within an asp.net website. One issue I encountered was with the code not releasing event handlers when controls were no longer in use. To address this, I followed the disposing pattern outlined on MSDN to prope ...

What is the best way to generate a live map with constantly updating markers?

Is it possible for me to learn how to develop a live map similar to the one on this site: www.lightningmaps.org? It's fascinating to watch new markers pop up every few seconds. I'm interested in building a real-time map that can track IP locatio ...

The concept of Selenium PageObjects: breaking down web components into reusable modules

When implementing the PageObjects pattern to components of a page, what is considered the standard approach? For example, let's say we are writing tests for the various features on an Amazon product page. This page contains multiple separate feature ...

Ajax request triggers a page refresh

As I review the AJAX call within a React method, there are some key observations: handleActivitySubmit: function handleActivitySubmit(activity, urlActivity, callbackMy) { console.log("querying with activity: " + JSON.stringify(activity)); $.ajax ...

Identifying a specific item within an array

Can someone guide me on how to manipulate a specific object within an array of objects? For example: var myArray = [ {id: 1}, {id: 2}, {id: 3}, {id: 4} ] // Initial state with the 'number' set as an array state = { number: [] } // A functio ...

The Epub text box feature is malfunctioning

I have a quiz task in an epub format where users need to enter their answers in a text box after reading the question. However, I am facing an issue where the text box does not display the keyboard for typing the answer. Is there a solution using javascr ...

How to activate select only when specific options are chosen using jQuery

I am working on a form that has 1 select element disabled, with 4 options. I am looking to implement a jQuery function that will perform the following actions: If OPTION #1 is clicked, it should check if any other options are selected and reset them (eras ...

Testing with karma/jasmine in AngularJS can lead to issues when there are conflicts between test

Currently experiencing challenges while running my midway tests (or integration tests, which are halfway between unit tests and E2E tests). Working with an AngularJS build featuring RequireJS, I am utilizing the RequireJS plugin in Karma to run the tests. ...

Webpack encounters an error while attempting to load Bootstrap v4.0.0-beta

I've encountered an issue with my webpack configuration and Bootstrap v4.0.0-alpha.6 that was working fine until I attempted to switch to v4 beta. Unfortunately, I can't seem to get it working properly now :( Here is a snippet of my config: w ...

Unexpected behavior: jQuery AJAX request isn't triggering the alert function as expected

I can't figure out why this code isn't working: $.get("/some/url/", function(event) { alert('hey'); }); When I click the button, I can see the response being executed in the Firefox console, and it shows a successful status (200 O ...

The use of a Bootstrap row is leading to incorrect dimensions for FullPageJS

Within the body tag, I have included the following code snippet: <div id="container"> <div class="section profile"> <div class="row"> <div class="col-sm-6"> A </div> ...

Exploring Angular's Observables for handling HTTP requests

Exploring Angular Observable Documentation As I delved into the Angular observable documentation mentioned above, I came across a section that compares promises with observables. Observables provide many values, while promises provide one. This characte ...

Encountering an issue with the v-carousel component from Vuetify: receiving a 'Cannot read property 't' of undefined' error message

Currently, I am trying to create an image carousel using Laravel along with Vue/Vuetify. The issue lies in the fact that the Vuetify v-carousel and v-carousel-item components are not rendering properly due to the error mentioned in the title. I have alrea ...

Ways to retrieve multiple outcomes in NHibernate with C#

I am working on a stored procedure that returns multiple result sets in NHibernate using C#, the problem is that I have three result sets of different types - lure, bait, and fly. I am unsure how to handle this in my code. Any suggestions on how to appro ...

Calculation of time intervals based on input values from text boxes, calculating quarters of an hour

I am facing a couple of challenges: -I am trying to calculate the time duration in hours between two military times input by the user in two textboxes. The result should be in quarter-hour intervals like 2.25 hours, 2.75 hours, etc. -The current calculat ...

Ways to set the className prop for all components automatically without having to specify it repeatedly

One challenge I face is dealing with code duplication whenever I create a new component. Is there a way to pass the className property between components without having to explicitly define it every time a new component is created? For example, when I cr ...