簡體   English   中英

Javascript / Asp.net在下拉列表后觸發事件

[英]Javascript/Asp.net fire an event after dropdown list

我正在做一個項目,需要從下拉列表中選擇一個區域,該區域將在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)
        {

        }


    }

}

通常,這是實現。 我不確定如果將其用於多個下拉列表是否會產生問題。

試試看。 讓我知道它是否有效。

希望能做到! :)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM