Saturday, 1 June 2013

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




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

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

No comments:

Post a Comment