Asp.net sets the default button for carriage return.

Source: Internet
Author: User

When I enter a keyword and want to press the Enter key, I can search. However, because there are other buttons on the page, this is not my search button by default. I searched for some solutions online, the most is the following method:

In Asp.net, how do I press enter to trigger the event of the specified button?
Hypothesis:
<Asp: textbox id = "tbinput" runat = "server" width = "240px"> </ASP: textbox>
<Asp: button id = "btnok" runat = "server" borderwidth = "1px" bordercolor = "Purple" borderstyle = "solid" text = "search site"> </ASP: button>
Solution:
On the. ASPX page, add:
<Script language = "JavaScript">
Function submitkeyclick (button)
{
If (event. keycode = 13)
{
Event. keycode = 9;
Event. returnvalue = false;
Document. All [button]. Click ();
}
}
// -->
</SCRIPT>

Add the following in the page_load event:
Tbinput. Attributes. Add ("onkeydown", "submitkeyclick ('btnok ');");

But I added it to my code and found that my function was not implemented. Why?

The reason is that I used the template page master, so after the page is translated into the HTML code of the client, my button name has changed,

Changed from bt_search to ctl00 $ contentplaceholder1 $ bt_search

Therefore, only the background code is changed to tbinput. Attributes. Add ("onkeydown", "submitkeyclick ('ctl00 $ contentplaceholder1 $ bt_search ');");

In this way, the code will work normally.

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.