将焦点设置到回发后文本的文本框结束了吗? [英] Set focus to end of text in textbox after postback?

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

问题描述

我有与txtBox和BTN一个简单的ASP.Net的形式。结果
用户点击btn,它们将文本添加到一个ASP:文本框在回传(其加入了著名的首发文本结果。
回传后,我想将焦点设置在文本框中的文本的结尾。

I've got a simple ASP.Net form with txtBox and btn.
User click btn, which adds text to an ASP:TextBox in a postback (its adding a known "starter text".
After the postback I'd like the focus to be set to the end of the text in the textbox.

如果我叫Page.SetFocus(...)或txtBox.Focus(),则txtBox获得焦点,但在文本的开始 - 如果用户开始打字,这意味着,他们将在错误的地方

If I call Page.SetFocus(...) or txtBox.Focus() then the txtBox gets focus, but at the beginning of the text - which means if the user starts typing, they'll be in the wrong place.

例如。结果
光标 100-01

e.g.
cursor100-01

想这是

100-01 光标

我试过在文本框中以下内容:

I've tried the following in the textbox:

onfocus="alert('focus');this.value = this.value;"

但提醒只出现前两次?然后什么?

but the "alert" only appears the first two times? Then nothing?

推荐答案

我发现 asp.net网站(检查是否有关于特定解决方案的跨浏览器版本的讨论!)

I found a solution in asp.net website ( check it for discussion about cross browser version of given solution!)

存在的javascrip code,它做到这一点:

there is javascrip code that do it:

<script type="text/javascript">
        function SetCursorToTextEnd(textControlID)
        {
            var text = document.getElementById(textControlID);
            if (text != null && text.value.length > 0)
            {
                if (text.createTextRange)
                {
                    var FieldRange = text.createTextRange();
                    FieldRange.moveStart('character', text.value.length);
                    FieldRange.collapse();
                    FieldRange.select();
                }
            }
        }
</script>

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

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