Solved

Need enter key to trigger event on aspnet html page

Posted on 2014-10-03
14
207 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

896 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