Can a JavaScript function be handed to a constructor for an event handler?

The JavaScript function provided:

function ShowFax() {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "WebServices/SendFaxService.asmx/SaveFaxData",
        data: "{}",
        dataType: "json",
        success: function (data) {
            $('#<%=cSendFax.FindControl("dDIVFax").ClientID %>').show();
        },
        error: function (result) {
            alert("Error");
        }
    });
}

Should be included as a parameter in the event handler constructor below:

Navigation.PHQ2_SendFax += new Navigation.CommunicationsEventHandler();

Answer №1

One way to easily incorporate a function as an argument is by referencing the function's name directly:

EventManager.notify += new EventManager.EventHandler(DisplayNotification);

Alternatively, you can assign the function to a variable and then call it using that variable:

EventHandler = function(action)
{
    action();
}

Answer №2

My unconventional solution to the problem was to create a method in the code behind that calls a JavaScript function, which I then used as an argument.

Here is the code for the method in the code behind:

protected void ShowFax(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "ShowFax();", true);
}

And here is how I set up the Event Handler constructor:

Navigation.PHQ2_SendFax += new Navigation.CommunicationsEventHandler(ShowFax);

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

Nuget: Easily Deploy to IIS or Collaborate on Code Sharing

I am new to using NuGet and I have a question that seems so basic, I can't seem to find the answer. Question 1) I recently downloaded NuGet packages on VS2012: -Install-Package EntityFramework -Version 6.1.0 -Install-Package Microsoft.AspNet.Web.Op ...

Overabundance of Recursive Calls in Node.js Project Dependencies

After a tiring day at work, I noticed an alert for Windows SkyDrive showing that files couldn't be uploaded due to the path being too long. The lengthy directory structure made me chuckle at the technological limitation. However, it got me thinking: ...

Learn how to create an animated refreshing icon in React when clicked

I have a refresh icon in my React app that utilizes Material UI. I want the icon to spin for a second after it is clicked, but I am unsure of how to achieve this. Despite attempting some solutions, none have been successful. const useStyles = makeStyles(( ...

Tips for implementing daterangepicker js in an Angular 2 project

I'm currently working on an Angular 2 project and I'm looking to integrate the daterangepicker.js library for a date range picker. If you're not familiar with it, you can find more information about the library here. Here's the HTML co ...

What is the most effective way to eliminate shared variables in outdated code?

Within the system, there exists a singleton that represents the currently logged-in user (our application's user, not the Windows user) as a shared instance. In numerous data access class files, this singleton is utilized to assign CreateByID and Edi ...

The alignment of inline divs is off and they are not forming a straight row

From my understanding, adding display:inline to divs with a relative position should align them (left to right) similar to float:left. I attempted both methods but with no success. Below is an example of my latest try with inline displaying. My goal is to ...

Is there a way to create animated CSS box-shadow depth using jQuery or CSS3 transitions?

This code snippet applies delays but doesn't seem to update the style changes until the loop completes: for (i=20;i>=0;i--) { var boxShadow = i+"px "+i+"px "+i+"px #888"; $('article').css("box-shadow", boxShadow); ...

javascript how to retrieve the custom attribute value of an HTML style

I am dealing with an HTML element that has some inline styles as well as a custom CSS property. I am able to access every style attribute except for my custom style property. Here is the code I am working with: <img id="myimg" class="ImgClass" style=" ...

Transform the JSON response into a map

I have a functioning code that I am looking to adjust. let table_specs = {'columns': [ {'name': 'Col1', 'width': 7, ....}, {'name': 'Col2', 'width': 8, . ...

Encountering difficulty in implementing two modals simultaneously on a single web page while utilizing the useDisclosure() hook in Ch

ChakraUI provides a custom hook called useDisclosure() which gives you access to isOpen, onClose , onOpen. const { isOpen, onOpen, onClose } = useDisclosure() You can use the onOpen function as an onClick handler for a button to open a modal. <Modal ...

What sets response.setHeader apart from response.writeHead?

When it comes to sending a JSON response from my Nodejs server in my application, I have discovered two different methods. However, I am unsure about the distinctions between them. The first method involves var json = JSON.stringify(result.rows); respons ...

Using a tuple as a key in a Map in Typescript/Javascript can be a powerful

Encountered a strange bug in my code where I'm struggling to achieve constant time lookup from a Map when using a tuple as the key. Here is an example highlighting the issue, along with the workaround I am currently using to make it function: hello. ...

Redis Recursion: The callstack has reached its maximum size limit

Looking for some assistance with creating a game timer. I've decided to utilize Redis and Web Sockets in order to synchronize the timer across multiple devices. However, I'm running into an issue when trying to call my function recursively using ...

Utilizing React Typescript for Passing Props and Implementing them in Child Components

I'm currently working with React and TypeScript and attempting to pass data as props to a child component for use. However, I've encountered an error that I can't quite understand why it's happening or how to resolve it. Additionally, I ...

React component making repeated calls to my backend server

As a React newbie, I am currently in the process of learning and exploring the framework. I recently attempted to fetch a new Post using axios. However, upon hitting the '/getPost' URL, I noticed that the GetPost component continuously calls the ...

Switch body class when the navbar's collapse show class is toggled

There are numerous methods to accomplish this task, but I am seeking the most optimal and efficient approach. My goal is to toggle a custom body class when the .navbar-toggle triggers the .show class on the .navbar-collapse element. I'm hesitant abo ...

Problem with Formatting DateTime

How can I format a DateTime with the "general date short time" format without duplicating the time? Using the g specifier seems to be giving me 01-08-13 10:12:00 10:12 instead of 01-05-13 10:12. I'm puzzled as to why it's duplicating the time. ...

Executing XSS Reflected Attack by Loading an External JS Script via POST Parameter

Experimenting with XSS attacks on my vbox machines, just for kicks! I have two .html files - one works and the other doesn't. The file that works contains: <html> <head></head> <body> <form method="post" action=&q ...

Using routing with modules instead of components in Angular 10+ involves configuring the routing paths within the module files

I recently tried implementing modules instead of components for routing in Angular 10, but encountered a white screen issue. Any assistance would be greatly appreciated. Here is the code snippet I've been working with: import { NgModule } from &apos ...

Invoke the OnNavigatedTo method with the NavigationEventArgs parameter from a separate function

What is causing the issue? I am experiencing this problem. protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); RequestAsync( new Uri(teamsite), (html, exc) => { ...