但是在点击 GO 按钮-6ren">
gpt4 book ai didi

javascript - 没有在 ASP.Net 中获取 ClientID

转载 作者:搜寻专家 更新时间:2023-11-01 04:54:43 27 4
gpt4 key购买 nike

我有一个 ASP.Net 页面,我在其中调用这样的 JavaScript 函数:

 Enter server name: <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
<asp:Button ID="btn_view" runat="server" OnClick="View_btn_click" OnClientClick="return AlertOnGo('View Configuration',document.getElementById('<%= txt_name.ClientID %>').value)" Text ="GO!" />

但是在点击 GO 按钮时,出现以下错误:

JavaScript 运行时错误:无法获取未定义或空引用的属性“值”

查看按钮的渲染代码时:

<input type="submit" name="btn_view" value="GO!" onclick="return AlertOnGo('View Configuration',document.getElementById('<%= txt_name.ClientID %>').value);" id="btn_view" />

它没有放置适当的 ClientID。我尝试将测试盒的 CLientIDMode 设置为静态,但没有帮助。

这个问题类似于这个unanswered question .

最佳答案

这个问题有几个解决方案。

一种更简单的方法是将名称移动到 JavaScript 变量中,因为问题仅在尝试评估 ASP.NET 控件的 OnClientClick 属性内的 txt_name.ClientID 时出现在控件中。

<script>
var txtName = '<%= txt_name.ClientID %>';
</script>

Enter server name: <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
<asp:Button ID="btn_view" runat="server" OnClick="View_btn_click"
OnClientClick="return AlertOnGo('View Configuration',
document.getElementById(txtName).value)" Text ="GO!" />

关于javascript - 没有在 ASP.Net 中获取 ClientID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16147288/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com