ASP.NET控件的JavaScript getElementById是否返回null?

ASP.NET控件的JavaScript getElementById是否返回null?,javascript,asp.net,webforms,getelementbyid,Javascript,Asp.net,Webforms,Getelementbyid,我使用JavaScript,在执行过程中会出现以下错误: Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object 这是我的代码: <asp:Content ID="content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> <script language="javascript

我使用JavaScript,在执行过程中会出现以下错误:

Microsoft JScript runtime error: 'document.getElementById(...)' is null or not an object
这是我的代码:

<asp:Content ID="content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script language="javascript" type="text/javascript">
    function ConfirmTransfere() {
        if (confirm("Syatem not allow negative inventory, would you like to continue ?") == true) {
            document.getElementById("btnAlelrt").click();

        }       
}
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="uxContainer" runat="server">
    <ContentTemplate>
 <table> 
<tr>
        <td>
        <asp:Button ID="uxTransfer" runat="server" Text="Transfer" OnClick="uxTransfer_Click" /> 
        <asp:Button ID="btnAlelrt" runat="server" Text="GetDetails" OnClick="btnAlelrt_Click" />       
        </td>
        </tr>
</table>
    </ContentTemplate>
<Triggers>
    <asp:PostBackTrigger ControlID="uxTransfer" />    
    </Triggers>
    </asp:UpdatePanel>

 </asp:Content>

函数confirmTransfer(){
如果(确认(“系统不允许负库存,是否继续?”)==真){
document.getElementById(“btnAlelrt”)。单击();
}       
}
这是:

function ConfirmTransfere() {
        if (confirm("Syatem not allow negative inventory, would you like to continue ?") == true) {
            document.getElementById("btnAlelrt").click();

        }       
需要这样做:

function ConfirmTransfere() {
        if (confirm("Syatem not allow negative inventory, would you like to continue ?") == true) {
            document.getElementById('<%=btnAlert.ClientID%>').click();

        }     
函数confirmTransfer(){
如果(确认(“系统不允许负库存,是否继续?”)==真){
document.getElementById(“”)。单击();
}     

问题是,您的按钮是ASP.Net控件,它将生成自己的客户端id,该id与您指定的id不同。输入代码
会将生成的id发布到浏览器。

如果查看呈现的页面,名为btnAlelrt的控件不再具有该名称


如果添加“,则asp:按钮提供的ID是asp.Net在服务器上使用的ID。而不是客户端脚本使用的ID

你应使用:

document.getElementById("<%= btnAlelrt.ClientID %>").click();
document.getElementById(“”)。单击();

如果您在生成的页面上查看源代码,您可能会发现按钮的ID不再是“btnAlert”。它可能有类似“ctl001\u btnAlert”的内容,或者其他生成的唯一性标识符

这就是javascript找不到它的原因

您需要按标记搜索,并检查ID是否以btnAlelrt结尾,或者必须在其周围放置一个不会更改的ID(即没有runat=“server”)的DIV或SPAN。然后,您可以根据其ID查找该元素,然后在其中获取按钮

如果可以,可以使用语法在javascript中插入正确的ID


(旁注:这应该是btnAlert吗?

我现在找不到可供发布的代码示例,但我可以解释您遇到的问题

ASP.NET在将对象/元素/按钮写入页面时不使用您提供的ID,而是为对象提供客户端ID

如果您在代码中使用Msgbox(btnAlelrt.clientID),那么您将看到javascript应该使用的对象名称

最好让.NET代码用javascript将这些值写入页面,因为它们不是常量

希望这有帮助

詹姆斯

你可以使用这个:

document.getElementById('<%= btnAlelrt.ClientID %>').click()
将呈现如下内容:

<span id="Label1" name="ctl00$MasterPageBody$ctl00$Label1" />

尝试使用clientmode=“Static”。这不会在运行时更改按钮id

  <asp:Button ID="SendMessageButton" ClientIDMode="Static" runat="server" Text="Send Message" CssClass="buttonPositive"
            CausesValidation="True" OnClientClick="validate();" OnClick="SendMessageButton_Click" />
谢谢,clientmode=“Static”保存我的问题,因为我无法在javascript中获取div。
  <asp:Button ID="SendMessageButton" ClientIDMode="Static" runat="server" Text="Send Message" CssClass="buttonPositive"
            CausesValidation="True" OnClientClick="validate();" OnClick="SendMessageButton_Click" />
 var element = document.getElementById('SendMessageButton');