Call JavaScript function from code behind in c#

Many times we come across situations where we need to call JavaScript functions from code-behind.
This can be achieved by registering ClientScript block.
Let's take a look by an example.

JavaScript function :
1
2
3
4
5
<script type="text/javascript">
        function WelcomeUser(username) {
            alert("Welcome::" + username);
        }
    </script>
Code behind :

1
2
3
4
5
protected void Page_Load(object sender, EventArgs e)
{
     ClientScript.RegisterStartupScript(GetType(), 
     "hwa", "WelcomeUser('VJ');", true);
}



This will call Welcomeuser method from code behind.

No comments:

Post a Comment