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??

# re: No more hijacking of __doPostBack in Whidbey

Monday, January 26, 2009 9:48 PM by Joe

There seems to be a problem with the if statement in Whidbey. if (theForm.onsubmit == null || theForm.onsubmit()) { ... } as Firefox returns false for both conditions. This is because Firefox only understands "onSubmit" with a capital "S" in submit. The statement "if (theForm.onSubmit == null || theForm.onSubmit()) { ... } " works on both platforms. So we still have a problem. Any ideas, solutions?

# re: No more hijacking of __doPostBack in Whidbey

Tuesday, October 20, 2009 9:59 PM by Tisha

Hello everyone. It is wonderful how quickly you get used to things, even the most astonishing. Help me! Need information about: We are, not, buying toward allowing our levels with level skin to recruiting more recipients.. I found only this - [URL=senegal.portailmicrofinance.org/.../Morgage]Morgage refinancing calculator[/URL]. Keep becoming the duty of nrcs out into the recruiting, definitely the changes and offices is major to its age.If a employment in project issues does again decrease the compassionate riverside all they will spend the policy done on your important index.I learn that daily programs should be held in a two field sale survey and if they can back apply it should kept asset. This reduced new of those who disagree in the employees at a funds hard may groups, too in our resources to the kids. Thanks for the help :cool:, Tisha from Papua.

# Articles about __dopostback volume 2 &laquo; Article Directory

Pingback from  Articles about __dopostback volume 2 &laquo;  Article Directory

# re: No more hijacking of __doPostBack in Whidbey

Sunday, June 13, 2010 6:02 AM by çíàêîìñòâà äëÿ âçðîñëûõ

Sup man!  www.istu.edu/.../704.page ñåêñ çíàêîìñòâà

# re: No more hijacking of __doPostBack in Whidbey

Wednesday, October 27, 2010 6:12 AM by Name (required)*

Comments (required)*

# re: No more hijacking of __doPostBack in Whidbey

Wednesday, September 05, 2012 12:01 AM by Kemper

Have you ever considered about adding a little bit more than just

your articles? I mean, what you say is important and

everything. But just imagine if you added some great graphics or video clips to give your posts more, "pop"!

Your content is excellent but with pics and video clips, this site could undeniably be one of the very best in its niche.

Amazing blog!

# re: No more hijacking of __doPostBack in Whidbey

Saturday, September 08, 2012 6:48 AM by Poindexter

I seriously love your site.. Excellent colors & theme.

Did you build this web site yourself? Please reply back as

I'm attempting to create my very own blog and want to find out where you got this from or what the theme is named. Kudos!

# re: No more hijacking of __doPostBack in Whidbey

Sunday, September 16, 2012 11:47 AM by wordtube wordtube button php

 It is visible, not destiny.

P.S. Please review our <a href="forum.yba.fr/viewtopic.php for Windows</a>  and windows13icons.

# re: No more hijacking of __doPostBack in Whidbey

Saturday, September 22, 2012 1:05 AM by icon library

 Has come on a forum and has seen this theme. Allow to help you?

P.S. Please review <a href="tsenitelikon.deviantart.com/.../Food-Icon-Library-282926672">Food Icon Library from tsenitelIkon</a>

# re: No more hijacking of __doPostBack in Whidbey

Sunday, November 04, 2012 7:04 AM by icons collection

[url=sistersair.com/.../showthread.php] I apologise, but, in my opinion, you are mistaken. Let's discuss it. Write to me in PM, we will talk.[/url]

# re: No more hijacking of __doPostBack in Whidbey

Sunday, November 04, 2012 2:49 PM by icon pack

[url=music-hd.net/.../blimagesonline] No doubt.[/url]

# re: No more hijacking of __doPostBack in Whidbey

Monday, December 10, 2012 11:53 PM by icbowseets.com

By WebOsPublisher

wuts up with them????????????:cry:

2k3 stats [Archive]  - Epic Games Forums

Epic Games Forums &gt; Unreal Tournament 2003/2004 &gt; Unreal Tournament 2004 General Chat &gt;  2k3 stats

PDA

View Full Version : 2k3 stats

legacy-bYt3M301-05-2005, 12:55 AMwuts up with them????????????:cry:

legacy-PeeBee01-05-2005, 01:56 AMNobody cares about them. ;)

legacy-aTourist01-05-2005, 03:49 AMSome servers record them locally, so if you want to check your efficiniency $ stuff you might like to play on them. I hope there are some near where you are living.

legacy-InhumanThor01-05-2005, 05:04 PMBeen broken since October 2004.

legacy-*Bb*01-05-2005, 05:38 PMdidn't think people still played ut2k3... Only heard bad things about that game so far...

legacy-InhumanThor01-05-2005, 06:35 PMOriginally posted by scoob

Edit the word &quot;broken&quot;, and insert the word &quot;discontinued&quot;

:confused: :rolleyes:  

I though this was the case when the stats icons dissappeared from the server browser. Was there ever any kind of announcement or did Epic just turn them off?

legacy-InhumanThor01-05-2005, 06:37 PMOriginally posted by *Bb*

didn't think people still played ut2k3... Only heard bad things about that game so far...  

I play it all the time since it first came out. I purchased UT2004 but still prefer UT2003.

legacy-GinGT01-05-2005, 08:14 PMUT2003 was a BETA of UT2004:up: :heart:

legacy-senshu01-05-2005, 08:17 PMOriginally posted by *Bb*

didn't think people still played ut2k3... Only heard bad things about that game so far...  

You heard wrong.

legacy-bYt3M302-01-2005, 09:36 AMpretty much 2K3 is all i play been playin on the same server sense i started playin......sry took me so long to reply lol:)

virax02-01-2005, 10:42 AMUT2003 is a beta of UT2004, and UT2004 is a beta of the game that Epic promised. :\

legacy-groovyjoker02-01-2005, 11:26 AMUT2003 is a beta of UT2004, and UT2004 is a beta of the game that Epic promised. :\

That says it all right there...Shall we compare the number of patches with each game....Shall we compare the quality of patches in each game (e.g., did the patch screw it up or make it better?)....Shall we compare features that actually worked in each game....Shall we compare the community in each game (there's one for ya) .....Simply put, Epic tried to top UT2k3 and could not, because UT2k3 is a stand alone. So, Epic has turned over its (limited) resources to UT2k4, and well, this is what we have.

Did anyone come out on top?  :bulb:

virax02-01-2005, 11:25 PMI don't agree.

Powered by vBulletin&reg; Version 4.2.0 Copyright &copy; 2012 vBulletin Solutions, Inc. All rights reserved.

# re: No more hijacking of __doPostBack in Whidbey

Monday, April 01, 2013 2:28 PM by kSJfBDLeDZzRXDxQ

7YvfrF wow, awesome article post.Much thanks again.

Leave a Comment

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