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>