在 JavaScript 中正确引用 ASP.NET 用户控件中的控件 [英] Properly referencing controls in ASP.NET user controls in JavaScript

查看:17
本文介绍了在 JavaScript 中正确引用 ASP.NET 用户控件中的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ASP.NET 用户控件,其中包含一个文本框控件和一个按钮控件.此用户控件将多次添加到我的网页中.我需要有一段 JavaScript 可以在文本框更改时运行,如果文本框的值无效则禁用该按钮.我的问题是:如何将 JavaScript 放在只能引用同一用户控件中的按钮的文本框中?请记住,有许多 ASP.NET 用户控件,每个控件都有一个按钮和一个文本框,我只希望文本框中的无效值影响一个关联的按钮.谢谢!

I have an ASP.NET user control that contains a text box control and a button control. This user control will be added to my web-page many times. I need to have a piece of JavaScript that will run whenever the text box changes, and disable the button if the value of the text box is invalid. My question is this: how do I put JavaScript on the textbox that can reference only the button that is in the same user control? Remember, there are many ASP.NET user controls, each with a button and a text box, and I only want the invalid value in a text box to affect the one associated button. Thanks!

推荐答案

你可以使用控件的ClientId属性(它在客户端唯一标识控件)并做这样的事情:

You can use ClientId property of controls (it's uniquely identifies control on the client side) and make something like that:

<asp:TextBox runat="server" ID="myTextBox" />
<asp:Button runat="server" ID="myButton" Text="click" />

<script language="javascript" type="text/javascript">
    document.getElementById("<%=myTextBox.ClientID %>").onclick = function() {
        document.getElementById("<%=myButton.ClientID %>").disabled = "disabled";
    }
</script>

另请参阅此文档:ASP.NET 网页中的客户端脚本参考服务器部分客户端脚本中的控件

这篇关于在 JavaScript 中正确引用 ASP.NET 用户控件中的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆