[RESOLVED] Enter Key on Textfield
Hey there,
i have created a web form and on it are 3 read only textfields, 1 normal textfield and 4 buttons
When data is entered in the textfield i want to be able to call one of the buttons when they hit enter after typing in the data in the textfield.
How could i get this to work
Re: Enter Key on Textfield
C# Code:
Clicking enter in a textbox would normally call the default button on the page.
To override this you need to add some server side code
Code:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender (e);
TextBox1.Attributes.Add("onkeydown",string.Format("if(window.event.keyCode == 13) {{{0}.focus();}}",Button1.ClientID));
}
VB Code:
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
TextBox1.Attributes.Add("onkeydown", String.Format("if(window.event.keyCode == 13) {{{0}.focus();}}", Button1.ClientID))
End Sub
Note: Dont forget to replace "TextBox1" with the name of your textbox and "Button1" with the name of the button you want to call.
:wave:
Re: Enter Key on Textfield
whats the default button on the page.... could i even set this
that code worked great...
what exactly does the prerender event do
Re: Enter Key on Textfield
Forgive me for my incorrect use of terminology - but there is no such thing as a default button for the page.
However I've noticed a very intresting thing:
I took a blank page and added a text box to it. During runtime when I type something in the textbox and press "Enter" the page posts back.
Now I add a button to the page. Again during runtime the same thing happens.
Next I added a second text box to the page. Now when I press enter in any of the textboxes, the page posts back and the Click event of the Button is fired.
Now add a second button to the page. Pressing Enter in any of the textboxes still fires the click event of the first button but not for the second button.
:confused:
Does anyone know why?