【问题标题】:Javascript/Asp.net fire an event after dropdown listJavascript/Asp.net 在下拉列表后触发一个事件
【发布时间】:2015-03-23 09:33:14
【问题描述】:

我正在做一个项目,我需要从下拉列表中选择一个区域,该区域将在 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)
        {

        }


    }

}

【问题讨论】:

    标签: c# asp.net google-maps


    【解决方案1】:

    通常,这是实现。如果您将它用于多个下拉菜单,我不确定它是否会产生问题。

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

    希望如此! :)

    <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);
                        }
                    }
                    );
                }

    【讨论】:

      猜你喜欢
      • 2012-08-19
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多