The Textbox Control registers the carriage return event and triggers the button to submit the event.

Source: Internet
Author: User

Introduction:
In ASP. in the Net page, the index of the textbox is 1 (or n), the index of the subsequent submit button is 2 (n + 1), and the cursor is in the textbox, after you press enter, the focus is automatically moved to the button that follows the button, which triggers the button click event.

However, when accessing the Web page on the Lyncplus client, the submission event of the TextBox Control press ENTER auto-completion button becomes invalid (the focus should be automatically switched ).

Because the TextBox Control on the server does not provide events such as OnKeyPress or OnKeyDown, you cannot write background code for the carriage return event to call the Button click event.

As a result, the following two problems were finally solved by searching online.:
(1) execute JS code in the carriage return event of the TextBox Control to control the value of the page element.
(2) The Click Event of the server control is called in the carriage return event of the TextBox Control to execute the server C # code and implement related functions.
The specific implementation is as follows::
1. Register and trigger the server-side TextBox Control carriage return event
1. PageLoad Event code:Copy codeThe Code is as follows: protected void Page_Load (object sender, EventArgs e)
{
MessageTxt. Attributes. Add ("onkeypress", "EnterTextBox ()");
MessageTxt. Attributes. Add ("onkeydown", "EnterTextBox ()");
}

2. javascript code:Copy codeThe Code is as follows: <script language = "javascript">
Function EnterTextBox (){
If (event. keyCode = 13 & document. all ["MessageTxt"]. value! = "") // Press enter, and there is a value in the text box
{
$ ("# <% = HidKeywords. clientID %> "). val ($ ("# <% = MessageTxt. clientID %> "). val (). replace (/[^ \ u0000-\ u00FF]/g,
Function ($0 ){
Return escape ($0). replace (/(% u) (\ w {4})/gi, "$2 ;")
}));
}
}
</Script>

2. Call the server Button control click event in the TextBox Control carriage return event
1. PageLoad Event code: Same as above.Copy codeThe Code is as follows: protected void Page_Load (object sender, EventArgs e)
{
MessageTxt. Attributes. Add ("onkeypress", "EnterTextBox ()");
MessageTxt. Attributes. Add ("onkeydown", "EnterTextBox ()");
}

2. javascript code: Use the get button of the original dom object. Jquery cannot get the button.Copy codeThe Code is as follows: <script language = "javascript">
Function EnterTextBox (){
Var button = document. getElementById ('<% = btnSearch. ClientID %>'); // obtain the Page Object corresponding to the server control
If (event. keyCode = 13) // press ENTER
{
Button. click ();
Event. returnValue = false;
}
} </Script>

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.