I have a problem with focus after postbacks on the page, when there is autosuggest on the page.
I use __doPostBack() function from javascript and use focus() from javascript to focus on the next control on the page. But after 2 postbacks that doesn't work. The whole form seems to have lost focus. I use javascript function to change focus between controls using enter, and it doesn't work after 2 postbacks.
I'm using Visual Studio 2008, ASP .NET 3.5 AJAX Application and autosuggest version 2.2
Here is the code of the small project i made to demonstrate that problem:
Default.aspx
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FixFocusTest._Default" %>
<%@ Register assembly="ConvincingMail.AdvancedAutoSuggest" namespace="ConvincingMail.AdvancedAutoSuggest" tagprefix="cc3" %>
<!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>FixFocus Test Page 1</title>
<script src="prototype-1.6.0.2.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
//focus na Enter
function focusNext(field, event) {
var mainForm = document.forms[0];
if (event.keyCode == 13 && event.shiftKey == false) {
for (i = 0; i < mainForm.elements.length; i++)
if ((mainForm.elements[i].tabIndex >= field.tabIndex + 1)
&& (isVisibleOrDisabled(mainForm.elements[i]) == false)
&& (isRequiredControl(mainForm.elements[i]) != false)) {
mainForm.elements[i].focus();
if (mainForm.elements[i].type == "text")
mainForm.elements[i].select();
break;
}
return false;
}
if (event.keyCode == 13 && event.shiftKey == true) {
for (i = mainForm.elements.length - 1; i >= 0; i--) {
if (mainForm.elements[i].tabIndex <= field.tabIndex - 1
&& (isVisibleOrDisabled(mainForm.elements[i]) == false)
&& (isRequiredControl(mainForm.elements[i]) != false)
&& (mainForm.elements[i].tabIndex != 0)) {
mainForm.elements[i].focus();
if (mainForm.elements[i].type == "text")
mainForm.elements[i].select();
break;
}
}
}
return true;
}
function isVisibleOrDisabled(field) {
if (field.style.display == "none" || field.style.visibility == "hidden" || field.disabled == true)
return true;
else
return false;
}
function isRequiredControl(ctl) {
//if ((ctl.type == "text") || (ctl.type == "select-one"))
if (ctl.type != "hidden")
return true;
else
return false;
}
////////kraj focusNext
function txtChanged(tBox, evt) {
if (tBox.value != '')
__doPostBack(tBox.id, '');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
Text1: <asp:TextBox ID="TextBox1" runat="server" TabIndex="1"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<br />
Text2: <asp:TextBox ID="TextBox2" runat="server" TabIndex="2"
ontextchanged="TextBox2_TextChanged"></asp:TextBox>
<br />
Text3: <asp:TextBox ID="TextBox3" runat="server" TabIndex="3"
ontextchanged="TextBox3_TextChanged"></asp:TextBox>
<br />
Text 4: <asp:TextBox ID="TextBox4" runat="server" TabIndex="4" ></asp:TextBox>
<br />
ASG: <asp:TextBox ID="TextBox5" runat="server" TabIndex="5" ></asp:TextBox>
<cc3:AdvancedAutoSuggestExtender ID="asgEsercizio" runat="server" TargetControlID="TextBox5"
ServiceUrl="~/WebService1.asmx/GetEsercizio" UpdateField="hf1" ></cc3:AdvancedAutoSuggestExtender>
<asp:HiddenField ID="hf1" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Default.aspx.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FixFocusTest
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.GetCurrent(Page).Scripts.Add(new ScriptReference("FixFocus.js"));
if (!Page.IsPostBack)
{
TextBox1.Attributes.Add("onkeypress", "focusNext(this, event)");
TextBox2.Attributes.Add("onkeypress", "focusNext(this, event)");
TextBox3.Attributes.Add("onkeypress", "focusNext(this, event)");
TextBox4.Attributes.Add("onkeypress", "focusNext(this, event)");
TextBox5.Attributes.Add("onkeypress", "focusNext(this, event)");
TextBox1.Attributes.Add("onchange", "txtChanged(this,event)");
TextBox2.Attributes.Add("onchange", "txtChanged(this,event)");
TextBox3.Attributes.Add("onchange", "txtChanged(this,event)");
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
string s = "";
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
string s = "";
}
protected void TextBox3_TextChanged(object sender, EventArgs e)
{
string s = "";
}
}
}
Notice that when i remove autosuggest from the page everything works perfectly.
If anyone wants the entire project i can mail them.