473,385 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Return Key in ASP.NET

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 !?
Nov 18 '05 #1
2 2818
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" <Jo***********@Computacenter.com.nospam> wrote in message
news:04**********************************@microsof t.com...
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 !?
Nov 18 '05 #2
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

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
My first time working with a PHP class, and after 6 hours of working out the kinks I am unable to return a value from the class, so now I appeal to the general audience what on earth did I do wrong...
20
by: Jakob Bieling | last post by:
Hi! I am using VC++ 7.1 and have a question about return value optimization. Consider the following code: #include <list> #include <string> struct test {
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
2
by: PengYu.UT | last post by:
I have the following sample program, which can convert function object with 1 argument into function object with 2 arguments. It can also do + between function object of the same type. The last...
2
by: Rhino | last post by:
I am trying to verify that I correctly understand something I saw in the DB2 Information Center. I am running DB2 Personal Edition V8.2.1 on Windows. I came across the following in the Info...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
10
by: Mark Jerde | last post by:
I'm trying to learn the very basics of using an unmanaged C++ DLL from C#. This morning I thought I was getting somewhere, successfully getting back the correct answers to a C++ " int SumArray(int...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
6
KoreyAusTex
by: KoreyAusTex | last post by:
If anyone can help me figure out the what the missing return statements are, I think it might be the fact that I need to add a return false in the getValue()? import java.util.*; public class...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.