Javascript getElementById 找不到由 ASP.net 生成的控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4595823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getElementById not finding control generated by ASP.net
提问by Johnrad
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 中的变量中,但由于某种原因,这不适用于document.getElementById('control');
. 我知道我的 javascript 可以很好地链接到我的 html 文件,因为其他一切都可以正常工作。
Here is my javascript code:
这是我的 javascript 代码:
function performEvapCooledCircuit(txt)
{
var error = document.getElementById('lblError');
if (txt.value == null || isNaN(txt.value))
{
error.style.visibility = "visible";
}
}
Here is the html code for my label:
这是我的标签的 html 代码:
<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??
我收到一个错误,提示对象是预期的??
回答by hunter
The ID that ASP.NET will generate will not be "lblError" so you'll need to reference it by its ClientID
ASP.NET 将生成的 ID 不会是“lblError”,因此您需要通过其引用它 ClientID
document.getElementById('<%=lblError.ClientID %>');
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
如果您的 javascript 文件是外部的,我通常必须编写一种“Init”javascript 方法来确保我的 ID 已设置属性
On your ASPX page:
在您的 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>
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
然后在您的 javascript 文件中,您必须调用InitializeVariables()
以确保变量指向正确的 asp.net 控件
function performEvapCooledCircuit(txt)
{
InitializeVariables();
if (txt.value == null || isNaN(txt.value))
{
lblError.style.visibility = "visible";
}
}
回答by Maverick
"hunter" gives a pretty solid way of doing things, however a, in my opinion far better method is to use the "CliendIDMode" property on the control and set that property to "Static". This will make the client and server IDs the same. Like this:
“猎人”提供了一种非常可靠的做事方式,但是,在我看来,更好的方法是在控件上使用“CliendIDMode”属性并将该属性设置为“静态”。这将使客户端和服务器 ID 相同。像这样:
<asp:TextBox ID="ServerAndClientId" runat="server" ClientIDMode="Static" />
回答by ?ime Vidas
The ID of the label is not "lblError". The ASP.net engine changed the ID. Check the HTML source code in the browser to find out the real ID.
标签的 ID 不是“lblError”。ASP.net 引擎更改了 ID。在浏览器中检查 HTML 源代码以找出真实 ID。
回答by StuperUser
That's not HTML for the label, that is an ASP.NET Control which will be rendered into HTML before it is sent in the response. ASP.NET WebForms controls sometimes change the id for the HTML they create.
这不是标签的 HTML,而是一个 ASP.NET 控件,它将在响应中发送之前呈现为 HTML。ASP.NET WebForms 控件有时会更改它们创建的 HTML 的 id。
View the source of the webpage to see what the id for the HTML element is on the rendered page.
查看网页的源代码以查看呈现的页面上 HTML 元素的 id。
回答by algreat
You can use this:
你可以使用这个:
document.getElementById('<%= lblError.ClientID %>').click()
Starting from ASP.NET 4.0 you can use ClientIDModeproperty for you element. And if you set it into Static
then the ClientID
value will be set to the value of the ID property:
从 ASP.NET 4.0 开始,您可以为您的元素使用ClientIDMode属性。如果您将其设置为,Static
则该ClientID
值将设置为 ID 属性的值:
<asp:Label ID="lblError" runat="server" ClientIDMode="Static" />
will be rendered as something like this:
将呈现为如下所示:
<span id="lblError" name="ctl00$MasterPageBody$ctl00$Label1" />