ASP.Net - Javascript inside AJAX UpdatePanel

后端 未结 4 1341
攒了一身酷
攒了一身酷 2020-12-08 12:14

I am running into an issue with running javascript from an external javascript file inside of an UpdatePanel. I am trying to get a color picker working inside of a ListView

相关标签:
4条回答
  • 2020-12-08 12:35

    After an asynchronous roundtrip, any startup scripts will not be run, which is likely why it doesn't work after the AJAX callback. The color picker likely has functions which need to be executed on page load.

    I've run into this so many times that I wrote a small method to register my scripts in the code-behind, which handles both async and non-async round trips. Here's the basic outline:

    private void RegisterClientStartupScript(string scriptKey, string scriptText)
    {
        ScriptManager sManager = ScriptManager.GetCurrent(this.Page);
    
        if (sManager != null && sManager.IsInAsyncPostBack)
        {
            //if a MS AJAX request, use the Scriptmanager class
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), scriptKey, scriptText, true);
        }
        else
        {
            //if a standard postback, use the standard ClientScript method
            scriptText = string.Concat("Sys.Application.add_load(function(){", scriptText, "});");
            this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), scriptKey, scriptText, true);
        }
    }
    

    I actually baked the above into a base page class so that any page I'm working with can call this.RegisterClientStartupScript(...). To do that, simply create a base page class and include it there (making sure to mark protected not private or your inheriting page classes won't be able access it).

    With the above code, I can confidently register client scripts regardless of whether the page is doing a postback or callback. Realizing you are using external script files, you could probably modify the above method to register external scripts rather than inline. Consult the ScriptManager class for more details, as there are several script registering methods...

    0 讨论(0)
  • 2020-12-08 12:43

    After looking at the jscolor source code, I noticed that it initializes everything on window load. So, you will probably need to re-init with something like this (inside the UpdatePanel):

    function yourInit(){
       /* keep in mind that the jscolor.js file has no way to determine
          that the script has already been initialized, and you may end
          up initializing it twice, unless you remove jscolor.install();
       */
    
       if (typeof(jscolor) !== 'undefined'){
          jscolor.init();
       }
    }
    if (typeof(Sys) !== 'undefined'){
       Sys.UI.DomEvent.addHandler(window, "load", yourInit);
    }
    else{
       // no ASP.NET AJAX, use your favorite event
       // attachment method here
    }
    

    If you decide to put the jscolor script inside the UpdatePanel, you will also need to add something like this to the end of the jscolor.js:

    if(Sys && Sys.Application){
       Sys.Application.notifyScriptLoaded();
    }
    
    0 讨论(0)
  • 2020-12-08 12:52

    I would guess that the jscolor.js code which runs to setup the color picker is only being called when your page first loads, so when the control is regenerated on the server, you lose the changes jscolor made. Could you register some javascript to be called in your code behind so that it would call the init method on jscolor when your asynch call completed?

    0 讨论(0)
  • 2020-12-08 12:53

    Have you tried ScriptManager.RegisterStartupScript which allows you to "adding JavaScript from the server to a page when performing an asynchronous postback" ?

    0 讨论(0)
提交回复
热议问题