After postback my JavaScript function doesn't

2020-08-10 06:20发布

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("kodid");
        }
        else
            $(this).find('.AddNewGeneralPanelStyle').text("");

        $(this).next('.AddNewGeneralAccordionDiv').slideToggle('slow', function () {

        });

    });
  }
});

6条回答
乱世女痞
2楼-- · 2020-08-10 06:40

Faced same problem. Change

$(document).ready(function() {

to

Sys.Application.add_load(function() {

this will force it to run even on partial postbacck

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-08-10 06:41

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.

查看更多
Explosion°爆炸
4楼-- · 2020-08-10 06:44

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 () {....}
}
查看更多
一夜七次
5楼-- · 2020-08-10 06:52

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.

查看更多
姐就是有狂的资本
6楼-- · 2020-08-10 07:00

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.

查看更多
甜甜的少女心
7楼-- · 2020-08-10 07:02

i have the same problem, i have solved with this code:

<script type="text/javascript">
    function pageLoad()
    {
        $('#datetimepicker2').datetimepicker();           
    }
</script>
查看更多
登录 后发表回答