Call JavaScript function from c# Code behind (Server Side) in Asp.net
In this asp .net tutorial we will learn how to call JavaScript function from asp .net code behind. We will cover following scenarios in this tutorial: Call JavaScript function from asp .net code behind. Call JavaScript function from asp .net code behind and pass values to the function. Create functions in JavaScript
Explanation: First function "showAlert" will display alert message box and second function "sumValues" will take 2 arguments and displays their sum in alert box.
Call JavaScript function without arguments.
protected void btnShowAlert_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "AnyValue", "showAlert();", true);
}
Call JavaScript function with arguments.
protected void btnPassValues_Click(object sender, EventArgs e)
{
int a = 10;
int b = 20;
ScriptManager.RegisterStartupScript(this, GetType(), "AnyValue", "sumValues(" + a + "," + b + ");", true);
}
Final Output