1,105,332 Community Members

i want asp.net code for Keydown event

Member Avatar
bansarisavaliya
Newbie Poster
15 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
JorgeM
IT Addict
6,413 posts since Dec 2011
Reputation Points: 581 [?]
Q&As Helped to Solve: 963 [?]
Skill Endorsements: 172 [?]
Moderator
Featured
Sponsor
 
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...

Member Avatar
bansarisavaliya
Newbie Poster
15 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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>

Member Avatar
JorgeM
IT Addict
6,413 posts since Dec 2011
Reputation Points: 581 [?]
Q&As Helped to Solve: 963 [?]
Skill Endorsements: 172 [?]
Moderator
Featured
Sponsor
 
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>
Member Avatar
bansarisavaliya
Newbie Poster
15 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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>
Member Avatar
JorgeM
IT Addict
6,413 posts since Dec 2011
Reputation Points: 581 [?]
Q&As Helped to Solve: 963 [?]
Skill Endorsements: 172 [?]
Moderator
Featured
Sponsor
 
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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article