Textbox OnEnter and setting focus to a control OnLoad

Update 5/23/05: I posted a C# version of this code, and an additional method to allow enter directly from any textbox.  This allows you to have the onEnter behavior using an ImageButton or no button at all.  [Click here to view]

First, set focus to a control when a page loads,
Also, trigger the button's click event when hitting enter in a corresponding textbox.

I'm working in a VB.Net project, so I had to translate the C# version of the TieButtonToTextbox method from an article at allasp.net to VB.Net.  Since I also needed a SetFocusToTextbox method, I also refactored the code out to create a GetFormName method that takes a control and returns the name of the parent form.

The methods below would be called from Page_Load like this: TieButton(Me.txtSearch,Me.btnGo), and SetFocus(Me.txtSearch)

Private Sub SetFocus(ByVal controlToFocus As Control)
   Dim formName = GetFormName(controlToFocus)
   Dim jsString As String = " "
   If Page.IsStartupScriptRegistered("SetFocusToSearch") = False Then
      Page.RegisterStartupScript("SetFocusToSearch", jsString)
   End If
End Sub

Private Sub TieButton(ByVal TextBoxToTie As Control, ByVal ButtonToTie As Control)
   Dim formName As String = GetFormName(ButtonToTie)
   Dim jsString As String = "if ((event.which && event.which == 13) || " & _
      "(event.keyCode && event.keyCode == 13)) " & _
      "{document." + formName + ".elements['" + ButtonToTie.UniqueID + "'].click();" & _
      "return false;} else return true; "
   If TypeOf ButtonToTie Is System.Web.UI.HtmlControls.HtmlForm Then
      CType(TextBoxToTie, HtmlControls.HtmlControl).Attributes.Add("onKeyDown", jsString)
   ElseIf TypeOf TextBoxToTie Is WebControls.WebControl Then
      CType(TextBoxToTie, WebControls.WebControl).Attributes.Add("onKeyDown", jsString)
      Throw New ArgumentException("Control TextBoxToTie should be derived from either System.Web.UI.HtmlControls.HtmlControl or System.Web.UI.WebControls.WebControl", "TextBoxToTie")
   End If
End Sub

Function GetFormName(ByVal sourceControl As Control) As String
   Dim formName As String
      Dim i As Int16 = 0
      Dim c As Control = sourceControl.Parent
      Do While Not TypeOf c Is System.Web.UI.HtmlControls.HtmlForm And Not TypeOf c Is System.Web.UI.Page And i < 500
         c = c.Parent
         i = i + 1
      If TypeOf c Is System.Web.UI.HtmlControls.HtmlForm Then
         formName = c.ClientID
         formName = "forms(0)"
      End If
   Catch ex As Exception
      formName = "forms(0)"
   End Try
   Return formName
End Function

Print | posted on Thursday, August 26, 2004 6:24 PM


# RE: *MORE* ON ASP.NET: How to create a Default

left by aaron weiker at 9/3/2004 9:27 AM Gravatar

# re: Textbox OnEnter and setting focus to a control OnLoad

left by Matt Bond at 9/1/2005 4:40 PM Gravatar
This works beautifully. Who would have thought such a simple bit of functionality would be so hard to find and would not be automatically included by Microsoft.

# re: Textbox OnEnter and setting focus to a control OnLoad

left by J-Brewer at 9/16/2005 1:53 PM Gravatar
Great Code, I agree with above comments. It is disappointing that MS didn't have this build in.

# re: Textbox OnEnter and setting focus to a control OnLoad

left by Manoj Prajapati at 9/21/2005 12:20 AM Gravatar
I have two user control in my form
one For login & other For search
THis code is not working in my case
any help?

# re: Textbox OnEnter and setting focus to a control OnLoad

left by Ryan at 10/6/2005 9:35 AM Gravatar

Thanks for this - it rocks.

Manoj - You need to do what it says up the very top of the page - use the TieButton and SetFocus commands to tie the code to a specific text box on the page. I don't know if it will work with two different text boxes, although I suppose it should.



# re: Textbox OnEnter and setting focus to a control OnLoad

left by Dominic Turner at 2/28/2006 8:46 AM Gravatar
The setfocus bit doesn't add any javascript....

Dim jsString As String = " "

Should be:

Dim jsString As String = "<script language=JavaScript>" + formName + "." + controlToFocus.ID + ".focus();</script>"
Email (never displayed)
Please add 3 and 2 and type the answer here: