How to call Javascript function in C#

Introduction :

      Hi, calling JavaScript Function in C# is pretty simple, but we should know the situation to use it. in below examples i have coded you the situation and its usage with example. Calling Javascript function in C# will reduce your code.

Senario 1 : ClientScriptManager.RegisterStartupScript Method (Type, String, String, Boolean)
This method add the script block to the page which is executed before page’s OnLoad event raised.

Page.ClientScript.RegisterStartupScript(this.GetType(), "Fourthbottle", "MyFunction();", true);


Senario 2 :      ClientScriptManager.RegisterClientScriptBlock Method (Type, String, String)
  This method adds the script-block to the top of the rendered page

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Fourthbottle", " MyFunction();", true);


Senario 3 : RegisterStartupScript(Control, Type, String, String, Boolean) 
  This method registers the script block with script manager for the controls inside the update panel

ScriptManager.RegisterStartupScript(divFeatures, typeof(HtmlControl), " Fourthbottle ", " MyFunction();", true);


 Senario 4 :  RegisterStartupScript(Page, Type, String, String, Boolean)
Registers a Scriptblock with script manager for every asynchronous postback of the page

ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), " Fourthbottle ", " MyFunction ();", true);



Senario 5 :  RegisterClientScriptBlock(Control, Type, String, String, Boolean)

Registers a client script block with Script Manager for use with a control which is inside the Update Panel.

ScriptManager.RegisterClientScriptBlock(divFeatures, typeof(HtmlControl), "Fourthbottle", "MyFunction();", true);


Senario 6 :   RegisterClientScriptBlock(Page, Type, String, String, Boolean)


ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), " Fourthbottle ", "MyFunction();", true);