After postback my JavaScript function doesn't work in ASP.NET

49,294

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

Share:
49,294
Mennan
Author by

Mennan

Updated on June 10, 2021

Comments

  • Mennan
    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
      PraveenVenu about 12 years
      Are you doing partial postback? I mean update panel with Conditional postback?
    • Rodrigo Vedovato
      Rodrigo Vedovato about 12 years
      Just 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
      Mennan about 12 years
      yes 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
      Mennan about 12 years
      sorry i write it manualy in there.edited it.
  • Josh Earl
    Josh Earl about 12 years
    I added a link that should help you work out how to resubscribe your function.
  • Mennan
    Mennan about 12 years
    this code works on same page but i called it in masterpage.i try it but dont work for me
  • PraveenLearnsEveryday
    PraveenLearnsEveryday about 12 years
    do you have scriptmanager tag in master page? if not put it there and try again.
  • Mennan
    Mennan about 12 years
    i solved this problem thx postback
  • dada
    dada over 8 years
    Thank you @JoshEarl and thank you Mennan for this question. It really help me.
  • Si8
    Si8 over 6 years
    The one function that was needed. Thanks! +1