如何使用javascript访问runat =“server”ASP元素?

时间:2021-11-10 15:57:45

It seems everyone is doing this (in code posts etc.)...but I dont' know how :(

似乎每个人都这样做(在代码帖等)...但我不知道如何:(

whenever i try to manipulate an asp element using javascript i get a "element is null" or "document is undefined" etc. error.....

每当我尝试使用javascript操纵一个asp元素时,我得到一个“元素为空”或“文档未定义”等错误.....

javascript works fine usually,...but only when i add the runat="server" attribute does the element seem invisible to my javascript.

javascript通常工作正常,...但只有当我添加runat =“server”属性时,该元素似乎对我的javascript不可见。

any suggestions would be appreciated

任何建议,将不胜感激

Thanks, Andrew

4 个解决方案

#1


24  

What's probably happening is that your element/control is within one or more ASP.NET controls which act as naming containers (Master page, ITemplate, Wizard, etc), and that's causing its ID to change.

可能发生的是您的元素/控件位于一个或多个ASP.NET控件中,这些控件充当命名容器(母版页,ITemplate,向导等),并且导致其ID发生更改。

You can use "view source" in your browser to confirm that's what's happening in the rendered HTML.

您可以在浏览器中使用“查看源代码”来确认呈现的HTML中发生的情况。

If your JavaScript is in the ASPX page, the easiest way to temporarily work around that is to use the element's ClientID property. For example, if you had a control named TextBox1 that you wanted to reference via JS:

如果您的JavaScript位于ASPX页面中,暂时解决此问题的最简单方法是使用元素的ClientID属性。例如,如果您有一个名为TextBox1的控件,您想通过JS引用:

var textbox = document.getElementById('<%= TextBox1.ClientID %>');

#2


2  

Making an element runat="server" changes the client-side ID of that element based on what ASP.NET naming containers it's inside of. So if you're using document.getElementById to manipulate the element, you'll need to pass it the new ID generated by .NET. Look into the ClientId property to get that generated ID...you can use it inline in your Javascript like so:

使元素runat =“server”根据它所在的ASP.NET命名容器更改该元素的客户端ID。因此,如果您使用document.getElementById来操作元素,则需要将其传递给.NET生成的新ID。查看ClientId属性以获取生成的ID ...您可以在Javascript中使用内联,如下所示:

var element = document.getElementById('<%=myControl.ClientID%>');

#3


2  

If you have a textbox:

如果你有一个文本框:

<asp:TextBox id="txtText" runat="server" />

YOu can use:

您可以使用:

var textBox=document.getElementById('<%=txtText.ClientID %>');

Any WebControl exposes the same ClientID property.

任何WebControl都会公开相同的ClientID属性。

#4


1  

All though the question has been answered, thought I would just post some further info...

尽管问题已经得到解答,但我想我会发布更多信息......

Rick Strahl provided quite an intresting work around to this problem.

Rick Strahl为这个问题提供了一个非常有趣的工作。

http://www.west-wind.com/WebLog/posts/252178.aspx

Thankfully when ASP .NET 4.0 arrives, it will allow you to specify exacly what the client ID's will be!

值得庆幸的是,当ASP .NET 4.0到来时,它将允许您精确指定客户端ID将是什么!

http://www.codeproject.com/KB/aspnet/ASP_NET4_0ClientIDFeature.aspx

#1


24  

What's probably happening is that your element/control is within one or more ASP.NET controls which act as naming containers (Master page, ITemplate, Wizard, etc), and that's causing its ID to change.

可能发生的是您的元素/控件位于一个或多个ASP.NET控件中,这些控件充当命名容器(母版页,ITemplate,向导等),并且导致其ID发生更改。

You can use "view source" in your browser to confirm that's what's happening in the rendered HTML.

您可以在浏览器中使用“查看源代码”来确认呈现的HTML中发生的情况。

If your JavaScript is in the ASPX page, the easiest way to temporarily work around that is to use the element's ClientID property. For example, if you had a control named TextBox1 that you wanted to reference via JS:

如果您的JavaScript位于ASPX页面中,暂时解决此问题的最简单方法是使用元素的ClientID属性。例如,如果您有一个名为TextBox1的控件,您想通过JS引用:

var textbox = document.getElementById('<%= TextBox1.ClientID %>');

#2


2  

Making an element runat="server" changes the client-side ID of that element based on what ASP.NET naming containers it's inside of. So if you're using document.getElementById to manipulate the element, you'll need to pass it the new ID generated by .NET. Look into the ClientId property to get that generated ID...you can use it inline in your Javascript like so:

使元素runat =“server”根据它所在的ASP.NET命名容器更改该元素的客户端ID。因此,如果您使用document.getElementById来操作元素,则需要将其传递给.NET生成的新ID。查看ClientId属性以获取生成的ID ...您可以在Javascript中使用内联,如下所示:

var element = document.getElementById('<%=myControl.ClientID%>');

#3


2  

If you have a textbox:

如果你有一个文本框:

<asp:TextBox id="txtText" runat="server" />

YOu can use:

您可以使用:

var textBox=document.getElementById('<%=txtText.ClientID %>');

Any WebControl exposes the same ClientID property.

任何WebControl都会公开相同的ClientID属性。

#4


1  

All though the question has been answered, thought I would just post some further info...

尽管问题已经得到解答,但我想我会发布更多信息......

Rick Strahl provided quite an intresting work around to this problem.

Rick Strahl为这个问题提供了一个非常有趣的工作。

http://www.west-wind.com/WebLog/posts/252178.aspx

Thankfully when ASP .NET 4.0 arrives, it will allow you to specify exacly what the client ID's will be!

值得庆幸的是,当ASP .NET 4.0到来时,它将允许您精确指定客户端ID将是什么!

http://www.codeproject.com/KB/aspnet/ASP_NET4_0ClientIDFeature.aspx