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.
ftacoukjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tusharashahCommented:
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>

-tushar
0
jnhorstCommented:
"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:

document.yourformname.submit();

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.

John
0
jnhorstCommented:
Mistake:

"javascript:document.Form1.submit();"

should be

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

John
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

ftacoukjAuthor Commented:
What worked for me was to put the following code in ASPx page
<input type="text" style="DISPLAY:none">
0
AerosSagaCommented:
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)
   Else
      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
   Try
      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
      Loop
      If TypeOf c Is System.Web.UI.HtmlControls.HtmlForm Then
         formName = c.ClientID
      Else
         formName = "forms(0)"
      End If
   Catch ex As Exception
      formName = "forms(0)"
   End Try
   Return formName
End Function
0
mmarinovCommented:
Hi ftacoukj,

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

Regards!
B..M
0
MikeMCSDCommented:
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.
0
moduloCommented:
PAQed with no points refunded (of 500)

modulo
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.