We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,497 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

i want asp.net code for Keydown event

Hello,
any one can help me " i want that When i press ENTER key control focus on next tab in asp.net

2
Contributors
5
Replies
11 Hours
Discussion Span
2 Months Ago
Last Updated
16
Views
bansarisavaliya
Newbie Poster
13 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Rather than asp.net, you should handle this client side using javascript or easier with jQuery. Here is an example ...

 $(document).keypress(function(e) {
     if(e.which == 13) {
         alert('You pressed enter!');
     }
 });

Instead of the alert box, use the jQuery focus method to give the target element focus, or show/hide divs, etc...

JorgeM
Industrious Poster
4,026 posts since Dec 2011
Reputation Points: 297
Solved Threads: 549
Skill Endorsements: 115

Hello JorgeM,

I use this code but it nor work

<script language ="javascript" type ="text/javascript">

    $(document).keypress(function (e) {
        alert('hello');
        if (e.which == 13) {
            alert('You pressed enter!');
        }
            });


</script>

<body>
<form id="form1" runat="server">
<div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>

</body>

bansarisavaliya
Newbie Poster
13 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

So, if you are not familiar with jQuery, you may want to do some reading up on it. Its a very powerful JavaScript library. Very easy to use.

In your example, you may have forgotten to reference the jQuery library. Here is a complete example which you can adapt to your needs.

<%@ Page Language="VB" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>Demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
    <asp:Button ID="Button1" runat="server" Text="Button" />
</form>

<script>
    $(document).keypress(function (e) {
        if (e.which == 13) {
            alert('You pressed enter!');
        } else {
            alert('You did not press enter!');
        }
    });
</script>
</body>
</html>
JorgeM
Industrious Poster
4,026 posts since Dec 2011
Reputation Points: 297
Solved Threads: 549
Skill Endorsements: 115

Hello JorgeM,

Thnaks for your time but this code still not working

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type ="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>

</body>
<script type ="text/javascript">
    $(document).keypress(function (e) {
        if (e.which == 13) {
            e.keyCode = 9;
        } else {
            alert('You did not press enter!');
        }
    });
</script>
bansarisavaliya
Newbie Poster
13 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

The first issue is that you have the script block outside of the closing body tag. Secondly to switch the "enter" key for "tab", you'll need to do some additional reasearch. I havent had time to test this myself, but did a quick search and found a solution online which indicates working, tested code.

JorgeM
Industrious Poster
4,026 posts since Dec 2011
Reputation Points: 297
Solved Threads: 549
Skill Endorsements: 115

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0838 seconds using 2.66MB