No more hijacking of __doPostBack in Whidbey

Note: this entry has moved.

 

Intercepting form submissions

 

A few months back I was asked by a developer on my team about the best way to make sure a function was always called before form submission. This is a recurring problem among web developers. Listening for form.onsubmit (by using Page.RegisterOnSubmitStatement ) is just not enough because it won’t fire when the form is submitted programmatically as ASP.NET’s __doPostBack function does:

 

     function __doPostBack(eventTarget, eventArgument) {

          var theform;   

          // …

          theform.__EVENTTARGET.value = eventTarget.split("$").join(":");

          theform.__EVENTARGUMENT.value = eventArgument;

          // this won’t cause the form.onsubmit event to fire

          theform.submit();

     }

 

This means that for any given page in which the __doPostBack function is present we need another way to intercept form submissions. So, I told the developer to hijack __doPostBack

 

Hijacking __doPostBack      

 

A common technique, as we can’t modify __doPostBack to make it call our custom function, is to just hijack it:

 

// save a reference to the original __doPostBack

var __oldDoPostBack = __doPostBack;

// replace __doPostBack with another function

__doPostBack = AlwaysFireBeforeFormSubmit;

 

The implementation for our AlwaysFireBeforeFormSubmit should look like:

 

function AlwaysFireBeforeFormSubmit (eventTarget, eventArgument) {

// do whatever pre-form submission

// chores you need here

// finally, let the original __doPostBack do its work

return __oldDoPostBack (eventTarget, eventArgument);

}

 

Multiple hijackings is (usually) a bad thing

                                                                                                                    

Last week, pages on one of our web apps started breaking. What happened was that this developer passed my recommendation to another developer. They both were developing controls and they both were interested in getting a chance to run a function before form submission. Now… pages including both controls were breaking because of the two hijacking attempts.

 

How can the hijacking of __doPostBack be coordinated between different devs on the same team or better yet between any devs in the entire world? :-) Easy enough: the framework should support it.

 

Hijacking with first-class support from the framework

 

This is exactly what I’ve done for our custom framework. Basically I’ve told every dev on every team on the project to never, never, ever hijack _doPostBack by themselves. Instead they will simply call Page.RegisterOnSubmitStatement and the framework will make sure their function get always called before a form submission.

 

Our framework now includes its own hijacking of __doPostBack and it’s the only one allowed to do so. The “impostor” function makes sure form.onsubmit gets always called by checking for its existence and then calling it. Pretty much like Whidbey will do.

 

Whidbey modifies __doPostBack

 

The ASP.NET team heard the many voices that were raised during last years regarding this topic and so decided to add support for this in Whidbey. Good news is this hijacking won’t be necessary anymore (so you can discard the all the previous paragraphs…) because the __doPostBack function has been modified to explicitly call form.onsubmit, something like this:

 

     function __doPostBack(eventTarget, eventArgument) {

          var theform;

          // note how form.onsubmit is being called explicity

          if (theForm.onsubmit == null || theForm.onsubmit()) {

               theForm.__EVENTTARGET.value = eventTarget;

                theForm.__EVENTARGUMENT.value = eventArgument;

                theForm.submit();

          }

     }

 

 

Published Monday, March 01, 2004 10:19 AM by vga
Filed under: ,

Comments

# re: No more hijacking of __doPostBack in Whidbey

Monday, March 01, 2004 7:39 PM by Jason
Hello, can you show ur code? I'm trying to achieve the same thing.

# re: No more hijacking of __doPostBack in Whidbey

Tuesday, March 02, 2004 11:38 AM by MikeWo
FINALLY! I've seen all sorts of work arounds to dealing with the __doPostBack.

I've mostly seen assigning a client side click event to a web control that looks like this:

Button.Attributes.Add("onClick", "if (foo() == true) ")

This creates the following in HTML:

<Button onClick="if (foo() == true) __doPostBack">

This was used to call a common foo() function for anything that would cause a postback, but had to be added for each control that needed to fire it. I had not seen the highjacking as you showed above. I kept thinking this was a bad idea given that it relied on the framework NOT adding a semicolon in front of the __doPostBack statement on the rendering.

# re: No more hijacking of __doPostBack in Whidbey

Tuesday, March 02, 2004 2:16 PM by Adam
Victor:

I've also failed to implement the hijacking without getting some jscript errors...

can you post your code?

THANKS
Adam.

# re: No more hijacking of __doPostBack in Whidbey

Thursday, March 18, 2004 7:19 PM by Mike C
Here's another way I found to do it. Basically, override the submit() method on the form.. Add these 2 lines to your control, and the onsubmit() event will be fired. Then, you can simply remove them when Whidbey fixes the issue..

Page.RegisterClientScriptBlock("ViewstateSubmitHandler", "<script language=JScript>function ViewstateForm.submit() {ViewstateForm.submitButton.click();}</script>");
Page.RegisterClientScriptBlock("ViewstateSubmitButton", "<input type=submit id=submitButton style=\"display: none;\">");

# re: No more hijacking of __doPostBack in Whidbey

Wednesday, March 31, 2004 8:37 AM by krish
Victor,

I was going through your example of avoiding
the __doPostBack Problem. As I am new to developing .Net controls I am not sure where to put the code to avoid the problem.

will I get more help on this?

Let me explain what I am doing.
I am using link buttons inside repeater so that
PRODUCTS are listed with the productId as a hyperlink. so when the user clicks the link the screen to product details are listed to allow the user to edit.

but when the user (@ the moment myself)clicks
on the link the __doPostBack is fired
and nothing works as what I have wanted.

Hope you will explain where exactly I should place the function AlwaysFireBeforeFormSubmit
so that I can implement your concept.

thanks,
Krish

# re: No more hijacking of __doPostBack in Whidbey

Monday, April 05, 2004 10:56 AM by NiKoLай
On my mind, I've found more useful way
You can override a Render method to solve the problem, like this [http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=191690]

overriding method would be looked like
[I'm using C#]

protected override void Render(HtmlTextWriter writer)
{
StringBuilder stringBuilder = new StringBuilder();
StringWriter stringWriter = new StringWriter(stringBuilder);
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

base.Render(htmlWriter);

string html = stringBuilder.ToString();
if (html.LastIndexOf("theform.submit();") != -1)
html = html.Replace("theform.submit();", "theform.onsubmit(); theform.submit();");
writer.Write(html);
}


P.S.: fireEvent can be used instead of theform.onsubmit();

# re: No more hijacking of __doPostBack in Whidbey

Tuesday, April 13, 2004 4:09 PM by Eric Newton
I'd like to know why we have to wait for WHIDBEY for dumb little fixes like this, instead of minor service packs for .NET.

# re: No more hijacking of __doPostBack in Whidbey

Saturday, April 17, 2004 11:42 PM by VGA
Hi Eric,

Sorry I don't have an answer for you :-)

# re: No more hijacking of __doPostBack in Whidbey

Wednesday, June 09, 2004 8:50 PM by Chris Tran
Victor,
if you're getting javascript errors running the
above code try completely qualifying the function names:

// save a reference to the original __doPostBack
var __oldDoPostBack = window.__doPostBack;

// replace __doPostBack with another function
window.__doPostBack = window.AlwaysFireBeforeFormSubmit;

# Easy

Sunday, July 25, 2004 5:04 PM by Edilberto Sánchez Forero
Only you must use the override with the arguments... That's all:

<script>
var __oldDoPostBack


function reDirigirPostBack ()
{
if (typeof __doPostBack != "undefined")
{
// save a reference to the original __doPostBack
__oldDoPostBack = __doPostBack;
// replace __doPostBack with another function
__doPostBack = ejecutarPrevioEnvio;
}
}


function ejecutarPrevioEnvio (eventTarget, eventArgument)
{
mostrarMensajeEmergente ();

// Si es necesario se tendria en cuenta el onsubmit de cada pagina con
// if (theForm.onsubmit == null || theForm.onsubmit()) {

return __oldDoPostBack (eventTarget, eventArgument);
}



/* Asignaciones previas */

window.onload = reDirigirPostBack;

# re: No more hijacking of __doPostBack in Whidbey

Monday, January 22, 2007 7:11 PM by Jes

When I call __doPostBack in Asp.Net 2.0 asynchronusly, I need to fire an function in Javascript after the server does its thing. Is there a delegate function I can specify in Javascript or something that fire after __doPostBack is done firing??

Leave a Comment

(required) 
(required) 
(optional)
(required)