Solved

'Enter key pressed' event, form submit, Page_Load, __postback in .NET

Posted on 2004-09-22
11
50,926 Views
Last Modified: 2011-08-18
Hi,

I am using C#.NET to create some simple web pages. I noticed that 'enter_key_pressed' (window.event.keyCode == 13) will cause the postback to the server. I know I can link this event to the button_click, but have to cancel the event like:

if (window.event.keyCode == 13) {
           event.returnValue=false;
           event.cancel = true;
           updateButton.click();
}

Otherwise the page will be submitted twice (right?).

My question is how the 'Enter key pressed' causes the postback? I wrote a simple page to test, it didn't mean like whenever there's page, pressing the enter key would cause the form to submit. Also, I didn't see other event_handler was called in Page_Load upon pressing the enter key. So seems to me that the submit wasn't thru a default button clicking. It must be wired into some JavaScript functions created by .NET on the backend without my permission. If so, can I disable it?

What if there are more than one form, which one to submit then?

Is there a way to catch this event, or to distinguish this event from a real button_click event on the Page_Load() function? So instead of doing but.click() on client side, I can explicitly call the handler on the server-side?

Thanks a lot.

D.
0
Comment
Question by:ds869
  • 3
  • 2
  • 2
  • +1
11 Comments
 
LVL 24

Accepted Solution

by:
Justin_W earned 75 total points
ID: 12129287
>> My question is how the 'Enter key pressed' causes the postback?
It is part of IE's built-in behavior.  The standards don't really specify exactly how browsers are required to handle Enter, but it usually depends on the type and number of form input tags in the page.  You can capture and cancel the event using a OnKeyDown handler on the <body> tag if you want to prevent it completely.
0
 
LVL 7

Expert Comment

by:vnvk
ID: 12130515
4 open questions:
09/23/04 http://www.experts-exchange.com/Q_21141621.html "'Enter key pressed' event, form submit, ..."
09/14/04 http://www.experts-exchange.com/Q_21129660.html "Server-side Validator.IsValid value is a..."
10/10/03 http://www.experts-exchange.com/Q_20762418.html "How to add user defined functions (UDF) ..."
07/22/03 http://www.experts-exchange.com/Q_20685559.html "Can't open a remote file which is on sha..."

Please maintain your old Questions
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 12130801
D:

When you create a FORM tag and put text boxes and a submit button, the browser will usually treat the Enter key as a command to submit the form.  It is as if you are calling the submit() method of the form object in JavaScript.

Depending on what controls you have on your page, ASP.NET might write a JavaScript function called __doPostBack() into your page.  The controls that do that use it to send information about the control and the postback to the server by putting that data in the __EVENTTARGET and __EVENTARGUMENT hidden text box fields that are also added to the HTML.  Then __doPostBack calls the submit() method of the form object.  So no, you are not necessarily posting the page back twice by hitting the enter key.  I can see some situations where you would not want the enter key to cause a postback and you can cacnel the event as you show in your question, but unless there is a reason to do so, it should not be necessary.

John
0
 

Author Comment

by:ds869
ID: 12134564
Thaks all.

I realized that I also have a couple of ImageButton controls on the page in the form of <input type='image'>. When I removed them from the form along with <input type=submit>, pressing the 'Enter' key won't trigger the form.submit().

Is there a way to set the imagebutton as 'input' intead of 'submit', e.g. using <input type='button'> instead of <input type='submit'> ? And although the imagebutton did trigger the form.submit, its event handler at the server-side wasn't called. Why?

The reason that pressing the Enter key submits the form twice is that I linked this event to a inputbutton.click which will submit the form, and it also triggers one of the imagebutton to submit the form, again. Those image button is for sorting the table only, which will be done at the server-side.

No one seems interested in answering my last question:

>Is there a way to catch this event, or to distinguish this event from a real button_click event on the Page_Load() function? So instead of >doing but.click() on client side, I can explicitly call the handler on the server-side?


Thanks again.

d.

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ds869
ID: 12134812
So, if there's a submit button or an image button, the form will be submitted by enter.key.pressed, BUT not thru either one of the buttons, and the button's event handler at the server side won't be called. Is this right?
0
 

Author Comment

by:ds869
ID: 12135121
If I add the onClick to image button, like

<input type="image" name="te" id="te" src="./images/up.gif"  onClick="alert(event.keyCode)">

the event.keyCode is always 0, no matter I clicked on the image button or anywhere on the form.

Can anybody tell me why?

D.
0
 
LVL 10

Assisted Solution

by:jnhorst
jnhorst earned 75 total points
ID: 12135761
If you are clicking on a button or somewhere on the form, keyCode will be 0 because no key has been pressed on the keyboard.  KeyCode is for keyboard input, not mouse input.

What event handler are you referring to when you say it isn't being fired?  I added an image button to a form with nothing else on it and then did this in the Page_Load event.

if (!this.IsPostBack)
{
     this.ImageButton1.Attributes.Add("onclick", "javascript:alert(\"This is a test.\");");
     this.ImageButton1.Attributes.Add("onkeydown", "javascript:alert(\"KeyCode is \" + event.keyCode);");
}

I got the alert, clicked OK and the postback fired and the server-side click event executed.  If I tabbed the button into focus and hit the enter key I got my key code alert, then the onclick event fired and then the page posted to the server firing the server-side click event.

John
0
 
LVL 24

Expert Comment

by:Justin_W
ID: 12135806
>Is there a way to catch this event,
I answered this above.

>or to distinguish this event from a real button_click event on the Page_Load() function?
No, not without catching the event on the client side.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now