how to keep javascripts after UpdatePanel partial

2019-02-25 09:12发布

问题:

There is a lot of code so I want to explain the issue shortly in words. I have a Master page, that includes all references to javascript files and have the pages that use the master page. I use update panel in my pages and inside the update panel there are some forms including that one-has postback capability(ie dropdownlist). The problem is when the state changes and a partial postback occurs, the forms which has some capabilities and effects because of those javascripts, then lose all the functionality. Any help would be appreciated.

回答1:

This is because after the partial post back with the Update panel you need to re-initialize the javascript. Here is a general example

<script type="text/javascript"> 
   // if you use jQuery, you can load them when dom is read.
   $(document).ready(function () {
       var prm = Sys.WebForms.PageRequestManager.getInstance();    
       prm.add_initializeRequest(InitializeRequest);
       prm.add_endRequest(EndRequest);

       // Place here the first init
    });        

    function InitializeRequest(sender, args) {
    }

    function EndRequest(sender, args) {
       // after update occur on UpdatePanel re-init what you need

    }
</script> 

Relative questions:
Asp.Net UpdatePanel in Gridview Jquery DatePicker
How to get Id update panel that initial a request in javascript

After the net 4 you can also simple use the pageLoad function, is similar to the OnLoad function, but is handled by asp.net and is called on the first page load, but also called after each ajax update.

function pageLoad()
{
    // init here your javascript
    // This is called when the page load first time
    //  and called again each time you have an Update inside an UpdatePanel
}

reference: http://msdn.microsoft.com/en-us/library/bb386417(v=vs.100).aspx