External JavaScript added by ScriptManager.RegisterStartupScript only executes once

Last post 02-28-2008 9:34 AM by timvdh. 14 replies.

Sort Posts:

  • External JavaScript added by ScriptManager.RegisterStartupScript only executes once

    02-27-2008, 8:41 AM
    • Loading...
    • timvdh
    • Joined on 10-23-2007, 11:46 AM
    • Posts 27

    Hi, 

    I want to reference an external script and execute it on every postback (asyn or not). The script must be executed "as is", I cannot bracket a function around it. I tried this:

    ScriptManager.RegisterStartupScript(

    this.Page,

    typeof(Page),

    string.Format("StartupInclude"),

    @"<script src="http://xyz.com/scripts/runme.js" type=""text/javascript""></script>",

    false);

    The code in runme.js is only executed on full postbacks, not on partial page updates.

    If I use the function to register a piece of js code:

    ScriptManager.RegisterStartupScript(

    this.Page,

    typeof(Page),

    string.Format("StartupScript"),

    "alert('HitMe!')",

    false);

    Then the js code will be executed on every update, as desired. Is there a way to execute external js files just like this?

    Thanks, regards

    DC

  • Re: External JavaScript added by ScriptManager.RegisterStartupScript only executes once

    02-27-2008, 9:04 AM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 8,686

    Read your post again and see if you can decipher what you are wanting. If you only want the script to run on PostBacks, place the call inside of an if ( IsPostBack ) qualifier. Example:

    if ( IsPostBack )
    {
     ClientScriptManager.RegisterStartupScript(...
    }

    You also probably should use ClientScriptManager instead of ScriptManager in that case as ScriptManager is used for AJAX.

    NC...

  • Re: External JavaScript added by ScriptManager.RegisterStartupScript only executes once

    02-27-2008, 9:18 AM
    • Loading...
    • timvdh
    • Joined on 10-23-2007, 11:46 AM
    • Posts 27

    Thank you. What I want is that runme.js is being executed on every page update - partial or not. Currently it is only being executed on first load.

    Example runme.js:

    <!--

    alert('RunMe!');

    //-->

    ScriptManager.RegisterStartupScript(

    this.Page,

    typeof(Page),

    "StartupCode",

    @"runtest();",

    false);

    One solution to this is to make runme.js something like this:

    <!--

    function runtest {

    alert('RunMe!');

    }

    //-->

    and register this:

    ScriptManager.RegisterStartupScript(

    this.Page,

    typeof(Page),

    string.Format("StartupInclude"),

    @"<script src="http://xyz.com/scripts/runme.js" type=""text/javascript""></script>",

    false);

    Function "runtest" will now be executed on every update - as desired. But I cannot modify the external runme.js file.

    Regards

    Tim

  • Re: External JavaScript added by ScriptManager.RegisterStartupScript only executes once

    02-27-2008, 9:33 AM

    Add the following to your javascript file.  The pageLoaded event of the PageRequestManager fires on every postback, synchronous and asynchronous. 

    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_pageLoaded(PageLoadedHandler);
    function PageLoadedHandler(sender, args) {
        //Code that you want to happen on every postback.
        //Asynch or Synch.
        alert('Hello.');
    }
     
    When you ask a question, remember to click "mark as answered" when you get a reply which answers your question.


    My latest ASP.NET AJAX blog entries.
  • Re: External JavaScript added by ScriptManager.RegisterStartupScript only executes once

    02-27-2008, 9:43 AM
    • Loading...
    • timvdh
    • Joined on 10-23-2007, 11:46 AM
    • Posts 27

    Thank you. I think this will not do what I want. I cannot modify the external .js (in my example runme.js) file and the file does not contain a function I can call, it is just plain JavaScript which must be executed from line 1.

    Regards

    Tim

  • Re: External JavaScript added by ScriptManager.RegisterStartupScript only executes once

    02-27-2008, 9:47 AM

    Then, place the code I gave you on the page and call whatever you want to from within that.

    When you ask a question, remember to click "mark as answered" when you get a reply which answers your question.


    My latest ASP.NET AJAX blog entries.
  • Re: External JavaScript added by ScriptManager.RegisterStartupScript only executes once

    02-27-2008, 9:57 AM
    • Loading...
    • timvdh
    • Joined on 10-23-2007, 11:46 AM
    • Posts 27

    The problem is: I cannot call the code in the external Javascript file since it does not contain a function. I also cannot change the code and I must load it the url since it may be changed externally. I could not find an indication in your code example on how to reference the external file which must be executed on every update.

  • Re: External JavaScript added by ScriptManager.RegisterStartupScript only executes once

    02-27-2008, 10:33 AM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 8,686

    Adding it as an include doesn't work?
    string javaScript = "<script src='http://xyz.com/scripts/runme.js' type='text/javascript'></script>";
    RegisterStartupScript(..., javaScript);

    NC...

  • Re: External JavaScript added by ScriptManager.RegisterStartupScript only executes once

    02-27-2008, 11:42 AM
    • Loading...
    • timvdh
    • Joined on 10-23-2007, 11:46 AM
    • Posts 27

    That's exactly what I thought should work, but it does not. The included script is not being executed on partial page / updatepanel updates, only on non-ajax postbacks.

    Regards
    Tim

  • Re: External JavaScript added by ScriptManager.RegisterStartupScript only executes once

    02-27-2008, 12:31 PM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 8,686

    Have you tried using ScriptManager instead of ClientScriptManager to register the script? Remember that the Page_Load handler only gets executed on a full refresh.

    I think that what you're going to have to do is in Page_Load use ClientScriptManager and in your AJAX code use ScriptManager.

    Other than that, I'm afraid that I can't help you as I just don't use that type of functionality.

    NC...

  • Re: External JavaScript added by ScriptManager.RegisterStartupScript only executes once

    02-27-2008, 12:53 PM
    • Loading...
    • timvdh
    • Joined on 10-23-2007, 11:46 AM
    • Posts 27

    Yes, I am using the ScriptManager, what I am doing in Page_Load is this:

    ScriptManager.RegisterStartupScript(
    this.Page,
    typeof(Page),
    "startupinclude",
    @"<script src="http://xyz.com/runme.js"" type=""text/javascript""></script>"
    false);

    I checked if Page_Load is not being executed on ajax callbacks and found that it is always executed.

    Regards
    Tim

  • Re: External JavaScript added by ScriptManager.RegisterStartupScript only executes once

    02-27-2008, 2:20 PM
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 8,686

    timvdh:

    Yes, I am using the ScriptManager, what I am doing in Page_Load is this:

    ScriptManager.RegisterStartupScript(
    this.Page,
    typeof(Page),
    "startupinclude",
    @"<script src="http://xyz.com/runme.js"" type=""text/javascript""></script>"
    false);

    I checked if Page_Load is not being executed on ajax callbacks and found that it is always executed.

    Regards
    Tim

    Let me repeat myself. In Page_Load, USE ClientScriptManager.

    And as for Page_Load being executed on AJAX Callbacks, I think that you are mistaken. Page_Load is ONLY executed on a FULL PostBack. At least that is the way that it works for me, but then I am not using the standard AJAX library. But I don't see how it could. That would negate the entire AJAX effort.

    NC...

  • Re: External JavaScript added by ScriptManager.RegisterStartupScript only executes once

    02-27-2008, 4:51 PM
    Answer

    NC01:

    And as for Page_Load being executed on AJAX Callbacks, I think that you are mistaken. Page_Load is ONLY executed on a FULL PostBack.

    Actually, the page does execute the entire page lifecycle on asynchronous postbacks. 

    However, the cause of the problem is that basically, once the script is registered on the page, it cannot be registered again.  That is why using ScriptManager.RegisterStartupScript, ClientScript.RegisterStartupScript, and Page.RegisterStartupScript will only call the javascript on the initial load.  It's like writing <script type="text/javascript" src="JScript.js" /><script type="text/javascript" src="JScript.js" /><script type="text/javascript" src="JScript.js" /> over and over in your document.

    What you need to do, is clear the script element from the page, so that it can be readded upon asynchronous postbacks.  Add the following on your page (changing the filename & path accordingly): 

    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <script type="text/javascript">
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_pageLoaded(PageLoadedHandler);
    
        function PageLoadedHandler(sender, args) {
            var allScriptTags = document.getElementsByTagName("script");
            for (i = allScriptTags.length; i >= 0; i--) {
                if (allScriptTags[i] && allScriptTags[i].getAttribute("src")!=null && allScriptTags[i].getAttribute("src").indexOf("JScript.js") != -1) {
                    allScriptTags[i].parentNode.removeChild(allScriptTags[i]);
                }
            }
        }
    </script>
     Then place this in your Page_Load event (again changing the filename and path to what you need): 
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            ScriptManager.RegisterStartupScript(Page, Page.GetType, "", "<script type='text/javascript' src='JScript.js' />", False)
        End Sub
     
    When you ask a question, remember to click "mark as answered" when you get a reply which answers your question.


    My latest ASP.NET AJAX blog entries.
  • Re: External JavaScript added by ScriptManager.RegisterStartupScript only executes once

    02-28-2008, 5:44 AM
    • Loading...
    • timvdh
    • Joined on 10-23-2007, 11:46 AM
    • Posts 27

    I am quiet sure that you are using native ajax then, e.g. pure javascript calling a webservice - not the page the request originated from. That of course would not run throught the page lifecycle, but the UpdatePanel partial page request functionality does.

    Regards
    Tim

  • Re: External JavaScript added by ScriptManager.RegisterStartupScript only executes once

    02-28-2008, 9:34 AM
    • Loading...
    • timvdh
    • Joined on 10-23-2007, 11:46 AM
    • Posts 27

    DisturbedBuddha:

    NC01:

    And as for Page_Load being executed on AJAX Callbacks, I think that you are mistaken. Page_Load is ONLY executed on a FULL PostBack.

    Actually, the page does execute the entire page lifecycle on asynchronous postbacks. 

    However, the cause of the problem is that basically, once the script is registered on the page, it cannot be registered again.  That is why using ScriptManager.RegisterStartupScript, ClientScript.RegisterStartupScript, and Page.RegisterStartupScript will only call the javascript on the initial load.  It's like writing <script type="text/javascript&quo