将焦点设置文本框后回传 [英] Set focus on textbox after postback

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

问题描述

我有3文本框搜索页面,用户可以过滤与搜索。

I have a search page with 3 TextBoxes that users can filter a search with.

我已经把重点放在包含文本文本框。如果有多个包含文本只注重最后的文本框。

I have put the focus on the TextBox that contains text. If more than one contains text just focus on last TextBox.

private void SetFocusOnTextBox(ControlCollection ctrlCollection)
{
    foreach (Control ctrl in ctrlCollection)
    {
        if (ctrl.GetType() == typeof(TextBox))
        {
            if (((TextBox)ctrl).Text != string.Empty)
            {
                SetFocus(ctrl);
            }
        }
    }
}

在code运行和用户搜索后,焦点涉及到文本框的开始,而不是结束的地方将是presumed。如何把插入标记在该文本框的结束?

After the code runs and a user searches, the focus comes to the beginning of the TextBox, not the end where it would be presumed. How to put insert marked at the end of that TextBox?

推荐答案

我想答案就在这里:<一href=\"http://stackoverflow.com/questions/511088/use-javascript-to-place-cursor-at-end-of-text-in-text-input-element\">Use JavaScript来光标放置在文本输入元素文本结束。

I think the answer is here: Use JavaScript to place cursor at end of text in text input element.

从链接溶液中取出:你有的onfocus =THIS.VALUE = THIS.VALUE添加到三个控件的标记文件。这不是那么容易的ASP.NET理所应当的,而是一个解决方案是增加属性的隐藏文件code。

Taken from the linked solution: You have to add onfocus="this.value = this.value" to the three controls in the markup file. This is not so easy in ASP.NET as it should be, but one solution is to add the attribute in the code behind file.

protected void Page_Init(object sender, EventArgs e)
{
    // MoveCursorToEndOnFocus is called in Page_Init and not Page_Load to avoid
    // filling the ViewState with unnecessary data.
    // TODO: Call MoveCursorToEndOnFocus here.
}

private void MoveCursorToEndOnFocus(ControlCollection controlCollection)
{
    foreach (TextBox textBox in controlCollection
        .Where(control => control.GetType() == typeof(TextBox)))
    {
        textBox.Attributes.Add("onchange", "this.value = this.value");
    }
}

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

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