Skip to content
Donner's Daily Dose of Drama
Donner's Daily Dose of Drama
  • The Good
    • Blogging
    • Consumer Protection
    • Environment
    • Ethics
    • Geek’s Home
    • Lisa Lanett
    • Medfield
    • Music
    • Parenting and Technology
    • Travel
    • wow
  • The Bad
    • Business
    • Ebay
    • Investment
    • Job search
    • Personal Finance
    • Politics
  • The Ugly
    • Information Technology
      • Business Intelligence
      • Content Management
      • Free Software
      • I18N and L10N
      • Java
      • Open Source
      • Mobile Devices
      • Open Source Business Intelligence
      • OSBI
      • SDA
      • Security
      • Smartphone
      • Software Best Practices
      • Software Engineering
      • SQL Server
      • Streaming Media
      • Web
    • Austria
    • Fiction
    • Hardware
    • iPod
    • Miscellaneous
    • Uncategorized
    • Video
    • Weekend Warrior
Donner's Daily Dose of Drama

asp:ImageButton with PostBackUrl and OnClientClick event

Christian Donner, March 16, 2010

When an asp:Imagebutton control is used with a client-side event, for instance to show a popup window, and the control is not in an asp:UpdatePanel, the client-side event does not complete because the ImageButton initiates a full post-back. In the browser, for instance with a ShadowBox popping up, it appears that the event fired and the popup starts to render, but then it disappears as the post-back response arrives. Again, this does not seem to be a problem with an asp:UpdatePanel. Today, I found that one of my ImageButtons no longer worked because I had to remove the UpdatePanel around it.

Here is the markup for my broken button:

function popBox(url, x, y)
{
  Shadowbox.open({
    content: url,
    player: "iframe",
    height: y,
    width: x,
    options: { modal: true }
  });
}

<asp:ImageButton ID="btn" SkinID="btn" runat="server" CausesValidation="false" PostBackUrl="" />

And the code-behind:

  btn.OnClientClick = "javascript:popBox('Popup.aspx?....', 790, 550);";

The PostBackUrl attribute is empty, because I don’t want to post back at all, I just want to pop up a ShadowBox. I just wanted to make sure that explicitely clearing it does not prevent the post-back (it does not, so it is not needed).

There are 2 ways to fix this problem. One is to replace the asp:ImageButton control with an asp:Image. The latter does not post back at all, but we need to change the code-behind as follows to make it work:

  btn.Attributes.Add("onclick", "javascript:popBox('Popup.aspx?....', 790, 550);");

The other fix is simpler and better. Keep the ImageButton control, but change the Javascript in the Onclick event handler (this is what ASP.Net generates for OnClientClick) to return ‘false’ by default:

  btn.OnClientClick = "javascript:popBox('Popup.aspx?....', 790, 550);return false;";

Now the client-side event handler can safely finish his work without being interrupted by an unwanted postback.

Related Posts:

  • The Voip.ms SMS Integration for Home Assistant
  • OpenVPN
  • Computer Build 2025
  • My USPS Certified Mail Experience Explained
  • Amazon threatens customer of 26 years

Web ASP.NetEvent HandlerImageButtonOnClientClickPostbackUrl

Post navigation

Previous post
Next post

Comments (7)

  1. sondlerd says:
    November 1, 2010 at 8:43 pm

    Great post, I didn’t think about the fact that the ImageButton’s “PostBackUrl” was the cause of the post back I didn’t want… silly me, the name “PostBack” is in the property name.

    thanks for your post!

  2. Andy says:
    April 12, 2011 at 1:27 pm

    I must say THANK YOU! This had me going for 4 hours, even using firebug, and various other tools, it had me puzzled.

  3. shawn says:
    September 19, 2011 at 9:43 pm

    bro…thanks a lot….you help me a lot……..muacksss

  4. Gaurav Pant says:
    January 12, 2012 at 5:42 am

    Really this is a good article, which helps a lot for beginners as me as well as developer.
    Thanks for sharing with us. Check out this helpful link too its also having nice post with wonderful explanation on image control in asp.net,…….

    http://mindstick.com/Articles/8e018bcf-7bec-4414-bb92-c0b454a9dcab/?ImageButton%20in%20ASP.Net

    Thanks everyone for precious post!!

  5. PF says:
    September 7, 2012 at 8:54 am

    You mentioned in
    http://weblogs.asp.net/mhildreth/archive/2009/01/26/testing.aspx#7209911
    that a solution to ASP menu pop-ups was not working:
    “The dyanmic menu pops up, but is behind a Span that is is front of it. Using z-index on either element has no effect.”

    I have the same problem – did you ever solve this problem?

  6. Christian Donner says:
    September 7, 2012 at 8:58 am

    I must have, because the application is still in use, but I don’t remember how. Sorry.

  7. Pingback: [RESOLVED]I want to set a client side validation for imagebutton . | ASP Questions & Answers

Leave a Reply

Your email address will not be published. Required fields are marked *

Pages

  • About
  • Awards
    • TechnoLawyer
  • Contact Christian Donner
  • Project Portfolio
  • Publications
  • Speaking Engagements

Recent Comments

  • Christian Donner on Sealing a leaky cast-iron fireplace chimney damper
  • Eric on Sealing a leaky cast-iron fireplace chimney damper
  • Christian Donner on Contact Christian Donner
  • Max on Contact Christian Donner
  • Christian Donner on Contact Christian Donner

Tags

AHCI Amazon Android ASP.Net AT&T Droid Drupal email Error failure featured firmware Garmin Godaddy Google honda Internet Explorer 8 iPhone Lenovo Lisa Lanett Modules NAS Nexus One Paypal Performance Privacy QNAP raid RS-407 sauna Security spam SQL SR3600 Synology T-Mobile T430s transmission tylö Verizon Virus VMWare Windows 7 windows 8.1 Windows Mobile
  • About
  • Awards
    • TechnoLawyer
  • Contact Christian Donner
  • Project Portfolio
  • Publications
  • Speaking Engagements
©2025 Donner's Daily Dose of Drama | WordPress Theme by SuperbThemes