Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need enter key to trigger event on aspnet html page

Posted on 2014-10-03
14
Medium Priority
?
238 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 

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 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

916 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