asp.net the ENTER key to execute the corresponding button event code

Source: Internet
Author: User

There is a search input box in the header, the Search button is a button control, and the button control is also used in the news article search user Custom control.


stops the focus in the information title or content input box, and returns, but the input box in the header corresponds to the contents.


Reason: Enter execution is the first submit button on your page. Workaround: (for example, the information search input box below)


1, JS:


----------------------------------------------------------------------------


/* Enter after the button to perform * *


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 news article keyword cannot all be empty or cannot be longer than 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 search news article keyword string contains illegal characters (" + C + ")! ");


Searchkey.focus ();


return false;


            }


}


    }


}





2, aspx page control:


---------------------------------------------------


<input id= "Txtsearchkey" name= "Txtsearchkey" type= "text" runat= "server" maxlength= "onfocus=" This.select () ; "/>





3, CS:


---------------------------------------------------


this.txtSearchKey.Attributes.Add ("onkeyPRess", "Submitkeyclick (' Articlesearch1_btnsearch ');");


this.btnSearch.Attributes.Add ("onclick", "return Checkinputbox ();");





is in the header's CS and adds the attributes attribute of the input box.





this.txtKey.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.