|
-
Aug 4th, 2004, 07:45 AM
#1
Thread Starter
Member
web form submit behavior in ASP.NET RESOLVED
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.
Last edited by soskinny; Aug 6th, 2004 at 09:43 AM.
-
Aug 4th, 2004, 09:55 AM
#2
I wonder how many charact
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:
Code:
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.
VB Code:
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...
VB Code:
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).
Last edited by nemaroller; Aug 4th, 2004 at 10:00 AM.
-
Aug 4th, 2004, 10:01 AM
#3
Thread Starter
Member
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.
-
Aug 6th, 2004, 08:30 AM
#4
Fanatic Member
soskinny
Did this work the way you wanted, I need to do a similar thing and would like to know.
-
Aug 6th, 2004, 08:44 AM
#5
Thread Starter
Member
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.
-
Aug 6th, 2004, 08:45 AM
#6
Thread Starter
Member
am I supposed to tag this thread solved? or indicate success? if so, someone please let me know how...
-
Aug 6th, 2004, 09:20 AM
#7
Fanatic Member
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
-
Jun 25th, 2005, 04:44 AM
#8
Frenzied Member
Re: web form submit behavior in ASP.NET RESOLVED
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.
-
Jun 27th, 2005, 07:22 AM
#9
Thread Starter
Member
Re: web form submit behavior in ASP.NET RESOLVED
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.
-
Jun 27th, 2005, 06:23 PM
#10
Frenzied Member
Re: web form submit behavior in ASP.NET RESOLVED
Thanks, soskinny. I used an adaptation of this code from another thread:
VB Code:
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|