Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

execute onclick event for submit key when you press enter key

Posted on 2004-10-25
11
Medium Priority
?
1,021 Views
Last Modified: 2010-07-27
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.
0
Comment
Question by:ftacoukj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 18

Expert Comment

by:tusharashah
ID: 12405972
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
 
LVL 10

Expert Comment

by:jnhorst
ID: 12405992
"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
 
LVL 10

Expert Comment

by:jnhorst
ID: 12405996
Mistake:

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

should be

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

John
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ftacoukj
ID: 12406027
What worked for me was to put the following code in ASPx page
<input type="text" style="DISPLAY:none">
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12406184
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 12410733
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
 
LVL 16

Expert Comment

by:MikeMCSD
ID: 12422956
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
 

Accepted Solution

by:
modulo earned 0 total points
ID: 12771856
PAQed with no points refunded (of 500)

modulo
Community Support Moderator
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question