本文介绍了用于 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!