Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

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>
0
Dave_CARSIT
Asked:
Dave_CARSIT
  • 8
  • 6
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now