How can I pass a value to a javascript function when a button in asp.net is clicked?

For the client side code, I have written the following:

<script>
    function initialize(x, y) {
        alert(x);
        alert(y);
        var mapProp = {


            center: new google.maps.LatLng(x, y),
            zoom: 7,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
    }

    function loadScript() {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://maps.googleapis.com/maps/api/js?key=&sensor=false&callback=initialize";
        document.body.appendChild(script);
    }

    window.onload = loadScript;
</script>

And for the server side code using asp.net:

protected void Button1_Click(object sender, EventArgs e) {
    Page.ClientScript.RegisterStartupScript(this.GetType(), "calljs", "initialize(" + "45.508742" + ", " + "-0.120850" + ");", true);
}

However, when running this code, the values are sent to the function but the line below does not work:

center: new google.maps.LatLng(x, y)

It works when changing the line to this:

center: new google.maps.LatLng(51.4323,-0.12332)

How can I resolve this issue?

Answer №1

To enhance the functionality of your asp.net button, I recommend utilizing the OnClientClick attribute:

<asp:button id="Button1" text="Display location" OnClientClick="initialize(51.4323,-0.12332); return false" runat="server" onclick="Button1_Click" />

It's advisable to avoid using RegisterStartupScript for setting up clientside click events.

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

What is the best way to transfer data between files in Python?

My website code is (ser.html) <div> <a v-bind:href="'ser/'+post.name"> {{post.name}}</a> </div> I am trying to transfer post.name data from this file to another one (det.html). In my views.py file: from django.shortcut ...

Can a React component be configured to show only a specific array key when returning an array?

Just diving into the world of react and experimenting with different approaches to understand how I can transition into this new architecture. Currently, I have a basic component where I am returning an array of elements (mainly for testing purposes): cl ...

Click on the button without any reaction

I'm having trouble with the button. When I click on the button with ng-click="goSearchTitle()", nothing happens. Any idea why it's not working? <body ng-app="myapp"> <div ng-contoller="searchbyTitle"> <h3>Sea ...

Get the redirectUri using npm's Request package

When using npm Request to generate a response, I am able to retrieve information like "statusCode" by using "response.statusCode". However, I am unable to retrieve other information such as "redirectUri" as it shows undefined. Is there a way to access the ...

Constantly loading image with Meteor HTTP request

Within my Meteor application, I am attempting to dynamically load a random image from an API which returns JSON data structured like this: { "id":2026 "url": "https:// ... " , "large_url":null, "source_id":609, "copyright":"CC0", "site":"unsplash" } ...

Ways to detect if there is a new database record and display it in the notification bar with an alert

In the Django framework, I am retrieving data through an AJAX request from a database like this: $.ajax({ url: '/activity/', type: 'GET', data:{}, success: function(data) { // alert(data); var div1 = doc ...

Determine if an object is already present in a JSON array by comparing their respective IDs

I have a shopping cart stored in JSON format. [{"tuote":{"id":"2","name":"Rengas 2","count":16,"price":"120.00"}},{"tuote":{"id":"1","name":"Rengas 6","count":"4","price":"25.00"}},{"tuote":{"id":"4","name":"Rengas 4","count":"4","price":"85.00"}}] Form ...

What is the best way to empty an array within the state of a React component using JSX?

I need assistance with a React and JSX project where I am creating input fields that can be removed by clicking a button. Currently, I have an empty array stored in the state of the Page component. This array is updated using the addItems function. Howev ...

Exploring the process of retrieving MongoDB documents with methods such as findOne() or find() in Node.js, particularly when the models and schema are contained in a separate file

As someone who is new to node, MongoDB, and coding in general, I'm currently working on a project involving the creation and storage of documents in MongoDB using Mongoose. Initially, all my models and schemas were housed in the app.js file, allowing ...

Tips for effectively crafting a component capable of managing both a value and an observable for that specific value

I'm actually curious about two things. When is it appropriate to pass an observable of an object into a component versus resolving it with the | async method? If I want to create a versatile reusable component that can handle both scenarios - accept ...

Uploading base64 encoded data to an asp.net 1.1 webpage

Our team is developing an automated patching tool and we need to upload files to our production server using an ASP.NET page or a web service. The challenge is that we can only access the production server via HTTP. Our goal is to have the page accept fi ...

Adjust camera view according to the rotation in three.js

I am in the process of creating a demonstration, and I'm facing an issue with moving the camera in my scene in the direction it is pointing. The concept is similar to pointer lock controls, but I need the camera to have the ability to move up, down, f ...

Newbie mishap: Utilizing an array retrieved from a function in javascript/jquery

After submitting the form, I call a function called getPosts and pass a variable str through it. My aim is to retrieve the data returned from the function. // Triggered upon form submission $('form#getSome').submit(function(){ var str = $("f ...

I am facing an issue with the clearTimeout function in my JavaScript code. Can anyone help

I am encountering some issues with the clearTimeout() function. The setTimeout() function is working as expected, but I want it to stop running when I close my notification. I'm not sure what is causing the problem in my function. After closing the ...

What Array functions in JavaScript cause changes to the original array?

Currently, I am working on developing a custom Array class in JavaScript and I'm curious about the functions that need to be overloaded in order to track any modifications made to the array. So far, I am aware that Array.push() and Array.splice() are ...

The webpage is unreachable on localhost after attempting to write to a file using node.js

I'm currently attempting to update a file using Node.js. I have a form that contains checkboxes, and upon form submission, the server should update the file based on which checkboxes are selected: a, b, or c. The JSON file structure is as follows: { ...

Attempting to display items using the map method, pulling in text from an array

I am working with an array state that tracks the text entered by the user in a text field. My goal is to display this text within a component so users can see what they have previously entered. However, I am facing an issue with my Hashtags component when ...

The access to the HTTP request has been restricted

Currently, I am developing multiple applications that need to communicate with each other. To test these apps, I am using both Chrome and Firefox browsers. Strangely, the issue persists in both browsers. The issue at hand: In one of my applications (let& ...

Obtain the template as a string within Vue

Let's examine the scenario of having a single file component in Vue with the following structure: // Article.vue <template> <div> <h1>{{title}}</h1> <p>{{body}}</p> </div> </template> If w ...

Unexpected behavior with jQuery AJAX Callback function:

When attempting to extract data from the success part of an AJAX call, I implemented a callback function for this purpose. Here is the code snippet: var data2; $(function () { function callback(data) { console.log(data); data2 = JSON.parse(data ...