execute onclick event for submit key when you press enter key

I have a search key on asp.net page and I need the code for search click to be executed when enter key is pressed. Please provide code in vb.net.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

moduloConnect With a Mentor Commented:
PAQed with no points refunded (of 500)

Community Support Moderator
Use Client-script to overcome this problem(trap enter key code):

<body onkeydown="if(event.keyCode == 13){document.getElementById('btnSubmit').click();}">
<asp:Button id="btnSubmit" runat="server" Text="Submit"></asp:Button>

"when the enter key is pressed"...  do you mean when you have the cursor in the textbox?  This should be the default behavior of the page.  If there are other controls that you want to have the Enter key cause the opstback, you can do this in page load, if the page is not a postback:

serverControl.Attributes.Add("onclick", "javascript:document.Form1.submit();")

This assumes the id string in your <form runat="server" id="Form1"> is "Form1".  If it is different, refer to it in the javascript command as:


This is IE compliant code.  If you need to support other browsers we'll have to get a bit more involved (only a little, though) in the JavaScript.

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.



should be

"javascript:if (event.keyCode == 13) document.Form1.submit();"

ftacoukjAuthor Commented:
What worked for me was to put the following code in ASPx page
<input type="text" style="DISPLAY:none">
Page.RegisterHiddenField("__EVENTTARGET", "MYBUTTON")

Change MYBUTTON to the ID of the button to which you want this action to  default.
another work around:
This has got to be the wierdest fix I've ever seen, but it works. I was completely puzzled as to why my search button would -not- work when I entered data and hit enter. It would just not do anything. Then I read this article on Matt Berther's blog:

For the last few hours, I've been trying to figure out why my event handler is not being hooked up when I hit the ENTER key to submit my ASP.NET form.

As it turns out, this is a bug with ASP.NET. If you're not aware of this, the work around is to insert an invisible *HTML* textbox (an asp:TextBox wont work).

Add this line somewhere on the page:
<input type="text" style="display:none">
, and all will work as expected.
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

Private 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
Hi ftacoukj,

See this article : http://www.mag37.com/csharp/articles/DefaultSubmitButton.html?tabid=8 
that describes the default submit button

Here is simply way to "catch" the Enter Key:

<asp:TextBox ID="txtSearch" MaxLength="50" Runat="server"></asp:TextBox>

put this in code file:

Protected WithEvents txtSearch As System.Web.UI.WebControls.TextBox

Private Sub txtSearch_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged

      If Trim(txtSearch.Text) <> "" Then
          Response.Redirect(Request.Your_Search_Page + "?Search=" + txtSearch.Text )
      End If

End Sub

you then pass the search string to the page you use for the search.
All Courses

From novice to tech pro — start learning today.