Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 () {

        });

    });
  }
});
like image 248
Mennan Avatar asked Mar 15 '12 11:03

Mennan


1 Answers

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.

like image 96
PraveenLearnsEveryday Avatar answered Oct 14 '22 09:10

PraveenLearnsEveryday