Javascript / Asp.net在下拉列表后触发事件

时间:2015-03-23 09:33:15

标签: c# asp.net google-maps

我正在开展一个项目,我需要从下拉列表中选择一个区域,该区域将在Google地图上进行缩放。在我的代码中,我有3个下拉列表。我已经为所有区域放置了标记,但是我需要为一个选定区域获取标记,并且该区域应该在地图上放大。这是我所做的代码。我应该添加什么才能获得所需的结果?

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)
        {

        }


    }

}

1 个答案:

答案 0 :(得分:0)

通常,这是实施。如果你将它用于多个下拉列表,我不确定它是否会产生问题。

试一试。让我知道它是否有效。

希望它能做到! :)

&#13;
&#13;
<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);
                    }
                }
                );
            }
&#13;
&#13;
&#13;