Trigger an event in Javascript/ASP.net following a dropdownlist selection

On a single HTML page, I have incorporated three dropdown lists for country, city, and district with an initial blank selected value, along with a Google map. The aim is to automatically center and zoom in on the map whenever users make selections from these dropdown lists.

An issue arises in the following scenario:

  1. The user selects a country from the country dropdown list.
  2. I need to fetch the newly selected value once the dropdown list has been altered.
  3. An event should be triggered to update the Google map accordingly.

I attempted to utilize both the onchange and change JavaScript events, but encountered an obstacle where the text retrieved from the dropdown list was the text BEFORE the selection event took place.

Hence, the main question remains: How can I trigger an event AFTER the dropdown list has been changed?

Below is the snippet of code being used:

  <asp:DropDownList ID="DropDownList_Country" runat="server" Height="29px" Width="209px"
                            Font-Size="18px" CausesValidation="false" onchange="MapLocationUpdater();">


function MapLocationUpdater() {

                var address = getDropdownListAddress();
                geocoder.geocode({ 'address': address },
                function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        map.setCenter(results[0].geometry.location);
                        map.setZoom(11);
                        var marker = new google.maps.Marker({
                            map: map,

                            position: results[0].geometry.location
                        });

                    }
                    else {
                        alert("Geocode was not successful for the following reason: " + status);
                    }
                }
                );
            }

            function getDropdownListAddress() {

                var ddlCountry = document.getElementById("<%=DropDownList_Country.ClientID%>");
                var ddlCity = document.getElementById("<%=DropDownList_City.ClientID%>");
                var ddlDistrict = document.getElementById("<%=DropDownList_District.ClientID%>");

                var CountryTxt = ddlCountry.options[ddlCountry.selectedIndex].text;
                var CityTxt = ddlCity.options[ddlCity.selectedIndex].text;
                var DistrictTxt = ddlDistrict.options[ddlDistrict.selectedIndex].text;

                return CountryTxt + " " + CityTxt + " " + DistrictTxt;

            }

Answer №1

Here's a suggestion for you:

<asp:DropDownList ID="DropDownList_Country" runat="server" Height="29px" Width="209px"
                        Font-Size="18px" CausesValidation="false" onchange="setTimeout('MapLocationUpdater();',1000);">

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

Enhance user interaction in Angular 13 by animating a selected element using just one animation block

I am currently working on a one-page website project to enhance my Angular skills, and I'm facing a challenge with animating multiple DOM elements using a single animation. Defining the animation for each element individually seems like a cumbersome a ...

JavaScript code to prevent user from entering phone number

I operate a booking platform similar to AirBnB where users communicate with each other through messages on the site. My goal is to prevent users from sharing telephone numbers and email addresses within these messages. Currently, I am implementing a meth ...

Ways to dynamically update a div with data from a JSON response

I'm currently in the process of developing a search platform. I have three static divs on the search results page that display certain content, all containing similar code. For example: <div id="result" class="card"> <img src="hello.png" ...

Tips for creating JSON using AngularJs to store tabular information

I successfully created an HTML page using AngularJS. <form name="target" ng-submit="createAllKeywordsJSON(codeMasterList)"><!-- createAllKeywordsJSON() --> <input type="submit" value="Save" class="myButton" name="submit" style="margin: ...

Enhance the axis functionality in c3.js

My goal is to display a user's financial data for the previous week, but sometimes they may not have data for all seven days. When using c3.js, I noticed that it automatically extends the 'endpoint-value' to the end of the axis. Is there a w ...

Connect a parent node to a specific subset within Mermaid's graph structure

Managing a complex Mermaid diagram that includes numerous subgraphs can be challenging. The size of the diagram often makes it difficult to maintain the correct order of subgraphs, leading to occasional rearrangements for clarity and positioning. However, ...

What is the best way to determine the normals of a closed shape in three.js?

I am currently developing a custom mesh importer for my proprietary file format. The challenge I'm facing is that the format does not include normal data. As a result, I am exploring methods to calculate normals for enclosed shapes and then apply thos ...

Guide to setting up a custom js file in Laravel admin template

Currently working with Laravel 5.8 and utilizing the laravel-admin Template for administrative purposes. There are times when I require custom JavaScript and CSS files specifically for certain admin controllers. How can I include these JS and CSS in lara ...

Error occurred when invoking jQuery.ajax in C# .NET is not recognized as valid

Take a look at my JQuery script below: $("#click_me").click(function() { jQuery.ajax({ type: "POST", url: "http://localhost:8882/Greet.aspx", data: '', ...

What is the best way to determine which DOM element triggered a click event?

I currently have a few Card components from material UI, each containing an EDIT button with a corresponding handler. These cards are dynamically added using Map traversal (for this example, I have hard coded two of them). My challenge now is trying to ma ...

Navigating Angular.js: finding my way to the endpoint paths

Despite my best efforts and extensive research, I find myself in need of assistance! I have a web application that utilizes node and express on the server side, with Angular on the client side. My issue lies with angular routing. IMPORTANT DETAILS My cur ...

What are some steps I can take to diagnose why my Express server is not receiving POST requests from my HTML form?

Struggling with an unexpected issue on my website where the form submission is not triggering the POST request to my Express server. I've set up a MongoDB database and created a HTML form to store user data, but something seems to be amiss. HTML: & ...

Updating a string in JavaScript by dynamically adding values from a JSON object

In my current function, I am making a request to fetch data and storing it as an object (OBJ). Afterwards, I make another request to get a new URL that requires me to update the URL with values from the stored data. The information saved in the object is ...

Mapping two arrays in JavaScript involves iterating through each element of the arrays

I'm having trouble displaying the content of two arrays contained within an object. When I map over RType.information.Type, I can display the content of the "Type" array. However, I am unable to display both Type[] and Price[]. I've tried various ...

Attempting to generate printed documents with multiple components spread across individual pages using react-to-print

I am working with the react-to-print library and I have a requirement to print a list of components, with each component on its own separate page. However, when I click on the print button, I encounter an error stating that the argument does not appear to ...

How can I display several custom markers that are constantly updating on a Google map with MySQL and PHP?

Currently, I am using the following code to generate markers on a Google map by retrieving data from a database. However, the issue I am facing is that it only generates one marker instead of all the markers stored in the database. & ...

Concealing alert messages automatically in CodeIgniter PHP after a certain amount of time

After attempting to use a script to hide the flash message once displayed, I found that it was not working as expected. The flash message remains visible until the page is refreshed. Controller: if ($this->email->send()) { $this- ...

What is the best way to ensure that the search box automatically adjusts its position and size regardless of the screen resolution?

I'm currently working on a responsive website project and facing an issue with the absolute positioning of the search bar on the main page, which is crucial for me. Below is the code snippet: <div class="span4"> <form class="well form ...

Customizing the starting number of rows per page in TablePagination from material-ui

As a newcomer to using materials, I am looking to customize the table pagination to show 'n' number of rows, for example 10, and remove the "Rows per page" dropdown. <TablePagination rowsPerPageOptions={[]} component="div" ...

Storing JavaScript code in a PHP variable fails to function

I've encountered an issue with my JavaScript code. <script> $(document).ready(function(){ $('.delete').click(function() { alert('passed'); }); }); </script> Everything work ...