How to move to the next textbox after pressing Enter

esposito

Centurion
Joined
Jul 11, 2003
Messages
103
Location
Perugia - Italy
Hello, I'm new to ASP.NET. I'd like to know if it is possible, while you are typing in a textbox, to set the focus to the next textbox pressing Enter on the keyboard.

Any help will be appreciated.

TIA
 
Dear brother,Pasquale
Every TextBox Control has a property called TabIndex.
this item specifies which textbox or Server Side control must be focuced on!(1one 2nd ,...)
By default when ever u press enter the unique Button fires.
So theorically u need to write or override new Event for pressing Enter!
Correct me profs.:) ;)
 
You'll have to verify that OnKeyPress if the key is <ENTER> (not written like that... you know what I mean).

If it's <ENTER> you'll have to search the next TextArea (or text) and use .Focus() on it.
This way... everything will work... at least... I think ! ;)
 
Dear Arch4ngel, I'm not talking about Windows forms but Web forms and, as far as I know, the Focus() method and the onKeyPress event do not exist when dealing with server controls. Please correct me if I am mistaken.

Arch4ngel said:
You'll have to verify that OnKeyPress if the key is <ENTER> (not written like that... you know what I mean).

If it's <ENTER> you'll have to search the next TextArea (or text) and use .Focus() on it.
This way... everything will work... at least... I think ! ;)
 
Dear shahab, I have set the TabIndex property to 1 for TextBox 1 and to 2 for TextBox2. Then I have a button whose TabIndex is set to 3. In spite of this, if I press Enter when the focus is on TextBox1, the Click event of the button is executed.

How could I amend this problem?

TIA

shahab said:
Dear brother,Pasquale
Every TextBox Control has a property called TabIndex.
this item specifies which textbox or Server Side control must be focuced on!(1one 2nd ,...)
By default when ever u press enter the unique Button fires.
So theorically u need to write or override new Event for pressing Enter!
Correct me profs.:) ;)
 
I was much more talking about Javascript.
I don't think you can code an override of the TAB function unless you are on the client side.

And yes... focus() exists on TextBox but... with JS.

Server-Side isn't made for overriding this kind of thing.
 
try this:(by changing):
Example
[Visual Basic, C#, C++] The following example sets focus to the specified Control, if it can receive focus.

[Visual Basic]
Public Sub ControlSetFocus(control As Control)
' Set focus to the control, if it can receive focus.
If control.CanFocus Then
control.Focus()
End If
End Sub

[C#]
public void ControlSetFocus(Control control)
{
// Set focus to the control, if it can receive focus.
if(control.CanFocus)
{
control.Focus();
}
}

[C++]
public:
void ControlSetFocus(Control* control) {
// Set focus to the control, if it can receive focus.
if (control->CanFocus) {
control->Focus();
}
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button in the upper-left corner of the page.
 
UI Tip

LANGUAGES: VB .NET

ASP.NET VERSIONS: 1.0 | 1.1


Get Focused

Make keyboard-friendly forms by ensuring the control you want has focus when the page loads.


By Brad McCabe


One of the most common questions I get asked by developers is how to set focus to a particular control when a form loads. By setting focus to a control you enable the end user to begin typing instead of having to click on a form or control. With a little code, you can even make this dynamic and data-driven.


To make this possible you need to use the ClientID property of any ASP.NET control. With the correct script, you can call the HTML element's Focus method with some JavaScript code using the ClientID as the bridge between the server-side processing and the client-side code.


Add the following procedure to your Web Form or your custom base class (see related tip on custom base classes at http://www.aspnetpro.com/NewsletterArticle/2003/05/asp200305mc_l/asp200305mc_l.asp):


Private Sub SetFocus(ByVal FocusControl As Control)

Dim Script As New System.Text.StringBuilder

Dim ClientID As String = FocusControl. ClientID



With Script

.Append("<script language='javascript'>")

.Append("document.getElementById('")

.Append(ClientID)

.Append("').focus();")

.Append("</script>")

End With



RegisterStartupScript("setFocus", Script.ToString())

End Sub


Before you use this function, let's look at what it's doing. Start by passing a reference into the control that you want to utilize. The function uses this reference to obtain the ClientID of the control.


After obtaining the ClientID the function builds a simple JavaScript line to get a pointer to the HTML object that the control will render on the client side. With the pointer the JavaScript will call the focus method of the object to set focus to it.


After the JavaScript is written the function will use the RegisterStartupScript method from ASP.NET to cause this code to fire when the page loads in the client side browser (see related tip at http://www.aspnetpro.com/NewsletterArticle/2003/05/asp200305mb_l/asp200305mb_l.asp).


Now that you know what the SetFocus function does, let's look at two ways to use it. The simplest approach is to call the function to set focus to control on the page load event:


Private Sub Page_Load( ... )

If IsPostBack Then Exit Sub

SetFocus(TextBox2)

End Sub


In this code, the TextBox2 control receives focus when the page loads. This could be done dynamically or in response to an event, as the following command button code shows:


Private Sub Button1_Click( ... )

SetFocus(TextBox3)

End Sub


This code will set the focus to TextBox3 whenever Button1 is clicked.


With a few easy lines of server-side code you can control the focus on a Web Form as the page loads in the browser. Setting focus to a control lets you make your application interface more productive and more keyboard-friendly for data entry.


Got a UI question, tip, or idea for a topic you'd like me to cover? I'd love to hear it. E-mail me at mailto:uitips@aspnetPRO.com.


This article's sample code is available for download.


Brad McCabe is the technical evangelist for Infragistics. Brad also has been a systems architect and consultant for Verizon Communications and many other clients, and he was a leading .NET evangelist within Ajilon Consulting. His primary interests include ASP.NET, Windows CE .NET, .NET Compact Framework, and Microsoft's networking technologies. E-mail him at mailto:uitips@aspnetPRO.com.
 
Thanks, everybody. So, the problem with the SetFocus method has now been solved. I have tested it and found it works perfectly.

And what about the second issue? Do you know how to trap the Enter key while typing in a textbox and make it act as if it were a Tab?

Thanks again.
 
Back
Top