ENTER key to submit forms

S

Sandra Castellanos

Hello,

I want to know what I have to do to make the enter key to submit forms. I
read in another newsgroup that inserting an html hidden text box made the
trick, and it actually does, but in my page I have more than one web control
that have their independient "forms" (in the .NET environment, it makes it
to be just one). And I need that, if the user is, say, writing the keywords
to perform a search, the enter key submits this form, but if he is writing
his username and password to login, the enter key submits this other
one........

Does anyone know how to do this?

Best Regards,

SC
 
S

S. Justin Gengo

Sandra,

I've created a javascript component with some commonly used javascripts in
it. One of them does exactly what you are looking for.

The component is free, comes with a help file, and the entire projects
source code is available on my web site: www.aboutfortunate.com. Just click
on the code library link and then click on the "Javascript" button in the
menu on the left.

Even if you don't want to use the entire component the code you need to
create your own is in the project.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
S

Sandra Castellanos

Hi,

Thank you very much for the code, but I still have one problem, and it is
that .NET is the one that builds the form, and it always gives it the name
of the page, for example if the page is Default.aspx, the name of the form
will be FormDefault. These "forms" I have are in web controls, and they are
included in many different pages. So I can't use the name of the form like
that.... Is there anything like document.forms(0).thebutton?

Thanks again and bye

SC
 
S

Sandra Castellanos

I'm sorry, could you please tell me how to attach this eventhandler to my
textboxes? I am working with <asp:textbox>. does it work with them?
Sorry :'(

SC
 
S

S. Justin Gengo

Sandra,

No problem! :)

It's pretty easy. Once you've dragged the component onto the page switch to
the code behind page.

If you didn't change the default name of the component when it was placed on
the page you would refer to it as: Javascript1

And you can attach it to as many text boxes as you want like this:

'---Page is the page object literally type "Page" there.
'---Textbox1 is the id of your text box.
'---Button1 is the id of the button you want to fire when the enter key is
' pressed while in this text box.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Javascript1.TextBoxEnterEventHandler(Page, TextBox1, Button1)
Javascript1.TextBoxEnterEventHandler(Page, TextBox2, Button2)
End Sub

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
S

Sandra Castellanos

Does it work if the user has the autocomplete enabled? In the page I added
the eventhandler, if I have autocomplete enabled, and I select an item from
the things with which I had filled that textbox before and I hit enter (just
as I always do), then it just does nothing, the form goes cleared and the
buttons don't work anymore.
 
S

S. Justin Gengo

Sandra,

It only works if the textbox has focus. That's the way javascript works. If
you don't click in the text box or move the cursor when you use autocomplete
to select something the text box doesn't have focus the autocomplete drop
down does. I have no idea how to attach a javascript to that.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
B

Bill Cohagan

On a related note, I'm interested in AVOIDING having the form submitted when
a user hits the ENTER key (say just after selecting an Option button). Is
there a way to force the user to click the Submit button instead?

Thanks in advance,
Bill
 
S

Sandra Castellanos

Sorry, I think there is a missunderstanding here. If the user has
autocomplete enabled in his browser, when the focus is in a textbox the
browser sometimes shows it the diferent things with which he has completed
that field, the user can, pressing the arrows select one of those and with
enter that item gets selected. All the time, the focus has been in the text
box. Now, if I have this handler for the keypressed event, that event is
raised when the user selects one word to fill the box. That's what is
happenning to me now
 
S

S. Justin Gengo

Bill,

You could take the same script and instead of having it click the button
when the javascript senses that the enter key is pressed you could return
"false" which would make it as if the enter key was never pressed.

Something like this:


TextBox.Attributes.Add("onkeydown", "javascript:if((event.which &&
event.which == 13) || (event.keyCode && event.keyCode == 13)){return
false;}else return true;")



--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top