Return Key in ASP.NET

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

On an asp.net page, when focus is in a text box and the user presses return, then a postback occurs and the first ImageButton on the page handles the event. I would prefer that nothing happens. I don't really understand why another control is handling the event anyway

Can anyone help me out with any tips or by pointing me to where I can get more information about understanding this behavior !?
 
I ran into a similar issue just last week.

This page I found explained the issue and some of the potential solutions :

http://www.allasp.net/enterkey.aspx

Personally, I chose to use <Button> rather than <asp:button>


Joe Erpenbeck said:
On an asp.net page, when focus is in a text box and the user presses
return, then a postback occurs and the first ImageButton on the page handles
the event. I would prefer that nothing happens. I don't really understand
why another control is handling the event anyway.
Can anyone help me out with any tips or by pointing me to where I can get
more information about understanding this behavior !?
 
Hi,


Thanks for posting in the community!
From your description, you'd like to avoid your page from being posted back
when a certain textbox is onfocus and the user pressed the "enter" key, yes?
If there is anything I misunderstood, please feel free to let me know.

Based on my experience, this is a normal behavior on a web page since when
"enter" key is pressed and no "submit" control such as <Input
type=submit..> is on fucus, the page will find a default submit html
element on page and fire its submit event(look for it by sequence). That's
why you found your first Imagebutton on the page always be fired the submit
event when "enter" key pressed and a certain entry field on focus.

One simple workaround to this problem is to set the Textbox's TextMode to
MulipleLine. Then ASP.NET will create the textbox as a TextArea on the
client side. Pressing Enter in a TextArea control will not cause it to post
back.

Another workaround is to use Client side script to cancel the Enter key
when it is press inside of the Textbox. For example the client side script
below checks if it is Enter pressed, and if it is, cancel it. You can copy
it to the ASPX page.

<SCRIPT>
function checkKey()
{
if (window.event.keyCode == 13) // checks whether the SHIFT key
// is pressed
{
window.event.cancelBubble = true;
window.event.returnValue = false;
alert("Canceled");
}
}
</SCRIPT>

Then you can hook the client side onKeyPress handler like this:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
TextBox2.Attributes.Add("onKeyPress", "javascript:checkKey();")
End Sub

In addition, here is some tech aritlces which discussing on the the related
problems:
#HOWTO: Prevent Form Submission When User Presses the ENTER Key on a Form
http://support.microsoft.com/?id=298498

#http://www.allasp.net/enterkey.aspx



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Back
Top