在回发后在文本框中设置焦点

在回发后在文本框中设置焦点

问题描述:

我有一个简单的页面,我想根据文本框中的值筛选列表框 - 两者都在UpdatePanel中。
这工作正常,但是,回发后的文本框失去了焦点...所以我把焦点放回了page_load。 然后我注意到,当我最终需要光标时,光标现在位于文本的开头,所以用户可以继续打字,所以我在文本框中添加了onfocus(...)属性以将该值设置回本身(见下面的代码)。在回发后在文本框中设置焦点

这工作的前两次,但它然后停止设置焦点到文本框?

标记

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListTest.aspx.cs" Inherits="SalesForceTest.ListTest" %> 

<!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></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:ScriptManager ID="ScriptManager1" runat="server" LoadScriptsBeforeUI="true"/> 
     <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
      <ContentTemplate> 
       <asp:TextBox runat="server" ID="filter" AutoPostBack="true" onkeyup="__doPostBack(this.id, this.value)" onfocus="this.value = this.value;" /> 
       <br /> 
       <asp:ListBox ID="AccountList" runat="server" Width="185px"></asp:ListBox> 
      </ContentTemplate> 
     </asp:UpdatePanel> 
    </div> 
    </form> 
</body> 
</html> 

代码隐藏

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<string> allAccounts = new List<string> { "2342", "3434", "2332", "3224", "7899", "8797", "3435" }; 

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

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

任何帮助非常感激地接受:)

您需要设置光标/插入符号位置结束使用java脚本的文本。使用下面的js函数设置光标位置:

function setCaretTo(obj, pos) { 
    if(obj.createTextRange) { 
     /* Create a TextRange, set the internal pointer to 
      a specified position and show the cursor at this 
      position 
     */ 
     var range = obj.createTextRange(); 
     range.move("character", pos); 
     range.select(); 
    } else if(obj.selectionStart) { 
     /* Gecko is a little bit shorter on that. Simply 
      focus the element and set the selection to a 
      specified position 
     */ 
     obj.focus(); 
     obj.setSelectionRange(pos, pos); 
    } 
} 

源上面的代码:http://parentnode.org/javascript/working-with-the-cursor-position/

现在,你需要的是裁判您的客户端框对象(的document.getElementById)和文本长度( textbox.value.length)。调用启动脚本中的函数(通过ScriptManager.RegisterStartupScript注册)方法。

+0

嗯...我一定会错过一些东西! 这适用于前两个回调,就像以前一样,但后来停止工作? 如果我将控件移动到更新面板之外,则它始终保持工作状态。把它们放回到UpdatePanel中,它工作两次然后停止? – BlueChippy 2010-10-27 15:19:47

+0

我的怀疑是在线“if(Request.Form [”__ EVENTTARGET“== filter.ID)” - 也许你可以调试并检查设置焦点的代码是否被执行。 – VinayC 2010-10-28 05:36:51

+0

这似乎是每次都被击中......其“没有发生”的“集中焦点”。所以回发失去了控制的焦点和呼叫设置回来......不。 – BlueChippy 2010-10-28 07:20:54