Find An Element By Id Within A Dynamically Added Control
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. I use this javascri
Solution 1:
If you are working on .net 4.0:
You can set ClientIDMode="Static"
for your dynamically added controls. Also, you have to make sure you set unique ids for your controls.
Solution 2:
I managed to find the solution.
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:
functionHandleUserChanged() {
document.getElementById("<%=btnRevert.ClientID %>").disabled = false;
document.getElementById("<%=txtPassword.ClientID %>").disabled = false;
}
to this:
functionHandleUserChanged(btnRevertId, txtPasswordId, cellPasswordId) {
document.getElementById(btnRevertId).disabled = false;
document.getElementById(txtPasswordId).disabled = false;
}
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.
Post a Comment for "Find An Element By Id Within A Dynamically Added Control"