回传后,我的JavaScript函数不工作,ASP.NET [英] After postback my JavaScript function doesn't work in ASP.NET

查看:114
本文介绍了回传后,我的JavaScript函数不工作,ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有共同的功能,我崩溃它在脚本文件夹CommonFunctions.js。



我有它在我的主页上,并用它在我的网页。当我做任何回发的网页上,我的功能不起作用。 (我的恩不好意思)



我CommonFunctions.js:

  $(函数(){

GF();

如果(Sys.WebForms.PageRequestManager.getInstance()get_isInAsyncPostBack()){

GF();
}


功能GF(){$​​ b
$ b $('AddNewGeneralPanel。)点击(函数(){$。 b
$ b如果($(本).find('。AddNewGeneralPanelStyle')。文本()==){
$(本).find('。AddNewGeneralPanelStyle')。文本( (Gizle));
lastOpenId = $(本).attr(kodid);
}
,否则
$(本).find('AddNewGeneralPanelStyle'。 )的.text();

$(本)。接下来('。AddNewGeneralAccordionDiv')的slideToggle('慢',函数(){

});

});
}
});


解决方案

由于您使用的是的UpdatePanel ,你已经连接你的事件处理程序都被丢弃和回发后重新创建DOM的一部分。这有删除的jQuery被装第一次加载页面时,任何事件处理程序的效果。



当你回发只是页面的一部分,jQuery的 $(函数(){}); 不火了,所以你的处理程序永远不会重新连接



下面是一个<一个HREF =http://stackoverflow.com/questions/256195/jquery-document-ready-and-updatepanels>有关问题演示了如何重新订阅的事件时,的UpdatePanel 刷新。


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 postback on a page, my function doesn't work. (sorry about my en)

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

        });

    });
  }
});

解决方案

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.

这篇关于回传后,我的JavaScript函数不工作,ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆