Suresh Behera

The Microsoft .Net Junkies

News

Blogroll

Reading

How to Override the __doPostBack function ?

Well it sometime things are looks difficult when you work simple traditional way.Sometime back i was trying to submit server data from JavaScript confirm message box. I did by calling __doPostBack function in asp.net.Which looks something like this
btnSuresh.Attributes.Add("onclick", "if(!confirm('Are you sure you want to delete?')){return false;}__doPostBack();");

On other hand, It is also sometime good to overwrite override the __doPostBack function and write your own javascript code.

<script language="javascript">
var oldPostBack
function window.onload()
{
   oldPostBack = __doPostBack; 
   __doPostBack = MyFuntion;
}
function MyFuntion() 
{
    //Write you own code 
}
</script>

For More
http://www.dotnet247.com/247reference/msgs/15/77421.aspx
http://weblogs.asp.net/vga/archive/2004/03/01/NoMoreHijackingOfDoPostBackInWhidbey.aspx

Cheers..

Suresh Behera

 

Comments

Fabrice said:

You can use the following much simpler code for a confirmation:
btnSuresh.Attributes.Add("onclick", "return confirm('Are you sure you want to delete?')");
# August 4, 2005 10:49 AM

Karl said:

And build on fabrice's idea and create your own DeleteButton control which inherits from Button and does just that.
# August 4, 2005 10:56 AM

Dave said:

To override a function in javascript, just rewrite the function beneath it. Javascript won't complain about two signatures being the same and the last one in always wins. And no, its not anomaly. Its designed that way.

If you call the init function below you will always see the message "2".

function init(){
alert('1');
}

function init(){
alert('2');
}

# August 4, 2005 2:57 PM

Fabrice said:

Ok Dave, but how do you call the first init from the second?
# August 5, 2005 5:32 AM