将焦点设置在文本框后回传 [英] Set focus in TextBox after postback

查看:166
本文介绍了将焦点设置在文本框后回传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的页面,在这里我要过滤基于在一个文本框的值(S)的列表框 - 都是在一个UpdatePanel结果
这工作正常,然而,回发的文本框已经失去焦点后...所以我把焦点放回在Page_Load。
然后,我注意到光标是现在,当我想它在最后,使用户可以打字承载文本的开始,所以我增加了一个的onfocus(...)属性文本框将值设置回自己(见code以下)。

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.
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).

这工作的第2次,但随后停止将焦点设置到文本框?

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

标记

<%@ 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>

codebehind

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功能设定光标位置:

You need to set the cursor/caret position at the end of text using java-script. Use below js function for the setting cursor position:

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); 
    } 
} 

以上code来源: http://parentnode.org / JavaScript的/工作与 - 的光标位置/

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

Now, what you need is ref to your client-side textbox object (document.getElementById) and text length (textbox.value.length). Call the function in the start-up script (registered via ScriptManager.RegisterStartupScript) method.

这篇关于将焦点设置在文本框后回传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆