27 October, 2008

Calling JavaScript functions from C#

There is perhaps no way available to do this, nor should there ever be one. This is because your C# code runs in the code behind, a repository of code to handle post backs, while your JavaScript is executing on the client side. If at all client side javascript was let to be executed from C# code, then it would be similar to AJAX RPC's! A better way to call Javascript functions is by using hidden fields. The following logic demonstrates how:

|-->set a hidden field

|-->check the value of the hidden fields for changes, if any write logic which has to execute.

Reverse the above logic, and put the javascript hidden field check so that it executes when the page gets loaded, and VOILA, you have an answer to your solution.