js getelementbyid返回null

时间:2017-03-27 03:18:34

标签: javascript asp.net getelementbyid

getElementById返回null但填充了列表。什么是错误的猜测?

HomePage.aspx和Map.js



var Center = { lat: 20.452425, lng: -68.545446 };

function initMap() {
    
   
    map = new google.maps.Map(document.getElementById('map'), {
        zoom: 3,
        center: Center
    });   
    populateMap()
    function populateMap()
    {
        var DiveSiteList2 = document.getElementById("DiveSiteList")
         for (var i = 0; i < DiveSiteList2.options.length; ++i)
        {...}
        

    }
    }
&#13;
<div id="ListHolder">
  <asp:ListBox class="ListBox" ID="DiveSiteList" runat="server"></asp:ListBox>
</div>
&#13;
&#13;
&#13;

HomePage.aspx.cs

    private DiveManager.DiveManager Manager;
    protected void Page_Load(object sender, EventArgs e)
    {
        Manager = new DiveManager.DiveManager();
        BindDiveList();
    }

    private void BindDiveList()
    {
       foreach (DiveSite DS in Manager.MasterDiveSiteList)
        {
            ListItem Item = new ListItem();
            Item.Text = DS.DiveSiteName;
            Item.Attributes.Add("Name", DS.DiveSiteName);
            Item.Attributes.Add("Latitude", DS.Latitude.ToString());
            Item.Attributes.Add("Longitude", DS.Longitude.ToString());
            Item.Attributes.Add("ID", DS.DiveSiteID.ToString());
            DiveSiteList.Items.Add(Item);
        }
    }

页面错误:未捕获的TypeError:无法读取属性&#39;选项&#39;为null     在populateMap(Map.js:19)

1 个答案:

答案 0 :(得分:0)

请检查一下 请改变一下:

var DiveSiteList2 = document.getElementById('<%= DiveSiteList.ClientID %>')

var Center = { lat: 20.452425, lng: -68.545446 };

function initMap() {
    
   
    map = new google.maps.Map(document.getElementById('map'), {
        zoom: 3,
        center: Center
    });   
    populateMap()
    function populateMap()
    {
        var DiveSiteList2 = document.getElementById('<%= DiveSiteList.ClientID %>')
         for (var i = 0; i < DiveSiteList2.options.length; ++i)
        {...}
        

    }
    }
<div id="ListHolder">
  <asp:ListBox class="ListBox" ID="DiveSiteList" runat="server"></asp:ListBox>
</div>