How To Call JS Function In C#

Call JS Function In C#

With a Web application, javascript and jquery functions play an important role when we want to send values to the server side.

This article will discuss in the Asp.net Web application how calls JavaScript/Jquery Functions on the server side.

For this, we must create WebForms in Visual Studio, which is supported by .Net framework 4.5.

We can call the javascript function in c# by using the RegisterStartupScript method in ScriptManager and ClientScript class from the System. Web. UI namespace. But both classes can be accessed from WebForms only, not by normal classes, which means .aspx pages.

By using ScriptManager class,

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(),
                      "loadJSFunction();", true);

By using ClientScript class,

ClientScript.RegisterStartupScript
            (GetType(), Guid.NewGuid().ToString(), "loadJSFunction();", true);

By using param,

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(),
                      "loadJSFuncParam('" + param + "');", true);

JS Function,

<script type="text/javascript">
function loadJSFunction() {
    alert("Hi Load JS")
}
function loadJSFuncParam(param) {
    alert(param)
}
</script>


Similar Articles