Last week I found myself pondering what new feature to toy with for
the site. The answer I came up with was a search capability. The
search form is pretty simple, and things seemed to be going smoothly
- until I found that IE 7 won't let one submit a form using the enter
key without some programmer mojo in there to tell it to do so.
Firefox has no problem at all with triggering the button's event
handler with the enter key. 

Today I found a great bit of code which was originally published by
Janus Kamp Hansen (visit http://www.kamp-hansen.dk) and extended upon
by Microsoft MVP Darrell Norton (visit 
http://www.dotnetjunkies.com/weblog/darrell.norton/). Darrell's update
(I haven't seen Janus' original) is nice, but it needed only minor 
tweaking to reflect differences between .NET versions 1.0 and 2.0 and
use of a StringBuilder object. The JavaScript contained therein
remains unchanged. 

The code essentially lets you associate a control (such as a textbox)
to a button with submit behavior. The really cool thing about this is
you can wire different controls to different buttons with it. My 
search form is very simple - just a single textbox control for the 
search term, a submit button, and a pair of checkboxes with options
for good measure.

Darrell's C# code may be found at: 
http://codebetter.com/blogs/darrell.norton/archive/2004/03/03/8374.aspx


Here's my take, which is in VB.NET:

Public Sub SetDefaultButton(ByVal page As Page, ByVal textControl As TextBox, ByVal defaultButton As Button)
        Dim strFunction As String = String.Empty
        Dim strType As Type = GetType(String)
        Dim sb As New StringBuilder

        With sb
            .Append(vbCrLf & vbCrLf)
            .Append("<SCRIPT language=""javascript"">" & vbCrLf)
            .Append(vbTab & "function fnTrapKD(btn, event){" & vbCrLf)
            .Append(vbTab & vbTab & " if (document.all){" & vbCrLf)
            .Append(vbTab & vbTab & vbTab & " if (event.keyCode == 13){" & vbCrLf)
            .Append(vbTab & vbTab & vbTab & vbTab & " event.returnValue=false;" & vbCrLf)
            .Append(vbTab & vbTab & vbTab & vbTab & " event.cancel = true;" & vbCrLf)
            .Append(vbTab & vbTab & vbTab & vbTab & "btn.click();" & vbCrLf)
            .Append(vbTab & vbTab & vbTab & " }" & vbCrLf)
            .Append(vbTab & vbTab & " }" & vbCrLf)
            .Append(vbTab & vbTab & " else if (document.getElementById){" & vbCrLf)
            .Append(vbTab & vbTab & vbTab & " if (event.which == 13){" & vbCrLf)
            .Append(vbTab & vbTab & vbTab & vbTab & " event.returnValue=false;" & vbCrLf)
            .Append(vbTab & vbTab & vbTab & vbTab & " event.cancel = true;" & vbCrLf)
            .Append(vbTab & vbTab & vbTab & vbTab & "btn.click();" & vbCrLf)
            .Append(vbTab & vbTab & vbTab & " }" & vbCrLf)
            .Append(vbTab & vbTab & " }" & vbCrLf)
            .Append(vbTab & vbTab & " else if(document.layers){" & vbCrLf)
            .Append(vbTab & vbTab & vbTab & " if(event.which == 13){" & vbCrLf)
            .Append(vbTab & vbTab & vbTab & vbTab & " event.returnValue=false;" & vbCrLf)
            .Append(vbTab & vbTab & vbTab & vbTab & " event.cancel = true;" & vbCrLf)
            .Append(vbTab & vbTab & vbTab & vbTab & "btn.click();" & vbCrLf)
            .Append(vbTab & vbTab & vbTab & " }" & vbCrLf)
            .Append(vbTab & vbTab & " }" & vbCrLf)
            .Append(vbTab & " }" & vbCrLf)
            .Append("// -->" & vbCrLf)
            .Append("</SCRIPT>" & vbCrLf)

            strFunction = .ToString()

            .Remove(0, .Length)

        End With

        ClientScript.RegisterStartupScript(strType, "ForceDefaultToScript", strFunction)
        textControl.Attributes.Add("onkeydown", "fnTrapKD(" + defaultButton.ClientID + ",event)")

        'tidy up
        sb = Nothing

    End Sub

The last thing you need to do is to wire your control to your submit
button. I placed my call in the Page_Load event handler, as Darrell
suggested:

   	'wire up the enter key event handler
	Call SetDefaultButton(Me.Page, txtSearch, btnSubmit)

... and that's it. I tested it and found that it worked flawlessly. One
note I can offer: because the button's address is passed into the function,
you don't have to substitute your button's UniqueID for "btn." I spaced
and figured I'd have to substitute my button's ID. That's not necessary,
but reading for comprehension *is*.


Best,

halfgk


copyright 2007 halfgk.com