的getElementById没有找到控制由ASP.net产生 [英] getElementById not finding control generated by ASP.net
问题描述
我只是想存储在JavaScript,但由于某种原因,这是不以的document.getElementById('控制')的工作变量的标签;
。我知道我的JavaScript是链接到我的html文件很好,因为一切工作的。
I am simply trying to store a label in a variable in javascript but for some reason this isn't working with document.getElementById('control');
. I know my javascript is linking to my html file fine because everything else works.
下面是我的javascript code:
Here is my javascript code:
function performEvapCooledCircuit(txt)
{
var error = document.getElementById('lblError');
if (txt.value == null || isNaN(txt.value))
{
error.style.visibility = "visible";
}
}
下面是HTML code为我的标签:
Here is the html code for my label:
<asp:Label ID="lblError" class="NormLabel" runat="server"
style="color:red; visibility:hidden;" Text="Invalid Input."></asp:Label>
我收到,说对象预期??
I am getting an error that says object expected??
推荐答案
在的ID ASP.NET会产生不会是lblError所以你需要通过它的客户端ID 引用它code>
The ID that ASP.NET will generate will not be "lblError" so you'll need to reference it by its ClientID
document.getElementById('<%=lblError.ClientID %>');
如果您的JavaScript文件是外部的,我通常已经不得不写一个类型的初始化的JavaScript方法,以确保我的ID分别设置属性。
If your javascript file is external I've usually had to write a type of "Init" javascript method to make sure my ID's were set up property
在您的ASPX页面:
<script type="text/javascript">
var lblError = null;
function InitializeVariables()
{
if (lblError == null) // make sure you only do this once
{
lblError = document.getElementById("<%=lblError.ClientID %>");
}
}
</script>
<asp:Label
ID="lblError"
class="NormLabel"
runat="server"
style="color:red; visibility:hidden;"
Text="Invalid Input."></asp:Label>
然后在您的JavaScript文件中,你必须调用 InitializeVariables()
,以确保你有指向正确的asp.net控制变量
Then in your javascript file you'll have to call InitializeVariables()
to make sure you've got the variables pointing to the proper asp.net controls
function performEvapCooledCircuit(txt)
{
InitializeVariables();
if (txt.value == null || isNaN(txt.value))
{
lblError.style.visibility = "visible";
}
}
这篇关于的getElementById没有找到控制由ASP.net产生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!