How to call java script function from code behind file in asp.net

Introduction: In previous articles i explained how to  show Message box in asp.net website using JavaScript and how to redirect visitor from one website to another website using java script  and How to show Validation guidelines in web forms using JavaScript in Asp.net ? and Example to implement Jquery form validations and jQuery to Validate email address using RegularExpression and JavaScript validations in asp.net website 
 In this article i am going to explain how to call JavaScript function from code behind file.It is required many a times while working on asp.net application. Lets take an example:
  •  Declare a JavaScript function in the head tag of your design file as shown below:
<head runat="server">
    <title>Calling JavaScript From CodeBehind in asp.net file</title>
    <script type="text/javascript">
        function MyFunction() {
            alert('Demonstration Successfull..');
        }

    </script>
</head>
  • In order to call it from code behind, use the following code in your Page_Load
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 in asp.net


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

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

4 comments

Click here for comments
Anonymous
March 16, 2013 ×

very useful....keep the nice writing

Reply
avatar
admin
Anonymous
September 06, 2013 ×

Whoooo!

Reply
avatar
admin
August 27, 2014 ×

very gooddddddddddd

Reply
avatar
admin
August 28, 2014 ×

Thanks mahesh..it is always nice to hear that my articles helped anyone..stay connected and keep reading for more useful updates like this..:)

Reply
avatar
admin

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. Out Of Topic Show Conversion CodeHide Conversion Code Show EmoticonHide Emoticon

Thanks for your comment