Solved

execute onclick event for submit key when you press enter key

Posted on 2004-10-25
996 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
Question by:ftacoukj
    9 Comments
     
    LVL 18

    Expert Comment

    by:tusharashah
    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
    "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
    Mistake:

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

    should be

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

    John
    0
     

    Author Comment

    by:ftacoukj
    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
    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
    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 6

    Expert Comment

    by:vardium
    0
     
    LVL 16

    Expert Comment

    by:MikeMCSD
    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:
    PAQed with no points refunded (of 500)

    modulo
    Community Support Moderator
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

    846 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

    Need Help in Real-Time?

    Connect with top rated Experts

    6 Experts available now in Live!

    Get 1:1 Help Now