html 文本框功能 onkey_Up 在 ascx 控件上不起作用

html textbox funtion onkey_Up is not working on ascx control

我尝试onkey_up将一个textbos数据复制到用户控件ascx文件上的另一个文本框但是当我运行它不是wroking.is它因为我写了onkey_up .ascx 上的函数,我应该写在母版页上吗?

function sync() {
        var TextBoxA1 = document.getElementById('TestAmountTextBox');
        var TextBoxA2 = document.getElementById('TestAmountTextBox_2');
        TextBoxA2.value = TextBoxA1.value;
    }

html 输入框:

<input type="text" runat="server" id="TestAmountTextBox" class="form-control currency-input" placeholder="From " aria-label="From Transaction Amount" data-allownegative="true" style="width: 100px;" maxlength="14" tabindex="7" onkeyup="sync()"/>                                                         
<input type="text" runat="server" id="TestAmountTextBox_2" class="form-control currency-input" placeholder="To " aria-label="To Transaction Amount" data-allownegative="true" style="width: 100px;" maxlength="14" tabindex="13" />  

默认情况下,将文本框添加到用户控件将强制 ASP.NET 呈现唯一 ID。类似于“usercontrolId_textboxId”。您可以通过查看 HTML 来源找到实际 ID。

有几个选项可以解决 ASP.NET 生成的 ID:

选项 1:

您可以关闭 ASP.NET 自动生成的 ID,方法是设置 ClientIDMode="Static" 例如:

<input type="text"
      runat="server" 
      id="TestAmountTextBox" 
      ClientIDMode="Static"
      ... [the rest of the attributes]

ClientIDMode 中有可供使用的替代属性。也可以在 web.config.

中完全关闭它

如果 ClientIDMode 设置为 Static,则如果重复使用相同的 ID,ASP.NET 将抛出错误。例如,在同一页面上多次使用用户控件。

选项 2:

将ASP.NET ID绑定到JavaScript代码,使用ClientID,例如:

function sync() {
    var TextBoxA1 = document.getElementById('<%= TestAmountTextBox.ClientID %>');
    var TextBoxA2 = document.getElementById('<%= TestAmountTextBox_2.ClientID %>');
    TextBoxA2.value = TextBoxA1.value;
}

这只有在 JavaScript 放在用户控件上时才有效。

选项 3:

第三个选项是用 getElementsByClassName 替换 getElementById 并在文本框中添加另一个 class。