Need enter key to trigger event on aspnet html page

On an intranet site when someone types and hits enter in textbox, would like doclick to trigger on search button. The following works great in IE, but not Chrome:

On Load:
  Me.CurrentVin.Attributes.Add("onKeyPress", "doClick('" + Button23.ClientID + "',event)")
Html:

<script type= "text/javascript">
    function doClick(Button23, e) {
        //the purpose of this function is to allow the enter key to
        //point to the correct button to click.
        var key;

        if (window.event)
            key = window.event.keyCode;     //IE
        else
            key = e.which;     //firefox

        if (key == 13) {
            //Get the button the user wants to have clicked
            var btn = document.getElementById(Button23);
            if (btn != null) { //If we find the button click it
                btn.click();
                event.keyCode = 0
            }
        }
    }
</script>
Dave_CARSITAsked:
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.

Guru JiCommented:
Try something like this in Chrome

    var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
    if(key == 13) {
        e.preventDefault();
        alert("enter pressed");
0
Dave_CARSITAuthor Commented:
Am I adding or replacing lines?
0
Guru JiCommented:
Actually you are putting this is a keypress function just like below

$('#myDiv').keydown( function(e) {
    var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
    if(key == 13) {
        e.preventDefault();
        alert("enter pressed");
    }
})

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Dave_CARSITAuthor Commented:
Thank you for your time.
Unfortunately, I`m unable to make it work. When I hit enter in the textbox, site goes to another page.
I`m not sure where to place the code you supplied. Also, should I remove or change the On Load line?
0
Guru JiCommented:
Hi Dave,

Can you post your html file or paste your entire page code ?
0
Dave_CARSITAuthor Commented:
The first post is from the page. One Button on load, one textbox and button which do a simple search for a grid form. Works great in IE?? When I try it in Chrome, will not load (click?) search button. User has to manually click button.
Thanks
0
Guru JiCommented:
Why I asked for your code is to get the names and id's of all the text box which is missing in post 1 . Anyways
Here is what you have to do, I assume you are using Form

Lets say this is your code
<div>
     <input id="one" type="text"/>
     <button class="jSomeButton" onclick="javascript: return false;">              
     </button>
     <!--Click event handled in Javascript code-->
    </div>

$('#one').keydown( function(e) {
    var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
    if(key == 13) {
        e.preventDefault();
        alert("enter pressed");
     //call your code
    }
});

Open in new window


The above code will trigger when someone press enter key on the textbox one. Let me know if you sitll have problem using the code above, as it just goes into your javascript.
0
Dave_CARSITAuthor Commented:
Everything I have is below and I`m lost, sorry. SQL yes. HTML, still learning!

VB:
Public Class Testing
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.TextBox1.Attributes.Add("onKeyPress", "doClick('" + Button1.ClientID + "',event)")
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Session("FindCall") = Me.TextBox1.Text
        GridView1.DataBind()
    End Sub
End Class

HTML:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Testing.aspx.vb" Inherits="TestPages.Testing" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

<script type= "text/javascript">
    function doClick(Button1, e) {

        var key;

        if (window.event)
            key = window.event.keyCode;  
        else
            key = e.which;    

        if (key == 13) {
           
            var btn = document.getElementById(Button1);
            if (btn != null) {
                btn.click();
                event.keyCode = 0
            }
        }
    }
</script>


    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" />
   
    </div>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </form>
</body>
</html>
0
Guru JiCommented:
Sorry I was thinking you just using basic HTML and was ignoring .net part

I don't have .net to test your code, but can you replace your code

This one
Me.TextBox1.Attributes.Add("onKeyPress", "doClick('" + Button1.ClientID + "',event)")

Open in new window


to this one

TextBox1.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)){ __doPostBack('" + Button1.UniqueID + "','');return false;}else{return true;}}")

Open in new window



Let me know if it works or you get any errors
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
Dave_CARSITAuthor Commented:
Getting very close! Works in IE and Chrome only after first using the search button.

When the page is first opened and something is typed into texbox and enter key is hit, page bounces to another page?

But if seach button is used once, it works great until page is closed and reopened.

Hope you have an idea.

Thank you
0
Dave_CARSITAuthor Commented:
You are the best!!! Slight typo on my first try.

Thank you so much!!!!!!
0
Dave_CARSITAuthor Commented:
Absolutely, the best, quickest and most professional advice I have received!
0
Guru JiCommented:
Hey Glad It worked. So if it working when page is closed and reopened ?

you can remove that JavaScript function of doClick.
0
Dave_CARSITAuthor Commented:
Works perfectly!

Thank you
0
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
Visual Basic.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.