没有得到在ASP.Net的ClientID [英] Not getting ClientID in ASP.Net
问题描述
我有我在哪里调用JavaScript函数这样的ASP.Net页:
I have ASP.Net page where i am calling a JavaScript function like this:
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按钮,我收到以下错误:
But on clicking the GO button, i am getting the following error:
的JavaScript运行时错误:无法获取的未定义或为空引用属性'值'
在观看呈现的code的按钮:
On viewing the rendered code for the button:
<input type="submit" name="btn_view" value="GO!" onclick="return AlertOnGo('View Configuration',document.getElementById('<%= txt_name.ClientID %>').value);" id="btn_view" />
有没有把相应的客户端ID。我尝试设置的ClientIDMode为静态的测试中,还没有帮助。
It is not putting the appropriate ClientID. I tried setting the CLientIDMode to static for the test box, yet no help.
这个问题是类似于此<一个href=\"http://stackoverflow.com/questions/7147906/issue-getting-clientid-in-asp-net-c-sharp\">unanswered问题。
推荐答案
有这个问题的一些解决方案。
There are a few solutions to this problem.
一个简单的将是名字进入一个JavaScript变量,因为这个问题试图评估一个ASP.NET控件的属性的OnClientClick里面txt_name.ClientID时只发生在控制范围内。
One of the simpler would be to move the name into a JavaScript variable since the problem only occurs within the control when trying to evaluate txt_name.ClientID inside the OnClientClick attribute of an ASP.NET control.
<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!" />
这篇关于没有得到在ASP.Net的ClientID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!