1

I have a javascript function in the External JS File. I want to call it from the co0de behind. How can I do that ? I read somewhere that I can do from ScriptManager.RegisterStartupScript but how, can anyone explain me.

JS File:

    Function1()
    {
      alert("came");
     //Some More logic
    }

Update

Calling from

//Tried this But NOT WORKING
 protected void Page_PreRender(object sender, EventArgs e)
 {
      ScriptManager.RegisterStartupScript(this, GetType(), "Function1", "Function1();", true);
     Page.ClientScript.RegisterStartupScript(GetType(), "Function1", "Function1()", true);

 }
1

5 Answers 5

1

I've often used this

string script = string.Format("alert('{0}');",alert);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);

basically, it just inserts that line onto your page and runs it as soon as the page loads

Sign up to request clarification or add additional context in comments.

5 Comments

define alert as what ever variable you want to see show in the alert
My Function1() is containing some big logic, which I want to reuse also.So I need to keep in the EXt JS FIle.
is Function1() in the code behind? It's a bit trickier to communicate from js-->c#. If that's the case it's easiest to use an ajax function from js
are you trying to pass your entire function to the page? that won't work
I am just trying to call the function, not pass it.
0

To strictly answer your question you are looking for something like this:

//Following statement is used to call pre-defined javascript function
protected void btnServerSide_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this, GetType(), "myFunction", "myFunction();", true);            
}

Here is a site that provides some more details and examples

MSDN also has a great walkthrough with multiple examples

This is my favorite of the walkthroughs listed above, it is very comprehensive

1 Comment

Probably because your trying to call javascript before it is even processed, notice how mine fires off of a button click and you have yours within a prerender function
0

you can try

ClientScript.RegisterStartupScript(GetType(),"CallMyFunction","Function1();",true);

OR

ClientScript.RegisterStartupScript(this,this.GetType(),"CallMyFunction",
                                     "Function1();",true);

another approach is

ScriptManager.RegisterStartupScript(Page, typeof(Page), "somekey", script, true);

this will works during partial postbacks also.

2 Comments

@hellotemp11 try this.GetType() instead of this, GetType()
@hellotemp11 also try by putting an alert instead of function1() place and check is it working
0

sample code, which will include javascript file and then call method from the code behind.

MyJSFile.js

function Test() {
    alert("hi");
}

WebForm1.aspx.cs -

 using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace CodeprojectTest
{
    public partial class WebForm1 : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var js = new HtmlGenericControl("script");
            js.Attributes["type"] = "text/javascript";
            js.Attributes["src"] = "JScript1.js";
            Page.Header.Controls.Add(js);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "Test();", true);
        }
    }
}

1 Comment

Is the "script" and "myScript" should be same ? Why have you used those 2 variables here ?
-1

You may try this..

Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "CallMyFunction", "myFunction()", true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.