Monday, August 19, 2013

Call javascript function in code behind using c#.Net


Some time we need to call javascript function in .CS(Code behind) page. The below example will help you to understand the calling javascript function in code behind.  In .CS file you need to register the startup script using script manager. Below example I have register in two ways.


In ASPX.CS File

ScriptManager.RegisterStartupScript(Page, this.GetType(), "callFunctionStartupScripts", "loadMyFunction();", true);

(OR)

string command = "loadMyFunction();";
string script = "";
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "showsmyfunction", script, false);


In the design page (.ASPX) you have to create Javascript function loadMyFunction. While running the program the loadMyFunction will fire.

In ASPX file
<script type="text/javascript">
function loadMyFunction(container) {
try   {
      alert('Test');
} catch (ex) {  }

</script>

No comments: