简体   繁体   中英

Javascript/Asp.net fire an event after dropdown list

I'm working on a project in which I need to select an area from drop down list and that area will be zoomed on Google map. In my code, I have 3 dropdownlists. I have already put the markers for all areas, but I need to get a marker for one selected area and that area should be zoomed on the map. Here is the code of what I have done. What should I add to it in order to get the required result?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace trial2
{
    public partial class explore : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                DropDownList1.DataBind();

                ListItem liMainArea = new ListItem("Select", "-1");
                DropDownList1.Items.Insert(0, liMainArea);

                DropDownList2.DataBind();

                ListItem liSubArea = new ListItem("Select", "-1");
                DropDownList2.Items.Insert(0, liSubArea);

                DropDownList3.DataBind();
                ListItem liAmenities = new ListItem("Select", "-1");
                DropDownList3.Items.Insert(0, liAmenities);


                DropDownList2.Enabled = false;
                DropDownList3.Enabled = false;

            }
            string markers = GetMarkers();
            Literal1.Text = @"
     <script type='text/javascript'>
     function initialize() {

     var mapOptions = {
     center: new google.maps.LatLng(23.0300, 72.5800),
     zoom: 12,
     mapTypeId: google.maps.MapTypeId.ROADMAP};

     var myMap = new google.maps.Map(document.getElementById('googleMap'), mapOptions);"
            + markers +
            @"}
    google.maps.event.addDomListener(window, 'load', initialize);
     </script>";

            }
        protected string GetMarkers()
        {
            string markers = "";
            using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["gisConnectionString"].ConnectionString))
                {
                SqlCommand cmd = new SqlCommand("SELECT [Name], [Latitude], [Longitude] FROM [MAIN AREA]", con);
                con.Open();

                SqlDataReader reader = cmd.ExecuteReader();
                int i = 0;

                while (reader.Read())
                {
                    i++;
                    markers +=
                    @"var marker" + i.ToString() + @" = new google.maps.Marker({
              position: new google.maps.LatLng(" + reader["Latitude"].ToString() + ", " +
                    reader["Longitude"].ToString() + ")," +
                    @"map: myMap,
              title:'" + reader["Name"].ToString() + "'});";
                }
            }
            return markers;
        }


        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedIndex == 0)
            {            
                DropDownList2.Enabled = false;
                DropDownList2.DataBind();

                ListItem liSubArea = new ListItem("Select", "-1");
                DropDownList2.Items.Insert(0, liSubArea);

                DropDownList3.Enabled = false;
                DropDownList3.DataBind();

                ListItem liAmenities = new ListItem("Select", "-1");
                DropDownList3.Items.Insert(0, liAmenities);

            }
            else
            { DropDownList2.Enabled = true;


                DropDownList2.DataBind();

                ListItem liSubArea = new ListItem("Select", "-1");
                DropDownList2.Items.Insert(0, liSubArea);

                DropDownList3.SelectedIndex = 0;
                DropDownList3.Enabled = false;

            }
        }



        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList2.SelectedIndex == 0)
            {             
                DropDownList3.Enabled = false;

                DropDownList3.DataBind();
                ListItem liAmenities = new ListItem("Select", "-1");
                DropDownList3.Items.Insert(0, liAmenities);
            }
            else
            {
                DropDownList3.Enabled = true;


                DropDownList3.DataBind();

                ListItem liAmenities = new ListItem("Select", "-1");
                DropDownList3.Items.Insert(0, liAmenities);

            }
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


    }

}

Normally, this is the implementation. I am not sure if it will create issues if you use it for multiple dropdowns.

Give it a try. Let me know if it worked.

Hopefully it does! :)

 <asp:DropDownList ID="DropDownList1" 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); } } ); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM