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

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

问题描述

我有一个包含一个文本框控件和一个按钮控件的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网页,部分引用服务器控件的客户端脚本

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

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