开发者

Which executes first postback or jquery event

I have a button which retrieves in codebehind some value from Database . The button itself has a click event handled by Jquery . The problem is 开发者_如何转开发I need to use the retrieved value from database in the jquery click action .

So, Is that possible ?


It's not possible.

The events bound via jQuery will get executed first, only after that the page is posted back.

You may have to use ajax to get value from your server.

Or

You can store your value in a hiddenfield from code behind,and in your javascript code...

$(function(){
   if($("#hiddenfieldID").val())
   {
    //transfer control to wherever you want
   }
});


Calling ajax method will not wait for the response, so the function in click() even will quit before you receive the response from server. But you can refactor your code a bit to get the job done:

$button.click(function(){
   $.get('your-url', function(dataFromDb){
      // do something with dataFromDb
      alert('Got the response from server!');
   });
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜