Hijacking __doPostBack

I’ve often found myself wanting to do run some javascript before my form posts to the server, which is rather difficult to do when you have a .net server form.  It always auto generates that __doPostBack javascript method that submits the form. Placing an “onsubmit” handler on the form doesn’t help because that event doesn’t fire when you submit a form using javascript.

I cam across this post a little bit ago that solved the problem perfectly.  Its so simple, I’m embarrassed that I didn’t realize it before.  Since every function in javascript is also just a variable, you can reassign the variable anytime you like.  You can test this by creating a function called “test” with some trivial method body.  Then, at the bottom of your page you can write “alert(test);”  and in the alert box, you will see the method body.  So what Victor is doing in his example is storing the generated __doPostBack method into a new variable, then overwriting the method with his own method, while inside the new method, he calls the old method, so that the form still gets posted, did you follow all that? :)

Anyways, its a nifty trick for anyone who needs to insert some custom javascript code for your postbacks, just thought I would share.

9 Comments so far »

  1. Anonymous said,

    Wrote on March 24, 2005 @ 5:12 am

    i just use a customvalidator.

  2. breichelt said,

    Wrote on March 24, 2005 @ 7:05 am

    Its not necessarily always validation logic that I want to insert, sometimes its opening or closing a window, or refereshing a window, or making an asychronous request.

  3. Anonymous said,

    Wrote on March 25, 2005 @ 6:04 am

    just because it’s called a validator it doesn’t necessarily have to do validation. you can call your method and do the “asynchronous” call you want to do.

  4. breichelt said,

    Wrote on March 25, 2005 @ 1:41 pm

    I haven’t used validators very much, only tried them a few times, does the javascript that they produce run in firefox?

  5. Anonymous said,

    Wrote on March 26, 2005 @ 9:31 am

    I don’t know. You’re the one how needs to “hijack” the page to do “asynchronous” calls not me.

    BTW, does talking like this pay more?

  6. breichelt said,

    Wrote on March 27, 2005 @ 6:40 am

    Geez, sorry, I was just trying to have a discussion, if you are annoyed, just stop reading and commenting.

  7. Anonymous said,

    Wrote on March 27, 2005 @ 6:59 pm


  8. Anonymous said,

    Wrote on March 27, 2005 @ 10:56 pm

    Have you played with Remote Scripting techniques at all? It might be a bit cleaner to do what you’re talking about than posting back (as seems to be the preferred ASP.NET way of doing things). Remote Scripting allows you to create very tight, interactive, UIs (think GMail; no postbacks, no page refreshes — responsiveness feels almost like a thick client).

    Here are a few links of interest:

    http://www.thycotic.com/dotnet_remotescripting.html

    http://www.codeproject.com/aspnet/AlvaroRemoteScripting.asp

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnclinic/html/scripting11122001.asp

  9. Anonymous said,

    Wrote on September 16, 2005 @ 5:36 am

    Very nice site!

Comment RSS · TrackBack URI

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Enter my name (ben) in this box, so I know you're a human.

Comment: