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:Amazon threatens customer of 26 yearsThe Great Cat Litter Poop OffTyreWiz not working after battery changeSUTAB Scam? Web ASP.NetEvent HandlerImageButtonOnClientClickPostbackUrl
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!
I must say THANK YOU! This had me going for 4 hours, even using firebug, and various other tools, it had me puzzled.
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!!
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?