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 用户控件中的控件


- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01