In ASP.NET, how to hook postback event on clientside

百般思念 提交于 2019-12-24 09:58:47

问题


I want to run a code on every postback on client-side. Is there an event or something to do that?


回答1:


If you just handle a form's submit event, you won't necessarily catch every __doPostBack(). A __doPostBack() that triggers an UpdatePanel refresh doesn't submit the form, for example.

One way that you can make sure you know about every call to __doPostBack() is to redefine it:

var oldDoPostBack = __doPostBack;

function __doPostBack(eventTarget, eventArgs) {
  // Your code here, which will execute before __doPostBack,
  //  and could conditionally cancel it by returning false.

  oldDoPostBack(eventTarget, eventArgs);
}

Just be sure to do that near the end of the page or in a document.ready type event, so you can be sure __doPostBack is defined when you attempt this.




回答2:


The way asp.net works is that the the whole page is a form. So to do something client side before the form is submitted (which is what the postback event really is), I would attach to the form submit event, return false to prevent the submission of the form, perform your client side code and then submit the form using javascript.

You can do this with jQuery easily: http://api.jquery.com/submit/

Let me know if you need more help.




回答3:


Dave Ward's answer almost worked for me, but for whatever reason (Telerik?), it created an infinite loop of calls to __doPostBack. So instead, I used the following code:

function __doPostBack_Custom(eventTarget, eventArgs) {
    // Your code here, which will execute before __doPostBack,
    //  and could conditionally cancel it by returning false.

    oldDoPostBack(eventTarget, eventArgs);
}
var oldDoPostBack = window.__doPostBack;
window.__doPostBack = __doPostBack_Custom;



回答4:


There is a javascript __doPostBack() method on every aspx page, that is generated by ASP.NET. So you can use it



来源:https://stackoverflow.com/questions/6290196/in-asp-net-how-to-hook-postback-event-on-clientside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!