代码之家  ›  专栏  ›  技术社区  ›  BlueChippy

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

  •  2
  • BlueChippy  · 技术社区  · 14 年前

    我有一个简单的页面,我想根据文本框中的值过滤一个列表框-两者都在UpdatePanel中。
    然后我注意到光标现在在文本的开头,而我希望它在结尾,这样用户就可以继续输入了,所以我在文本框中添加了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);
                    }
                }
            }
        }
    }
    

    有什么帮助吗 非常感谢:)

    1 回复  |  直到 14 年前
        1
  •  1
  •   VinayC    14 年前

    您需要使用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/

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