Call JavaScript function from C# Call JavaScript function from C# asp.net asp.net

Call JavaScript function from C#


If you want to call JavaScript function in C#, this will help you:

public string functionname(arg){    if (condition)    {        Page page = HttpContext.Current.CurrentHandler as Page;        page.ClientScript.RegisterStartupScript(            typeof(Page),            "Test",            "<script type='text/javascript'>functionname1(" + arg1 + ",'" + arg2 + "');</script>");    }}


This may be helpful to you:

<script type="text/javascript">    function Showalert() {        alert('Profile not parsed!!');        window.parent.parent.parent.location.reload();    }    function ImportingDone() {        alert('Importing done successfull.!');        window.parent.parent.parent.location.reload();    }</script>if (SelectedRowCount == 0){    ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "Showalert();", true);}else{    ScriptManager.RegisterStartupScript(this, GetType(), "importingdone", "ImportingDone();", true);}


.aspx file in header section

<head>    <script type="text/javascript">        <%=YourScript %>        function functionname1(arg1,arg2){content}    </script></head>

.cs file

public string YourScript = "";public string functionname(arg){    if (condition)    {        YourScript = "functionname1(arg1,arg2);";    }}