Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.

10 June 2012

Call javascipt function from codebehind


In this snippet we will see how to call javascipt function from codebehind.

Example:
Designer
<head runat="server">
    <script type="text/javascript">
        function showMessage() {
            alert('invoked JS funciton showMessage');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">

    <div>
    <input type="button"  value="Call JS function from Designer"  onclick="showMessage()" />
    </div>
    </form>
</body>

Code Behind:
protected void Page_Load(object sender, EventArgs e)
    {
       Page.ClientScript.RegisterStartupScript(GetType(), "Key", "showMessage();", true);
    }

Output: 'invoked JS funciton showMessage' will be invoked after loading the page or onclick of button.

0 Comments:

Post a Comment