views:

25

answers:

1

Hi all ,

I have created dynamic controls in ASP.NET as following ..

first i have created checkbox as

CheckBox chkDynamic = new CheckBox();

Then

TextBox txtDynamic = new TextBox(); txtDynamic.ID = "txtDynamic";

and these controls added in tablecell added in tableRow added in Table added in aspPanel(Only panel created at design page)

Now what i need.. when checkbox is selected i want to clear the txtDynamic Textbox using JavaScript

I tried following ways but not working..

chkDynamic.Attributes["onclick"] = "javascript:document.getElementById('" + txtDynamic.UniqueID + "').value='';";

also i tried with calling Method as

chkDynamic.Attributes.Add("onclick", "javascript:ClearText(this)");

but in this method following line giving me error not found "txtDynamic".. because the control added dynamicaly.

document.getElementById("<%= txtDynamic.ClientID %>").value="";

Thanks in advance....

A: 

Have you ensured that your textbox has been loaded into the DOM when this script is getting executed?

I would recommend registering the uniqueID/Client id of the control in a javascript variable from server side.

Then on client side after the whole DOM is loaded bind the click event on the checkbox and then do respective work of cleaning up the textbox

(you can ensure that the DOM has loaded either by writing the script at end of page or using document.ready in case you use jQuery)

Thanks,
Pranav Kauhik
pranavkaushik.wordpress.com

Pranav Kaushik