document.getElementById(" Table1")总是" null"

时间:2015-09-17 15:15:32

标签: javascript html asp.net

为什么document.getElementById("Table1")总是给我null, 我将使用下面的函数将动态添加列到表Table1 你有想法吗? 感谢更好的解决方案。我用aspx语言工作 vb.net

<%@ Page Title="Mapp" Language="VB" MasterPageFile="~/Site.Master"    AutoEventWireup="false"
CodeFile="Mappa.aspx.vb" Inherits="Mappa" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<style type="text/css">
    .style2
    {
        width: 409px;
    }
</style>
</asp:Content>
<asp:Content ID="BodyContent" runat="server"   ContentPlaceHolderID="MainContent">

<!DOCTYPE html>
<html>

<head>
    <link href="Styles/MapStile.css" rel="stylesheet" type="text/css" />
  <script src="http://maps.googleapis.com/maps/api/js?"></script>
<asp:ScriptManager id="ScriptManager" runat="server"      EnablePageMethods="true" EnablePartialRendering="true"       EnableScriptGlobalization="true" EnableScriptLocalization="true" />
<script>

    function displayResult() {
        var table = document.getElementById("Table1");
        var row = table.insertRow(-1); // change to 0 to add at the top of the table
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        cell1.innerHTML = "R? C1";
        cell2.innerHTML = "R? C2";
    }

    google.maps.event.addDomListener(window, 'load', initialize);

</script>


</head>

<body >

    <Table ID="myTable" runat="server" Width="100%"> 
        <tr>
            <td>
                <Table id="Table1" runat="server" Width="100%"> 
                        <tr>
                           <td>  ciao </td>
                        </tr>
                        <tr>
                           <td>  ciao </td>
                        </tr>
                        <tr>
                           <td>  ciao </td>
                        </tr>
                        <tr>
                           <td>  ciao </td>
                        </tr>
                  </Table>  

            </td>
            <td> <div id="googleMap" style="float:right;width:960px;border:0.5px solid blue; height:600px;"></div  ></td>

        </tr>
    </Table>  
    <p><button type="button" onclick="displayResult()">Insert new row</button></p>
</body>

1 个答案:

答案 0 :(得分:0)

希望这篇文章能够解决你的问题。

如何在服务器控件的javascript中通过id获取元素。

getElementById not finding control generated by ASP.net

使用此:document.getElementById('<%=Table1.ClientID %>');