Javascript - Utilizzare INVIO(ENTER) come TAB in una WebForm
Come utilizzare il tasto INVIO(ENTER) (come TAB) per passare da un campo ad un altro di una WebForm
Use Enter Key like Tab for move focus in a WebForm
In questo esempio viene mostrato come utilizzare il tasto INVIO(ENTER) (come TAB) per passare da un campo ad un altro di una WebForm.
Tested on IE8 - IE7 - IE6 - FF3.0
>>
File Default.aspx
<%@ 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 id="Head1" runat="server">
<title>ProgrammaSubito.it</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<script type="text/javascript">
var push =false;
var nextFocus = "";
document.onkeydown = function(e)
{
var evento = window.event ? window.event : e;
switch(document.activeElement.id)
{
case "TextBox_Nome":
case "TextBox_Cognome":
case "TextBox_Titolo":
var code = evento.keyCode;
if(code==13)
{
document.getElementById(nextFocus).focus();
return false;
}
break;
case "Button2":
var code = evento.keyCode;
if(!push)
{
return false;
}
break;
}
}
function nextFocusWithEnter(elemID)
{
push = false;
nextFocus = elemID;
}
function pushButton2()
{
push = true;
}
</script>
<asp:Panel ID="Panel1" runat="server" Width="500px">
<asp:TextBox ID="TextBox_Nome" runat="server"
TabIndex="1" onkeydown="nextFocusWithEnter('TextBox_Cognome')" />
<br /><br />
<asp:TextBox ID="TextBox_Cognome" runat="server"
TabIndex="2" onkeydown="nextFocusWithEnter('TextBox_Titolo')" />
<br /><br />
<asp:TextBox ID="TextBox_Titolo" runat="server"
TabIndex="3" onkeydown="nextFocusWithEnter('Button2')" />
<br /><br />
<asp:Button ID="Button2" runat="server" Text="Button"
OnClick="Button1_Click" onkeydown="pushButton2()" TabIndex="4" />
<br /><br />
</asp:Panel>
<asp:Panel ID="Panel2" runat="server" Width="500px">
<asp:Label ID="Label1" runat="server" />
<br />
<asp:Label ID="Label2" runat="server" />
<br />
<asp:Label ID="Label3" runat="server" />
<br />
</asp:Panel>
</div>
</form>
</body>
</html>
File Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox_Nome.Focus();
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = TextBox_Nome.Text;
Label2.Text = TextBox_Cognome.Text;
Label3.Text = TextBox_Titolo.Text;
TextBox_Nome.Text = "";
TextBox_Cognome.Text = "";
TextBox_Titolo.Text = "";
TextBox_Nome.Focus();
}
}