用于 ASP.NET 控件的 JavaScript getElementById 返回 nul

JavaScript getElementById for ASP.NET Control returns null?(用于 ASP.NET 控件的 JavaScript getElementById 返回 null?)
本文介绍了用于 ASP.NET 控件的 JavaScript getElementById 返回 null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 JavaScript,但在执行过程中出现此错误:

I use JavaScript and this error appears for me during execution:

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>

推荐答案

这个:

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

        }     

问题在于您的按钮是一个 ASP.Net 控件并且会生成它自己的客户端 ID,该 ID 将与您指定的 ID 不同.输入代码 <%=btnAlert.ClientID%> 将生成的发布到浏览器.

The problem is that your button is an ASP.Net control and will generate it's own client id, which will be different from the ID you specified. putting in the code <%=btnAlert.ClientID%> Will post the generated one out to the browser.

这篇关于用于 ASP.NET 控件的 JavaScript getElementById 返回 null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
dropbox-sdk-dotnet: how to get a refresh token and update the access token(Dropbox-SDK-Dotnet:如何获取刷新令牌并更新访问令牌)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)