TextBox Control register Carriage return event and trigger button commit event to implement _JAVASCRIPT techniques

Source: Internet
Author: User
Tags commit
Intro
In the ASP.net page, the index of the TextBox is 1 (or n), followed by the Submit button index of 2 (N+1), the cursor is in the textbox, and the auto focus moves after the carriage return to the button on the back, triggering the button's Click event.

However, when you access a Web page in the Lyncplus client, you experience a failure of the submit event of the TextBox control carriage return AutoComplete button (which should be the automatic focus of switching).

Because the server-side TextBox control does not provide events such as onkeypress or onkeydown, it is not possible to write a background code for the carriage return event to invoke the Click event of the button button.

then the Internet to find the relevant introduction, the final solution to the following two problems
(i) Implementation of the TextBox control carriage return event to execute the JS code to control the page element value.
(b) Implement the server-side C # code to implement the related functions by implementing the Click event of the server-side control in the TextBox control carriage return event.
specifically implemented as follows
Registering and triggering a server-side TextBox control carriage return Event
1.PageLoad Event code:
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
MESSAGETXT.ATTRIBUTES.ADD ("onkeypress", "Entertextbox ()");
MESSAGETXT.ATTRIBUTES.ADD ("onkeydown", "Entertextbox ()");
}

2.javascript Code:
Copy Code code as follows:

<script language= "JavaScript" >
function Entertextbox () {
if (Event.keycode = && document.all["messagetxt"].value!= "")//press ENTER, and the text box has a value
{
$ ("#<%=hidkeywords.clientid%>"). Val ($ ("#<%=messagetxt.clientid%>"). Val (). replace (/[^\U0000-\U00FF] /g,
Function ($) {
Return Escape ($). Replace (/(%u) (\w{4})/gi, "& #x $;")
}));
}
}
</script>

Second, the TextBox control carriage return event invokes the Service Side button control Click event
1.PageLoad event code: Ditto.
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
MESSAGETXT.ATTRIBUTES.ADD ("onkeypress", "Entertextbox ()");
MESSAGETXT.ATTRIBUTES.ADD ("onkeydown", "Entertextbox ()");
}

2.javascript Code: Note The original DOM object Fetch button is not available using jquery.
Copy Code code as follows:

<script language= "JavaScript" >
function Entertextbox () {
var button = document.getElementById (' <%=btnSearch.ClientID%> ');//Get the Page object for the server-side 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.