Welcome to AspAdvice Sign in | Join | Help

Cross-page postbacks and OnClientClick

This one was asked & replied originally on ASP.NET Forums

You have Button

<asp:Button ID="Button2" runat="server" Text="Button" PostBackUrl="~/Default4.aspx" OnClientClick="return confirm('Hello');" />

 

No matter what you click OK or Cancel, it does a normal postback, as if cross-page postbacks wouldn't exist. Why?

Answer:

Well,  cross-page postbacks are implemented using javascript. And when you put return statement like that it ends up into onclick of the rendered button. For example Button like this

<asp:Button ID="Button2" runat="server" Text="Button" PostBackUrl="~/Default4.aspx" OnClientClick="return confirm('Hello');" />

 

renders

<input type="submit" name="Button2" value="Button" onclick="return confirm('Hello');WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;Button2&quot;, &quot;&quot;, false, &quot;&quot;, &quot;Default4.aspx&quot;, false, false))" id="Button2" />

 

As you can see, this effectively prevents rest of the postbacking script from functioning since whatever confirmation box returns, it won't let postback options to be set and therefore prevents cross-page postbacking from working.

Answer is to change confirm check so that it returns only when user click Cancel.

Put the OnClientClick as

OnClientClick ="if(!confirm('OK?'))return false;"

Then the rendered onclick attribute changes

onclick="if(!confirm('OK?'))return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;Button2&quot;, &quot;&quot;, false, &quot;&quot;, &quot;Default4.aspx&quot;, false, false))"

 

And you should be good to go.

 

Published Sunday, April 30, 2006 10:12 AM by joteke
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Cross-page postbacks and OnClientClick

Hello
I couldn't understand the difference? I do find this post good, but I couldn't know the difference, can you help.

thanks
Sunday, April 30, 2006 5:31 AM by Fadi

# re: Cross-page postbacks and OnClientClick

Fadi,

the original one won't let WebForm_DoPostBackWithOptions ever run while the second which I provided, allows it to run when user click OK on the confirm box.

You can test it very easily by taking these two sample Buttons and experimenting with them.
Sunday, April 30, 2006 11:20 AM by Teemu

# re: Cross-page postbacks and OnClientClick

Thanks a lot, I noticed the difference. the first always returns without executing the second part of the script.

Thank you.
Sunday, April 30, 2006 1:16 PM by BilalHaidar [MVP]

# re: Cross-page postbacks and OnClientClick

Thank you so much.  This solved the problem I was struggling with for a few hours.
Friday, May 26, 2006 11:00 AM by Paul Nunes

# re: Cross-page postbacks and OnClientClick

I have a page including in a master page. This page have 3 buttons, it use OnClientClick. When i see HTML code the first button have OnClientClick function but another are not. why?
Tuesday, May 30, 2006 4:07 AM by Multi asp:Button in master page

# re: Cross-page postbacks and OnClientClick

Thanks, this tip was very welcome. It now works great for me!
Thursday, June 15, 2006 11:11 AM by ArkonXX

# Elegant Way to Handle &quot;Are you Sure&quot; Dialog on Client | keyongtech

# re: Cross-page postbacks and OnClientClick

Hi, I followed your article and had below in my aspx file. Your suggestion did not work for me. Would appreciate any suggestion? Please note loginview control was used.
Monday, May 04, 2009 2:11 PM by Jyh-Woei Shiue

# re: Cross-page postbacks and OnClientClick

Thanks a lot, you saved me a lot of time. I am searching for this.
Monday, June 01, 2009 3:47 AM by Aykut ARAS

# re: Cross-page postbacks and OnClientClick

Worked for me... THANKS!!!
Tuesday, June 30, 2009 11:20 AM by bburtonpa

# Confirmar o envio de formul??rio para outro script (cross-page postback) &laquo; Blog do Neilor

# re: Cross-page postbacks and OnClientClick

Saved at least 1 day of my work. Thanks a Lot.
Thursday, July 16, 2009 7:24 AM by Rom

# re: Cross-page postbacks and OnClientClick

Amazing article, Joteke must be a really smart guy!
Tuesday, September 29, 2009 3:41 AM by Nguyen Minh Dzung

# re: Cross-page postbacks and OnClientClick

To Jyh-Woei Shiue: Hi. It also didn't work for me at first. The problem was that I had both javascript and vbscript in the webpage. The validation function was in javascript but the program thought it was vbscript. The fix was to put the javascript code above the vbscript code! Regards, Ben
Friday, October 30, 2009 12:59 AM by BenM

# re: Cross-page postbacks and OnClientClick

To Jyh-Woei Shiue: Hi. It also didn't work for me at first. The problem was that I had both javascript and vbscript in the webpage. The validation function was in javascript but the program thought it was vbscript. The fix was to put the javascript code above the vbscript code! Regards, Ben
Friday, October 30, 2009 1:03 AM by BenM

# Buy Reno Grand Vitara 2008 Suzuki Sx4, Car Review Suzuki Sx4 Crossover

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below