Click to See Complete Forum and Search --> : web form submit behavior in ASP.NET RESOLVED
soskinny
Aug 4th, 2004, 07:45 AM
How can I stop or catch or detect when a user hits enter in my web form. I have all of the database input code inside a submit button and if the user hits enter the form submits, is cleared and missed all of my code. Also the user hates the form submit on enter behavior.
Is there some way to detect an event argument or something in the page unload event (or some event) to check if it was triggered by enter and if so then go back? What is the asp.net code equivalent of the user hitting the back key?
I am hoping I do not need a client side javascript here because it seems like a lot of work... and I don't know javascript.
Thanks.
nemaroller
Aug 4th, 2004, 09:55 AM
ASP.NET is server-side technology. You can't stop the default submit on Enter action of many browsers,
without including client-side technology......... javascript.
Now, even though ASP.NET is server-side technology, you can still use it to emit client-side code (javascript)
into the response stream to the client.
The javascript to block a Enter is:
function blockEnter(evt)
{
if (evt.keyCode == 13)
{
if (evt.srcElement.tagName=="TEXTAREA")
return true;
return false;
}
}
Now, to call a javascript function when a key is pressed on a textbox, you add this line to code-behind page.
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
TextBox1.Attributes.Add("onKeyDown", "return blockEnter(event)")
This will render the textbox with a javascript 'onKeyDown' event on the client-side.
One more thing we have to do is get javascript code to the page. You can either type it directly into the aspx front side,
or add the following lines to the Page_PreRender function...
Dim myjavascriptstring As String = _
"<script language=""javascript"">function blockEnter(evt){if (evt.keyCode == 13){if (evt.srcElement.tagName==""TEXTAREA"")return true;return false;}}</script>"
Me.RegisterClientScriptBlock("c12323",myjavascriptstring)
Now, additionally, you should check to see if the TextBox is empty when the user clicks Submit, and not replace the text (instead offer a Clear button).
soskinny
Aug 4th, 2004, 10:01 AM
Thanks Nemaroller- you are a life saver. This looks perfect and do-able. I'll implement it. And buy Javascript for dummies! I can't ignore the performance of client side script any longer...
I'm still surprised that I can't simply detect the key press on the server side and then cancel the submit. I know it's not good practice and creates an extra round trip, though.
davidrobin
Aug 6th, 2004, 08:30 AM
soskinny
Did this work the way you wanted, I need to do a similar thing and would like to know.
soskinny
Aug 6th, 2004, 08:44 AM
Yes, this worked perfectly and was very easy to implement. It only took a few minutes to add to my page. The form doesn't react at all to the enter key being pressed while in a text box.
soskinny
Aug 6th, 2004, 08:45 AM
am I supposed to tag this thread solved? or indicate success? if so, someone please let me know how...
davidrobin
Aug 6th, 2004, 09:20 AM
You can edit your original post and change the subject field. Add the word RESOLVED to the end to let people see at a glance the question is solved.
Thanks
robertx
Jun 25th, 2005, 04:44 AM
I have a text box and a submit button. How do I capture the user hitting enter while the text box has focus and calling the submit button's click event?
I don't want to put the button's click event code in the Page_Load event as it causes other problems. Alternatively, I could put the button's click event code in the Page_Load event if I was able to detect which object/event caused the page to be submitted to the server.
soskinny
Jun 27th, 2005, 07:22 AM
Robertx-
you should scroll up and review nemaroller's advice. it worked perfectly for me. unless I misunderstand your question, that should do the trick.
robertx
Jun 27th, 2005, 06:23 PM
Thanks, soskinny. I used an adaptation of this code from another thread:
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
txtSearch.Attributes.Add("onkeydown", String.Format("if(window.event.keyCode == 13) {{{0}.focus();}}", cmdSearch.ClientID))
End Sub
and it did exactly what I wanted it to do. ie.calls the cmdSearch button click event when the user hits enter when txtSearch has focus.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.