Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascripts not working after postback [closed]

I'm working with bootstrap components on a asp.net web application. It's works fine, but curiously, they stop working after a page postback. The appearance is fine, but the behavior isn't. That's why i supose that the problem envolves the javascript files.

For example, a dorp down button: after postback, the expand action doesn't work.

Do you have an idea what could be the cause of this?

like image 860
Leandro Faria Avatar asked May 27 '26 14:05

Leandro Faria


2 Answers

If you are using an UpdatePanel to do a partial page refresh, then when the contents of the panel are updated via AJAX, the usual $(document).ready(...) / $(...) stuff does not fire again, which means that the custom JS functionality (like the dropdowns) that is part of Bootstrap does not get re-bound to the new elements.

What I usually do is extract my code that sets up the javascript event handlers and behaviour into their own function (e.g. function BindEvents() { ... }), and then I use the following code inside the update panel to ensure it gets re-run after the partial postback (as well as on the initial page load):

<script type="text/javascript">
    Sys.Application.add_load(BindEvents);
</script>

EDIT: Here is a suggested basic BindEvents() function that should re-initialise the dropdowns for you:

function BindEvents() {
    $('[data-toggle=dropdown]').dropdown();
    // other bootstrap binding code - see the combined Bootstrap.js for ideas
}
like image 72
GregL Avatar answered May 30 '26 06:05

GregL


If you use this line of code, anything in the endRequestHandler should fire everytime an update panel updates.

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);

        function endRequestHandler(){

             // initialize controls here...

        }
like image 36
Korv Avatar answered May 30 '26 06:05

Korv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!