Properly referencing controls in ASP.NET user controls in JavaScript(在 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 用户控件中的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 JavaScript 中正确引用 ASP.NET 用户控件中的控件
基础教程推荐
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
