Introduction:
In the ASP.Net page, the index of the textbox is 1 (or n), and the subsequent submit button has an index of 2 (n 1). The cursor is in the textbox, and the focus is automatic after pressing Enter. Moving to the following button will trigger the button's click event.
But when accessing the web page in the Lyncplus client, I encountered a situation where the submit event of the Enter auto-complete button of the TextBox control failed (it should automatically switch focus).
Since the server-side TextBox control does not provide events such as OnKeyPress or OnKeyDown, it is impossible to write background code for the Enter event to call the Button button's click event.
So I searched for relevant introductions on the Internet, and finally solved the following two problems:
(1) Implement JS code execution in the Enter event of the TextBox control to control page elements value.
(2) Implementation of calling the click event of the server control in the Enter 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 enter event of the server-side TextBox control
1. PageLoad event code:
protected void Page_Load(object sender, EventArgs e)
{
MessageTxt.Attributes.Add("onkeypress ", "EnterTextBox()");
MessageTxt.Attributes.Add("onkeydown", "EnterTextBox()");
}
2.javascript code:
2. Call the server-side Button control click event in the Enter event of the TextBox control
1.PageLoad event code: Same as above.
protected void Page_Load(object sender, EventArgs e)
{
MessageTxt.Attributes.Add("onkeypress", "EnterTextBox()");
MessageTxt.Attributes.Add("onkeydown", "EnterTextBox()");
}
2.javascript code: Note that the original dom object is used to obtain the button, which cannot be obtained using Jquery.