Various methods to call javascript function from code behind | Coding Cluster - using, c#, mvc 4, iphone, php, ios, javascript, in mvc 3 & more

Various methods to call javascript function from code behind


Execute JavaScript function from ASP.NET code behind using c#:
                                              Calling a JavaScript function from codebehind is quiet simple. Here's how to do it. You can use ClientScript.RegisterStartupScript() to do what you want, like this:

 Declare a JavaScript function in your code as shown below.

<head runat="server">
    <title>Call JavaScript From CodeBehind</title>
    <script type="text/javascript">
        function fnSample() {
In order to call it from code behind, use the following code in your Page_Load

C# method 1:
protected void Page_Load(object sender, EventArgs e)
    if (!ClientScript.IsStartupScriptRegistered("alert"))
            "alert", "fnSample();", true);

C# method 2:
protected void Page_Load(object sender, EventArgs e)
   ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "fnSample", "fnSample();", true);

C# method 3:

 protected void btnSubmit_Click(Object sender, EventArgs e)
        var script = "alert('Hai codingcluster');";
        ClientScript.RegisterStartupScript(typeof(Page), "MyAlert", script, true);
The format is (type, scriptKey, scriptText, wrapItInScriptTags).

The Page.ClientScript.RegisterStartupScript() allows you to emit client-side script blocks from code behind.

That's all. If this post is useful for you , please share this knowledge to your friends. Thanks!


Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

Great delivery. Outstanding arguments. Keep up the great effort.
My page > dental implants problems

Anonymous said...

Good post. one more post like this

Rasim Serim said...

very userful.thanks...

faraz ahmad said...

thanks a lot .. u can find java dumps here...

Post a Comment

Share your thoughts here...




Blogger Widgets