我正在使用jQuery在UpdatePanel内的元素上连接一些鼠标悬停效果。事件绑定在$(document)中。准备好了。例如:

$(function() {    
    $('div._Foo').bind("mouseover", function(e) {
        // Do something exciting
    });    
});

当然,这在页面第一次加载时工作得很好,但是当UpdatePanel进行部分页面更新时,它不会运行,鼠标悬停效果在UpdatePanel内部也不再工作。

在第一个页面加载时,以及每次UpdatePanel触发部分页面更新时,建议使用什么方法来连接jQuery中的内容?我应该使用ASP。NET ajax生命周期代替$(document).ready?


当前回答

Sys.Application.add_load(LoadHandler); //This load handler solved update panel did not bind control after partial postback
function LoadHandler() {
        $(document).ready(function () {
        //rebind any events here for controls under update panel
        });
}

其他回答

pageLoad = function () {
    $('#div').unbind();
    //jquery here
}

pageLoad函数非常适合这种情况,因为它在初始页面加载和每次updatepanel异步回发时运行。我只需要添加unbind方法,使jquery工作在updatepanel回发。

http://encosia.com/document-ready-and-pageload-are-not-the-same/

对于在我的情况下的其他人,我试图让jquery文档准备函数为DevExpress ASPxCallbackPanel工作,上面没有(到目前为止)工作。这是对我有效的方法。

<script>
function myDocReadyFunction(){ /* do stuff */  }
</script>

<dx:ASPxCallbackPanel ID="myCallbackPanel" ... >
    <ClientSideEvents EndCallback="function(){ myDocReadyFunction();}"> 
    </ClientSideEvents>
    <PanelCollection ...>
</dx:ASPxCallbackPanel>

我会使用以下方法之一:

将事件绑定封装在函数中,并在每次更新页面时运行它。您总是可以包含到特定元素的事件绑定,这样就不会将事件多次绑定到相同的元素。 使用livequery插件,它基本上自动执行方法一。根据希望对事件绑定的控制程度,您的偏好可能有所不同。

使用下面的脚本并相应地更改脚本正文。

       <script>
        //Re-Create for on page postbacks
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_endRequest(function () {
           //your codes here!
        });
    </script>

当美元(文档)。ready(function(){…})不工作后页面发布回来,然后使用JavaScript函数pageLoad在Asp。页面如下:

<script type="text/javascript" language="javascript">
function pageLoad() {
// Initialization code here, meant to run once. 
}
</script>