Reputation: 4497
I have common functions and I collapse it on CommonFunctions.js
in Scripts folder.
I include it on my master page and use it on my pages. When I do any post back on a page, my function doesn't work.
My CommonFunctions.js
:
$(function () {
gf();
if (Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {
gf();
}
function gf(){
$('.AddNewGeneralPanel').click(function () {
if ($(this).find('.AddNewGeneralPanelStyle').text() == "") {
$(this).find('.AddNewGeneralPanelStyle').text("( Gizle )");
lastOpenId = $(this).attr("codeid");
}
else
$(this).find('.AddNewGeneralPanelStyle').text("");
$(this).next('.AddNewGeneralAccordionDiv').slideToggle('slow', function () {
});
});
}
});
Upvotes: 16
Views: 52730
Reputation: 368
I had an interesting situation and figured I'd post my solution here in case it helped anyone in the future.
My problem was my OnClick events were firing on initial page load, but not after postback. My second problem was my OnClick fired once on initial page load, then more and more as postbacks went by.
<script type="text/javascript">
// function that adds my various OnClick events
function AddOnClicks() {
$('.myClass').off("click.highlight"); // .off added so the event doesn't fire multiple times after postbacks
$('.myClass').on("click.highlight", function () { // note: ".highlight" is just naming the event
// ... on click logic
});
}
$(document).ready(function () {
AddOnClicks(); // this works for initial page load only
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function () {
AddOnClicks(); // this works after postback
});
});
</script>
Why I needed to re-add the event on postback but also remove the previous one is beyond me. Hope this helps someone :)
Upvotes: 0
Reputation: 1
you can put all your JavaScript code in between following code.
function pageLoad(sender, args) {
}
Upvotes: -1
Reputation: 161
Faced same problem. Change
$(document).ready(function() {
to
Sys.Application.add_load(function() {
this will force it to run even on partial postbacck
Upvotes: 4
Reputation: 435
It is because of updatepannel
used. Following code is working fine. Just put your jquery
code inside the pageLoad
event like below
function pageLoad(sender, args) {
$(document).ready(function () {....}
}
Upvotes: 8
Reputation: 51
i have the same problem, i have solved with this code:
<script type="text/javascript">
function pageLoad()
{
$('#datetimepicker2').datetimepicker();
}
</script>
Upvotes: 4
Reputation: 23551
Assuming you are using an UpdatePanel to do the postback and the JS code is attatched to HTML elements within the update panel you need to attach them again when the update panel is loaded. You can hook up your code on the endRequest event:
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler)
http://msdn.microsoft.com/en-us/library/bb383810.aspx
Keep in mind that if you are using more than one update panel you should check which update panel is refreshed and attach only the events that are needed otherwise you risk events firing more than once.
Upvotes: 1
Reputation: 18351
Since you're using an UpdatePanel
, the part of the DOM that you've attached your event handler to is getting dropped and recreated after the postback. This has the effect of removing any event handlers that were attached by jQuery when the page first loaded.
When you postback only part of the page, the jQuery $(function() {});
doesn't fire again, so your handlers never get reattached.
Here's a related question that shows how to resubscribe your events when the UpdatePanel
refreshes.
Upvotes: 14
Reputation: 595
It is because of updatepanel partial postbacks. here is what you need to do.
function pageLoad(sender, args)
{
$(document).ready(function(){
// put all your javascript functions here
});
}
I had the same issue and it worked for me. I hope it helps you too.
Upvotes: 29