Link to home
Start Free TrialLog in
Avatar of zhuansun
zhuansunFlag for United States of America

asked on

How to let 'Enter' key stroke to do the same thing as hit 'Submit' button in ASP.NET 2.0 application?

In login page of clasic asp application, if user enters password in password field and hit the 'Enter' key, the page is submitted without need to click the 'Submit' button.

How to do the same thing in ASP.NET 2.0 application? I think it should be easy but I cannot find the solution.

Thanks for your help.
Avatar of Maverick_Cool
Maverick_Cool
Flag of India image

This can be achieved by attaching a *Event 'onKeyDown' , in the event handler it key enter invoke form.submit.


*Using javascript
Avatar of _Zenix_
_Zenix_

Access keys

Attribute definitions

accesskey = character [CN]
    This attribute assigns an access key to an element. An access key is a single character from the document character set. Note. Authors should consider the input method of the expected reader when specifying an accesskey.

Pressing an access key assigned to an element gives focus to the element. The action that occurs when an element receives focus depends on the element. For example, when a user activates a link defined by the A element, the user agent generally follows the link. When a user activates a radio button, the user agent changes the value of the radio button. When the user activates a text field, it allows input, etc.

The following elements support the accesskey attribute: A, AREA, BUTTON, INPUT, LABEL, and LEGEND, and TEXTAREA.

This example assigns the access key "U" to a label associated with an INPUT control. Typing the access key gives focus to the label which in turn gives it to the associated control. The user may then enter text into the INPUT area.

<FORM action="..." method="post">
<P>
<LABEL for="fuser" accesskey="U">
User Name
</LABEL>
<INPUT type="text" name="user" id="fuser">
</P>
</FORM>

In this example, we assign an access key to a link defined by the A element. Typing this access key takes the user to another document, in this case, a table of contents.

<P><A accesskey="C"
      rel="contents"
      href="http://someplace.com/specification/contents.html">
    Table of Contents</A>

The invocation of access keys depends on the underlying system. For instance, on machines running MS Windows, one generally has to press the "alt" key in addition to the access key. On Apple systems, one generally has to press the "cmd" key in addition to the access key.

The rendering of access keys depends on the user agent. We recommend that authors include the access key in label text or wherever the access key is to apply. User agents should render the value of an access key in such a way as to emphasize its role and to distinguish it from other characters (e.g., by underlining it).
Page.RegisterHiddenField( "__EVENTTARGET", myButton.ClientID );
have a look at this:

<form name="myform" action="handleaction.asp">
<input type="submit" value="Submit" name="B1" onkeydown="javascript: submitform()">
</form>
<SCRIPT language="JavaScript">
function submitform()
{
  document.myform.submit();
}
</SCRIPT>
Hi, in asp.net you can do the same thing without adding any code, as long as you have only one submit button in your page, when Enter is hit the button's onclick event will be called.
ASKER CERTIFIED SOLUTION
Avatar of samtran0331
samtran0331
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zhuansun

ASKER

Your solution is the best for my ASP.NET 2.0 application.