Tutorial :Passing asp.net values to JavaScript function



Question:

I need to pass the TextBox control to the JavaScript function. How do I do it

ASP.NET

OnClick="PassVal('<%= TextBox1.ClientID %>')"  

JavaScript

<script type="text/javascript">          function PassVal(ctrl, e) {              alert(ctrl);          }  </script>  


Solution:1

If you are using Ms Ajax:

function Blablabla(){      var ctrl = $get('<%= TextBox1.ClientID %>');    }  

If you are using jQuery:

function Blablabla(){      var ctrl = $('#<%= TextBox1.ClientID %>');    }  

but basically it is:

function Blablabla(){      var ctrl = document.getElementById('<%= TextBox1.ClientID %>');    }  


Solution:2

you may modify the html that is output by accessing the Response.Body or similar. I'm not remembering the exact name.

you can then put a piece of script to it:

Response.Body += "<script type=\"text/javascript\">";  Response.Body += "    PassVal(" + myControl + "," + myE + ");"  Response.Body += "</script>"  

hope this helped you a bit. you have to look up the name of the property, don't got any VS here currently.

regards


Solution:3

This has been discussed before at this thread.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »