Execute JavaScript function from ASP.NET or codebehind C# code file

Execute  a Javascript function from c# code:




Calling a JavaScript function from codebehind is quiet simple, yet it confuses a lot of developers.

Here's how to do it. Declare a JavaScript function in your code as shown below:

JavaScript function on aspx page :

<head runat="server">

    <title>Call JavaScript From CodeBehind</title>

    <script type="text/javascript">

        function alertMe()
        {
            alert('Hello');
        }
    </script>

</head>

C# code for call javascript function on aspx.cs page :


protected void Page_Load(object sender, EventArgs e)

{

    if (!ClientScript.IsStartupScriptRegistered("alert"))

    {

        Page.ClientScript.RegisterStartupScript(this.GetType(),

            "alert", "alertMe();", true);

    }


}





Other Asp.net Related Post:


Comments