Using Enter to move to next field on a formview

I have an asp.net web site using ajax.asp 1.0. I am using many formview controls for inserting and editing records. The customer has requested that the enter key moves to the next field in the formview instead of executing the insert/update command. The only way I can see this happening is using client side jscript to capture the enter key in some event and switch the focus.

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;
}
}
}
[4966 byte] By [wmain] at [2007-11-20 8:57:22]
# 1 Re: Using Enter to move to next field on a formview
I've found a partial solution. I can use the ScripManager's SetFocus method instead of the Page's SetFocus. This works fine when there has been a change made to the control. In the control's TextChanged event I can call the ScriptManager's SetFocus method and this will result in setting the focus to the next control. However, when there has not been a change, pressing enter results in the cursor caret going into limbo. How do I control the focus of the cursor on a callback/postback? Setting MaintainScrollPositionOnPostBack to true does not seem to do anything.
wmain at 2007-11-9 11:53:34 >