Search code examples
javascriptdom-events

Attributes.Add Onclick Event in C# code behind


I have two textboxes tbxProdAc and txtbxHowMany. I would like to write a bit of code in the code behind so that when I enter text in one, any text that may have been in the other is removed.

How would one go about achieving this and in which event should I place it?

I have tried to use:

txtbxHowMany.Attributes.Add("onclick", "document.getElementById('tbxProdAC').innerText='';");

in the page load event but without success; should this be in the pre_init?

As you can probably tell, complete novice at work here.


Solution

  • First off, make sure that when the page has been generated that the ID is still "tbxProdAC" if this is a .Net control, the ID would have been changed. In this case you can use a class.

    Try the following, - This uses JQuery - you will need to include that

    txtbxHowMany.Attributes.Add("onclick", "$('#tbxProdAC').val('');"); 
    

    Add the following to the Head Section of your page.

     <script type="text/javascript" src="jquery.js"></script> 
    

    And you can get the JQuery.Js file from here: http://docs.jquery.com/Downloading_jQuery#Download_jQuery

    And Why: https://stackoverflow.com/questions/308751/why-use-jquery