简体   繁体   中英

Execute javascript after a partial postback of an updatepanel?

I have a page that add tree file script to it .

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/init.js"></script>
<script type="text/javascript" src="js/easing.js"></script>

I have a updatepanel with a dropdownlist. When run SelectedIndexChanged event (partial postback of an updatepanel), don't execute javascript .

Use the pageLoad function:

function pageLoad(sender, args) {
  InitialiseSettings();
}

function InitialiseSettings(){
    // replace your DOM Loaded settings here. 
    // If you already have document.ready event, 
    // just take the function part and replace here. 
    // Not with document.ready 
    $(element).slideUp(1000, method, callback});

    $(element).slideUp({
                   duration: 1000, 
                   easing: method, 
                   complete: callback});
}

Or, try adding an "end request" event handler with .add_endRequest() :

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(InitialiseSettings)

Edit:

It would be a better idea for you to move your code from document.ready into InitialiseSettings() , and to then register it as a pageLoaded event handler.

Code Example

 Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(InitialiseSettings)

To run your javascript in full and partial postbacks, put your javascript code into javascript pageLoad() function.

function pageLoad()
{
   //your javascript code
}

Example:

function pageLoad() {

    $(':submit').click(function () {
        CommodityArray();
    });
    $('#btn_image').click(function () {
       CommodityArray();
    });
    $(".repHeader").disableSelection();

    CommodityArray();
}

Hope it helps! :)

You have to use following code after your update panel.

<script type="text/javascript" language="javascript">
var pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
pageRequestManager.add_endRequest(NewCharacterCount);
</script>

where NewCharacterCount is your javascript function name.

Read this article Sys.WebForms.PageRequestManager endRequest Event Hope it may help you.

If you are using UpdatePanel and you want to call a javascript function after content refresh in update panel, you can use below way to do it easily. In Page body tag , call a function RefreshContent()

<body onload="RefreshContent()">

<script type="text/javascript">
  function RefreshContent()
  {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
  }

  function EndRequestHandler()
  {
    alert("Add your logic here" );
  }
</script>

Reference link http://www.infoa2z.com/asp.net/how-to-call-javascript-function-after-an-updatepanel-asychronous-request-to-asp.net-page

You can use PageRequestManager client events . The sender parameter will contain the information you need. For example one could do this:

// Register event handler
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);

/// Executed when all page content is refreshed, full page or async postback: https://msdn.microsoft.com/en-us/library/bb397523.aspx
function pageLoaded(sender, args) {
    var isPostBack = sender.get_isInAsyncPostBack();
    if(!isPostBack) return;

    // PostBack logic here.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM