Solved

Need enter key to trigger event on aspnet html page

Posted on 2014-10-03
14
201 Views
Last Modified: 2014-10-03
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>
0
Comment
Question by:Dave_CARSIT
  • 8
  • 6
14 Comments
 
LVL 11

Expert Comment

by:Guru Ji
ID: 40359369
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
 

Author Comment

by:Dave_CARSIT
ID: 40359422
Am I adding or replacing lines?
0
 
LVL 11

Expert Comment

by:Guru Ji
ID: 40359448
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
 

Author Comment

by:Dave_CARSIT
ID: 40359552
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
 
LVL 11

Expert Comment

by:Guru Ji
ID: 40359579
Hi Dave,

Can you post your html file or paste your entire page code ?
0
 

Author Comment

by:Dave_CARSIT
ID: 40359604
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
 
LVL 11

Expert Comment

by:Guru Ji
ID: 40359646
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
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.

 

Author Comment

by:Dave_CARSIT
ID: 40359781
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
 
LVL 11

Accepted Solution

by:
Guru Ji earned 500 total points
ID: 40359873
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
 

Author Comment

by:Dave_CARSIT
ID: 40359995
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
 

Author Comment

by:Dave_CARSIT
ID: 40360033
You are the best!!! Slight typo on my first try.

Thank you so much!!!!!!
0
 

Author Closing Comment

by:Dave_CARSIT
ID: 40360038
Absolutely, the best, quickest and most professional advice I have received!
0
 
LVL 11

Expert Comment

by:Guru Ji
ID: 40360130
Hey Glad It worked. So if it working when page is closed and reopened ?

you can remove that JavaScript function of doClick.
0
 

Author Comment

by:Dave_CARSIT
ID: 40360136
Works perfectly!

Thank you
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Text in tooltip bound field bridview 3 30
GridDataView 6 45
Library to convert HTML to PDF. 8 46
VB.Net - CSV to Oracle table 4 32
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

13 Experts available now in Live!

Get 1:1 Help Now