一个动态添加控件内查找ID元素 [英] Find an element by id within a dynamically added control

查看:149
本文介绍了一个动态添加控件内查找ID元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp页面中,我动态添加我创建了一个控制(多次)。在这种控制我有密码和用户名和恢复按钮,文本框。

I have an asp page in which I add dynamically a control I created (several times). In that control I have textbox for password and username and a revert button.

我用这个JavaScript code。在控制和失败:

I use this javascript code in that control and it fails:

function HandlePasswordChanged() {
    document.getElementById("<%=btnRevert.ClientID %>").disabled = false;
}
function HandleUserChanged() {
    document.getElementById("<%=btnRevert.ClientID %>").disabled = false;
    document.getElementById("<%=txtPassword.ClientID %>").disabled = false;
}
function btnRevertClick() {
    document.getElementById("<%=btnRevert.ClientID %>").disabled = true;
    document.getElementById("<%=txtPassword.ClientID %>").disabled = true;
    document.getElementById("<%=txtUsername.ClientID %>").value = document.getElementById("<%=systemAccount.ClientID %>").value;
    document.getElementById("<%=txtPassword.ClientID %>").value = "";
}

它的作用是,当我preSS上一个控制它禁用了其他控制文本框恢复按钮 - getelement未能找到正确的

what it does is when I press the revert button on one control it disables the textbox on the other control - getelement fails to find the correct one.

我该如何解决这个问题?

How can I fix this?

推荐答案

我设法找到解决方案。

I managed to find the solution.

问题是,每次我添加了acsx控制使用JavaScript code将其添加多种功能具有相同的名称,但里面是不同的。当一个控制想叫自己的功能,它只是使用了第一个,因为他们都命名为相同的。

The problem was that every time I added the acsx control with the javascript code it added multiple functions with the same name but the inside was different. when one control wanted to call its "own" function it just used the first one since they were all named the same.

我的解决办法是改变这一功能:

My solution was to change the function from this:

function HandleUserChanged() {
    document.getElementById("<%=btnRevert.ClientID %>").disabled = false;
    document.getElementById("<%=txtPassword.ClientID %>").disabled = false;
}

这样:

function HandleUserChanged(btnRevertId, txtPasswordId, cellPasswordId) {
    document.getElementById(btnRevertId).disabled = false;
    document.getElementById(txtPasswordId).disabled = false;
}

,然后在c#code我补充一点:

and then in the c# code I add this:

txtUsername.Attributes.Add("onchange", "HandleUserChanged(\"" + btnRevert.ClientID + "\", \"" + txtPassword.ClientID + "\", \"" + cellPassword.ClientID + "\")");

此方式,每个控制确切地知道哪些控制属于他,并发送正确的参数给函数。

This way each control know exactly which controls belong to him and sends the correct parameters to the function.

这篇关于一个动态添加控件内查找ID元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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