On the ASP. NET login page, how can I achieve automatic jump of text box focus and submit a form through the Enter key?

Source: Internet
Author: User
This article describes how to automatically redirect the focus of a text box on the ASP. Net Logon page and submit a form using the Enter key.
Required JavascriptCode:

< Script Language = "JavaScript" >

Ns4 = (Document. layers) ?   True : False ;

Function Checkenter (event, element)

{

VaR Code =   0 ;

If (Ns4)

Code = Event. Which;

Else

Code = Event. keycode;

If (Code = 13 )

{

If (Element. Name = 'Tbusername ') // Tbusername-name of the user name text box

{

Document. frmlogin. tbpassword. Focus ();//Frmlogin-form name, tbpassword-name of the Password text bar

}

If (Element. Name = 'Tbpassword ')

{

// Document. frmlogin. Submit (); Submit in this way, the Asp.net page will flash, but it is not actually submitted

// The following code can be used for submission. I checked the source file from the page generated by Asp.net and copied it.

If ( Typeof (Page_clientvalidate) ! = ' Function ' | Page_clientvalidate () _ dopostback ('lbllogin ','');

}

}

}

 

</ Script >



I put the code in a loginscript. js file, and then add the following code to the login. CS file to implement this function:

Tbusername. Attributes. Add ("onkeypress", "checkenter (event, this )");

Tbpassword. Attributes. Add ("onkeypress", "checkenter (event, this )");

System. Io. streamreader sr = new system. Io. streamreader (mappath ("script") + "\ loginscript. js ");

This. registerclientscriptblock ("loginscript", Sr. readtoend ());

Sr. Close ();

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.