Set focus in TextBox after postback
<p>I have a simple page where I want to filter a ListBox based upon a value(s) in a textbox - both are in an UpdatePanel.<br>
This works correctly, however, after the postback the textbox had lost focus...so I set focus back in the page_load.
Then I noticed that the cursor was now at the beginning of the text when I want it at the end so the user can carry on typing, so I added an onfocus(...) attribute to the textbox to set the value back to itself (see code below).</p>

<p>This works the first two times, but then it stops setting focus to the textbox?</p>

<p><strong>Markup</strong></p>

<pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListTest.aspx.cs" Inherits="SalesForceTest.ListTest" %&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head runat="server"&gt;
    &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
    &lt;div&gt;
        &lt;asp:ScriptManager ID="ScriptManager1" runat="server" LoadScriptsBeforeUI="true"/&gt;
        &lt;asp:UpdatePanel ID="UpdatePanel1" runat="server"&gt;
            &lt;ContentTemplate&gt;
                &lt;asp:TextBox runat="server" ID="filter" AutoPostBack="true" onkeyup="__doPostBack(this.id, this.value)" onfocus="this.value = this.value;"  /&gt;
                &lt;br /&gt;
                &lt;asp:ListBox ID="AccountList" runat="server" Width="185px"&gt;&lt;/asp:ListBox&gt;
            &lt;/ContentTemplate&gt;
        &lt;/asp:UpdatePanel&gt;
    &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p><strong>Codebehind</strong></p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace SalesForceTest
{
    public partial class ListTest : System.Web.UI.Page
    {
        List&lt;string&gt; allAccounts = new List&lt;string&gt; { "2342", "3434", "2332", "3224", "7899", "8797", "3435" };

        protected void Page_Load(object sender, EventArgs e)
        {
            AccountList.Items.Clear();
            allAccounts.Where(ac =&gt; ac.StartsWith(filter.Text)).ToList().ForEach(a =&gt; AccountList.Items.Add(a));

            if (Page.IsPostBack)
            {
                if (Request.Form["__EVENTTARGET"] == filter.ID)
                {
                    ScriptManager1.SetFocus(filter);
                }
            }
        }
    }
}
</code></pre>

<p>Any help <strong>VERY</strong> gratefully received :)</p>
