Tuesday, October 19, 2004

Avoid doPostBack and trap the caller object

(Evitar doPostBack y obtener el objeto que lo llama)

Sometimes you want to avoid the execution of the doPostBack, or make something before the doPostBack will be executed.

The following snippet do that. First We replace the __doPostBack with another variable called newPostBack, and maintain the old in another variable called oldPostBack.
When any object calls doPostBack, the function that will be executed will be newPostBack. In newPostBack function We can trap the caller object into the parameter eventTarget.
The ID of the object is btnDoit, and We get its outerHTML and do not execute the doPostBack.
If another object calls the doPostBack it will be executed normally.


<script id="anti_doPostBack" language="javascript">
<!--
// Replace the postback
oldPostBack = __doPostBack;
__doPostBack = newPostBack;

var oldPostBack

function newPostBack(eventTarget, eventArgument) {
if (eventTarget == "btnDoit") {
alert(document.getElementById('btnDoit').outerHTML);
} else {
//If the call to __doPostBack was NOT from btnDoit,
//then call the original __doPostBack.
oldPostBack(eventTarget, eventArgument);
}
}
//-->
</script>



Permalink: Avoid doPostBack and trap the caller object 

1 Comments:

At 7:19 PM, Anonymous Anonymous said...

I used your suggested code to replace doPostBack in my application, but it
seems window.onbeforeunload is called before, window.__doPostBack, so it can
not change the condition for showing warning message.
I am using datagrid with links as ButtonColumn but I do not want
onbeforeunload event called when they are clicked.

Any suggestion?

Thanks
Masoud

 

Post a Comment

<< Home