After postback my JavaScript function doesn't work in ASP.NET
Solution 1
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.
Solution 2
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.
Solution 3
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 () {....}
}
Solution 4
i have the same problem, i have solved with this code:
<script type="text/javascript">
function pageLoad()
{
$('#datetimepicker2').datetimepicker();
}
</script>
Solution 5
Faced same problem. Change
$(document).ready(function() {
to
Sys.Application.add_load(function() {
this will force it to run even on partial postbacck
Mennan
Updated on June 10, 2021Comments
-
Mennan almost 3 years
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 () { }); }); } });
-
PraveenVenu about 12 yearsAre you doing partial postback? I mean update panel with Conditional postback?
-
Rodrigo Vedovato about 12 yearsJust to confirm: is this EXACTLY the code you're using? I see a call to a "gf" function but a "gF" function is declared. Don't forget JS is case-sensitive
-
Mennan about 12 yearsyes i use update panel.it works with postback well when i use this javascript codes using <script>...<\script> on a page.But using a .js it doesnt working
-
Mennan about 12 yearssorry i write it manualy in there.edited it.
-
-
Josh Earl about 12 yearsI added a link that should help you work out how to resubscribe your function.
-
Mennan about 12 yearsthis code works on same page but i called it in masterpage.i try it but dont work for me
-
PraveenLearnsEveryday about 12 yearsdo you have scriptmanager tag in master page? if not put it there and try again.
-
Mennan about 12 yearsi solved this problem thx postback
-
dada over 8 yearsThank you @JoshEarl and thank you Mennan for this question. It really help me.
-
Si8 over 6 yearsThe one function that was needed. Thanks! +1