1

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

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?

ken1nil
  • 915
  • 3
  • 9
  • 21
  • Just to clarify: The correct text box is getting focused, and the problem is that the cursor is placed at the start of the text box instead of at the end? – Jan Aagaard Apr 23 '10 at 06:40
  • What does the SetFocus function do? Normally you would write ctrl.Focus(). – Jan Aagaard Apr 23 '10 at 06:42
  • Minor note: String.IsNullOrEmpty() is a bit more efficient than comparing to an empty string. – Jan Aagaard Apr 23 '10 at 07:29
  • Jan Aagaard: Yes the correct textbox is focused, but cursor is placed at the start. The method SetFocus() is within System.Web.UI.Page and does the same as ctrl.Focus() it seems (no change to cursor placement). Will use string.IsNullOrEmpty() instead, thanks for noticing. – ken1nil Apr 23 '10 at 08:46

2 Answers2

1

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

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");
    }
}
Community
  • 1
  • 1
Jan Aagaard
  • 10,940
  • 8
  • 45
  • 80
  • The answer maybe is there, I'm a total beginner at Javascript and JQuery. I cant figure out how to call the Javascript function(s) from code-behind. And the Javascript cant use a "Fixed" textbox, It has to take the textbox I choose also from Code-behind. – ken1nil Apr 23 '10 at 12:15
  • 1
    You can't really call JavaScript function from code behind. But you can use code behind to generate a JavaScript function that will be called when the page loads. This is what `Page.SetFocus()` does. – Jan Aagaard May 01 '10 at 21:54
0

Shouldn't it be

if (((TextBox)ctrl).Text == string.Empty)

to advance to the bottommost empty textbox? If it is if (((TextBox)ctrl).Text != string.Empty), it will set focus (eventually) to the bottommost nonempty textbox.

As for advancing your cursor to the last character, try this:

function cursorToEnd( elem )
{
  elem.focus();
  elem.value+='x';
  elem.value=elem.value.replace(/x$/,"");   
}
rlb.usa
  • 14,942
  • 16
  • 80
  • 128
  • No right now it's correct that I want the focus on the bottommost nonempty textbox. How do I call on the Function, and the function take the textbox i choose from code-behind? – ken1nil Apr 23 '10 at 12:17