Fix Firefox DefaultButton issue with UpdatePanel
I was going to call this post Blah blah blah: The Maddening, but a lucky dip into forums.asp.net just before I began to write saved my bacon. I now have a fix, and the madness has been reduced to a mere simmer from its former rolling boil. The Sample 1: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SaskatoonLibrary.OneSearch.WebForm1" %> 2: 3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 4: 5: <html xmlns="http://www.w3.org/1999/xhtml" > 6: <head runat="server"> 7: <title>Untitled Page</title> 8: </head> 9: <body> 10: <form id="form1" runat="server" defaultbutton="SearchButton"> 11: <asp:ScriptManager ID="ScriptManager1" runat="server" /> 12: 13: <asp:Panel ID="Panel1" runat="server"> 14: <asp:TextBox ID="Textbox1" runat="server"></asp:TextBox> 15: <asp:Button ID="SearchButton" Runat="server" 16: Text="Search"></asp:Button> 17: </asp:Panel> 18: 19: <asp:UpdatePanel ID="UpdatePanel1" runat="Server" 20: UpdateMode="Conditional" 21: ChildrenAsTriggers="false"> 22: <Triggers> 23: <asp:AsyncPostBackTrigger ControlID="SearchButton" /> 24: </Triggers> 25: <ContentTemplate> 26: <asp:Repeater ID="Repeater1" runat="server"></asp:Repeater> 27: </ContentTemplate> 28: </asp:UpdatePanel> 29: 30: 31: </form> 32: </body> 33: </html>
The Issue
An ASP.NET AJAX* UpdatePanel on the page, triggered conditionally by a button. A single textbox in the form. Without some way of forcing the button to click when the ENTER key is pressed, the form merely does a postback, not a callback, thus rendering the the entire point of my UpdatePanel COMPLETELY NIL.
Note, in the sample above, that the form's DefaultButton attribute is set to SearchButton. This is the usual way to ensure that pressing ENTER fires the appropriate button click event server-side. And indeed, without an UpdatePanel in the picture, this works as advertised for both IE7 and Firefox.
But. Throw an UpdatePanel and the need to fire a callback into the mix, and Firefox pays no attention, but continues to postback when ENTER is pressed. Only IE7 performs correctly: the button's client-side click event is fired and the callback, not a postback, occurs.
The Not-fixes
These don't work. Don't waste your time. Either IE will be happy, or Firefox, but not both. (Remember? The Maddening?)
- Add a non-visible textbox to the form, which is a hack only suitable for ensuring the correct server-side event fires. Makes no difference to client-side callback initiation.
- Add a client-side onkeypress event to the textbox to check and see if the ENTER key has been pressed, and if it has, forcibly fire the relevant button's click event.
- Bang your head on the keyboard so hard people on the street stop and compliment you on the QWERTY tattoo.
The Fix
Set the button's UseSubmitBehavior attribute to false:
<asp:Button ID="SearchButton" Runat="server" Text="Search" UseSubmitBehavior="false" ></asp:Button>
Apparently there's a little more monkeying to do if the button is an ImageButton:
<asp:ImageButton ID="ImageButton1" runat="server" OnClientClick="this.disabled=true;__doPostback(this.name,'');" />
* In the name of Cyclopean Cthulhu, couldn't we just have kept calling it by its in-development codename: "Atlas"? It feels like a mouthful of marbles every time I say "ASP.NET AJAX". And I have to write it in all caps? Fwah!
.NET 3.5 Extension methods
There's been a lot of excitement about a new language feature in the .NET Framework 3.5 "Orcas": extension methods. Extension methods allow developers to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type. -- ScottGu's Blog That's High Geek for "you can tack on methods to existing .NET classes." For example, you could extend the String class with a UrlEncode method: C# Extension Method: UrlEncode public static string UrlEncode(this string s) { return HttpUtility.UrlEncode(s); }
Then you could call it from any string:
string str = "blah"; string strEncoded = str.UrlEncode();
That's all very useful, and I'll make heavy use of it, I'm sure. It's not exactly new, though. Javascript has had extension methods on offer for years with the prototype object. Entire javascript frameworks have been built around it: Prototype, mootools, jQuery, etc. Taking the above UrlEncode example:
Javascript Extension Method: UrlEncode
String.prototype.UrlEncode = function() { return encodeURIComponent(this); }
So, as far as the coming extension methods in .NET go, I guess I'm less "wow, neato" and more "it's about time." I'm *such* a jerk.
Remove Diacritics
WebPublisher sometimes has problems with diacritic characters, and XML input actually requires that you do not include diacritics in field names AT ALL, so here is a *very* useful C# method that will strip diacritics from any string, replacing them with their non-diacritic equivalents. [Found at Sorting It All Out. Written in C# .NET 2.0.] using System.Globalization; using System.Text; public static string RemoveDiacritics(string input) { string stFormD = input.Normalize(NormalizationForm.FormD); int len = stFormD.Length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[i]); if (uc != UnicodeCategory.NonSpacingMark) { sb.Append(stFormD[i]); } } return (sb.ToString().Normalize(NormalizationForm.FormC)); }
Developer on the go
Not so much about attaching wheels to a developer, although that would be really cool, and would, I'm sure, lead to more rapid development as well as head-to-head racing and eventually tractor pulls. No, this is me and the requirement to work away from my office at a moment's notice. I may have to pull up stakes and run off at any moment, not because my office is highly flammable, or a contended military zone, but because I now share it with a newborn infant, who is more damaging to productivity than an inferno, and more interruptive than a civil war. Sometimes I'm able to work with the little bundle of fiery conflict wrapped slumbering on my chest, but long before she came along I laid careful plans for a backup and mobile office. Here it is. Operation Desert Skedaddle 1. Laptop Dell Inspiron 9400 "desktop replacement" laptop, Core 2 Duo, 2Gb RAM. Kind of ugly and clunky looking, which is oh so Dell, but it does the job. Of course, *now* they're offering pretty case colours. Now. I would have gone for espresso brown, but I'm stuck with yawn-inducing silver. An external USB Lacie 160GB drive, for virtual machines and a gigantic iTunes library. 2. File Synchronization GoodSync to sync files between desktop and laptop, by the makers of Roboform password keeper software, which I also can't live without, and it gets synced along with everything else. 3. VPN Hamachi VPN to stay connected to my home network. Dead easy to configure. I can sync over the VPN if I'm really desperate, and the VPN aids remote desktop in that I don't have to configure a remote desktop port on the home router for each computer on the network. 4. Office space I got a used Linksys WRT54G wireless router on eBay and flashed it with DD-WRT open source Linux-based router firmware. DD-WRT offers many advanced features not found in the original Linksys firmware. It wasn't strictly necessary, but it *was* very satisfying. I installed it at my parents' house and beefed up their internet plan to 10 Mpbs down, 1 Mbps up. They get pretty decent wireless, and I get a backup office. Are you mobile? How does it work for you?
Hello Calem!
Ted Jardine has a son! The Andornot family is blessed. Nice work, Jardines. Calem Nathanael Jardine was born September 7, 2007 8:50pm - 9 lbs. 8 oz. (Ted and Peter did not plan to have babies within a week of each other - it's all just happy coincidence. I swear!)
Hello Evvy!
Welcome to the newest Peaeater and congratulations to Peter and Rachelle! Woohoo!  (Click for larger view)
Yes, we like Vancouver too....
Joel Spolsky just finished his Vancouver stop of the FogBugz World Tour (which I would have liked to have gone to, but with #3 imminent...) and he has some nice things to say about Vancouver: Vancouver is, without a doubt, one of North America's most beautiful cities. Sparkling, clean, everything works well, nothing can possibly go wrong, people are friendly, and with the new weakened US dollar it's really quite a prosperous place to live. Brett and I had dinner at Joe Fortes, where you get a choice of 4 different local species of salmon, maybe 20 other kinds of fresh fish, or about 10 different type of oysters, and there's a beautiful rooftop deck where you can enjoy the usually pleasant Vancouver weather. I guess there are reasons for needing half a million to get a house even in the 'burbs.
|