Asp.net enter key executes the code for the corresponding button event

Source: Internet
Author: User

There is a search input box in the Header. The search Button is the Button control, which is also used in the custom control of "news article search.
Stop the focus in the "News Title or content" input box, press enter, but the corresponding content is the content of the input box in the Header.
Cause: Press enter to execute the first submit button on your page. Solution: (the following uses the information search input box as an example)
1. js:
----------------------------------------------------------------------------
/* Press enter and execute the button */
Function SubmitKeyClick (button ){
If (event. keyCode = 13 ){
Event. keyCode = 9;
Event. returnValue = false;
Document. all [button]. click ();
}
}

Function checkinputbox (){
Var charBag = "[^ '~ @ # $ % ^ &/'| *] ";
Var searchkey = document. getElementById ("ArticleSearch1_txtSearchKey ");

If (trim (searchkey. value). length = 0 | trim (searchkey. value). length> 30 ){
Alert ("the keyword of the news article cannot be blank or the length cannot exceed characters! ");
Searchkey. focus ();
Return false;
}

If (trim (searchkey. value )! = ""){
For (var I = 0; I <searchkey. value. length; I ++ ){
Var c = searchkey. value. charAt (I );
If (charBag. indexOf (c)>-1 ){
Alert ("the keyword string for searching news articles contains illegal characters (" + c + ")! ");
Searchkey. focus ();
Return false;
}
}
}
}

2. aspx page controls:
---------------------------------------------------
<Input id = "txtSearchKey" name = "txtSearchKey" type = "text" runat = "server" maxlength = "30" onfocus = "this. select ();"/>

3. cs:
---------------------------------------------------
This.txt SearchKey. Attributes. Add ("onkeyPRess", "SubmitKeyClick ('articlesearch1 _ btnsearch ');");
This. btnSearch. Attributes. Add ("onclick", "return checkinputbox ();");

In cs of the Header, add the Attributes attribute of the input box.

This.txt Key. Attributes. Add ("onkeypress", "SubmitKeyClick ('header1 _ btnsearchkey ');");

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.