Andornot Blog

Thursday, September 27, 2007 10:12 AM

Fix Firefox DefaultButton issue with UpdatePanel

by Peter Tyrrell

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

  1. 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.
  2. 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.
  3. 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!

 

Monday, September 24, 2007 1:57 PM

S.J. McKee Archives @ Brandon University

by Administrator
Andornot Consulting is proud to announce their latest archives web site: the S.J. McKee Archives and Special Collections @ Brandon University.

The S.J. McKee Archives houses a wide-ranging collection of unpublished archival records including minute correspondence, programs, catalogues, photographs, scrapbooks, school records created by individuals, clubs, businesses, schools, located in Brandon and southwestern Manitoba.

The archives also holds three special collections of published archival sources, the earliest dating from 1800. These include the Thomas Dudley Howland collection of Fabian tracts, the George Thorman collection of school textbooks and the Manitoba Pool Elevator Library.

The S.J. McKee Archives and Special Collections uses Andornot's Archives Online v2 and Starter Kit.

Visit their web site at http://bartok.brandonu.ca/
Monday, September 24, 2007 10:40 AM

.NET 3.5 Extension methods

by Peter Tyrrell

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.

Sunday, September 23, 2007 3:13 PM

BC Teachers' Federation - Video Resources

by Administrator
Using the Andornot Starter Kit, BCTF Information Services and Andornot Consulting simultaneously developed two online versions of their internal Video and Library catalogues. Available only to BCTF members, users can browse the online catalogues and send an email request with the title information to BCTF staff.

The BCTF Video Resources can be browsed at http://bctf.ca/videos.aspx
Friday, September 21, 2007 3:59 PM

Remove Diacritics

by Peter Tyrrell

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));
}

Month List