开发者

postback problem when working with ajax updatepanel and jquery together

开发者 https://www.devze.com 2023-02-12 07:19 出处:网络
I am working on asp.net and jquery. I used updatepane开发者_运维百科l and jquery together. Enerything is fine. But sometimes face postback problem. Suppose after running a page i test some jquery call

I am working on asp.net and jquery. I used updatepane开发者_运维百科l and jquery together. Enerything is fine. But sometimes face postback problem. Suppose after running a page i test some jquery call which call pagemethod of the same page. After some event the submitt button is not working. I press several times but it do nothing. I am unable to find the problem.

Please help.


When you hook up your events in jquery they are tied to dom elements that are replaced by the update panel. Hence the events you hooked up are lost as their dom element is destroyed by the update panel's replacement elements.

You can use $("MySelector").live('click',function(){ alert('hi')});

It works by attaching an event handler for mouse click at the root dom node of your page that runs when the original source meets the selector criteria. Events will bubble up the dom until they are handled and canceled. This is a really handy tool for dealing with any code where you are dynamically replacing or adding buttons. It is particularly helpful when you are unfortunate enough to have to deal with update panels.

You can also look into .delegate. That would let you define the function at a parent node in the dom that is closer (thus having the event bubble up less and giving you more efficiency). I have always been fine with just using .live


If the jQuery that is tied to the button click is wrapped in a $(document).ready it will loose the jQuery action that was tied to it on an update panel postback. Microsoft asp.net Ajax automatically calls a function called pageLoad, so do the following.

Set it up like this :

pageLoad(){ $('#button1').click(function(){ //do something }); }

Not this :

$(document).ready(function(){ $('#button1').click(function(){ //do something }); });

0

精彩评论

暂无评论...
验证码 换一张
取 消