Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Wednesday, 4 February 2015

Introduction:

In this article We will learn how to call JavaScript function from code behind file.It is required many a times while working on asp.net application.
  •  Declare a JavaScript function in the head tag of your design file as shown below:
<head runat="server">
    <title>JavaScript function from CodeBehind in asp.net </title>
    <script type="text/javascript">

        function MyFunction() {
            alert('Function is working fine.');
        }
    </script>
</head>

C#.Net Code to call java script function from code behind file in asp.net

protected void Page_Load(object sender, EventArgs e)
{
    if (!ClientScript.IsStartupScriptRegistered("alert"))
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(),
            "alert", "MyFunction();", true);
    }
}

VB.Net Code to call java script function from code behind file

If (Not ClientScript.IsStartupScriptRegistered("alert")) Then
    Page.ClientScript.RegisterStartupScript _ 
    (Me.GetType(), "alert", "MyFunction();", True)
End If

0 comments:

Post a Comment