JavaScript getElementById for ASP.NET Control returns 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用于 ASP.NET 控件的 JavaScript getElementById 返回 nul
基础教程推荐
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
