Using Enter to move to next field on a formview
At first I thought about using server side except there is no event I can find on the server side that is fired on a single keystroke.
Any ideas?
I've been able to move using client side script but as soon as I invoke a server side postback on the textchanged event of the control, the caret ends up in limbo after the event handler finishes.
Here is an example of a page and the code behind
aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" RenderMode="Inline" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="pnlEdit" runat="server" Wrap="False">
<asp:Label ID="Label1" runat="server" Text="text 1"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox_TextChanged" Wrap="False" AutoPostBack="True"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="text 2"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" OnTextChanged="TextBox_TextChanged" Wrap="False" AutoPostBack="True"></asp:TextBox><br />
<asp:Label ID="Label3" runat="server" Text="text 3"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server" OnTextChanged="TextBox_TextChanged" Wrap="False" AutoPostBack="True"></asp:TextBox></asp:Panel>
<asp:Label ID="lblTest" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Services;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
if (SetNextControlOnEnter != null)
Page.SetFocus(SetNextControlOnEnter);
}
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterClientScriptBlock(typeof(TextBox), "OnKeyPress", @"
function movetonextonenter(where)
{
var evtobj=window.event? event : e //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
var unicode=evtobj.charCode? evtobj.charCode : evtobj.keyCode
var actualkey=String.fromCharCode(unicode)
if (unicode==13)
where.focus();
}
",true);
SetNextOnEnter(TextBox1, TextBox2.ClientID);
SetNextOnEnter(TextBox2, TextBox3.ClientID);
SetNextOnEnter(TextBox3, TextBox1.ClientID);
}
void SetNextOnEnter(TextBox tb, string Where)
{
string js = "movetonextonenter(" + Where + ")";
tb.Attributes.Add("onkeypress",js);
}
TextBox SetNextControlOnEnter
{
get { return (TextBox)Session["SetFocus"]; }
set { Session["SetFocus"] = value; }
}
protected void TextBox_TextChanged(object sender, EventArgs e)
{
lblTest.Text = ((TextBox)sender).ClientID;
switch (((TextBox)sender).ClientID)
{
case "TextBox1":
SetNextControlOnEnter = TextBox1;
break;
case "TextBox2":
SetNextControlOnEnter = TextBox2;
break;
case "TestBox3":
SetNextControlOnEnter = TextBox1;
break;
}
}
}

