On the web page, how do I press enter to get the focus and trigger the web Control event ..?

Source: Internet
Author: User
  A long time-consuming question: on a web page, press enter to get the focus and trigger a web control event. How can this problem be solved?

A web page
<Form id = "form1" runat = "server">
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<Asp: Button ID = "Button1" runat = "server" Text = "Button" OnClick = "button#click"/>
<Asp: TextBox ID = "TextBox2" runat = "server"> </asp: TextBox>
<Asp: Button ID = "Button2" runat = "server" Text = "Button" OnClick = "Button2_Click"/>
</Form>
When you input something in textBox1 and press enter, the button#click server event is triggered.
When you input something in textBox2 and press enter, the Button2_Click server event is triggered.

How can this problem be achieved? Sorry for a long time

========================================================== ====================================
TextBox2.Attributes. Add ("onkeydown", "if (event. keyCode = 13) {document. all. Button1.focus (); document. all. Button1.click ();}");
========================================================== ====================================
TextBox1.Attributes. Add ("onkeydown", "if (event. keyCode = 13) {document. all. Button1.focus (); document. all. Button1.click ();}");
The above is a bit incorrect.
========================================================== ====================================
Focus ()
========================================================== ====================================
Click directly. You don't need to focus.
========================================================== ====================================
Onkeypress = "if (event. keyCode = 13) btntasklt. click ();
========================================================== ====================================
You cannot use the server button or <input type = "submit"
Only html buttons are allowed.

<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<Input ID = "Button1" runat = "server" type = "button" Text = "Button" onserverclick = "button#click"/>
<Asp: TextBox ID = "TextBox2" runat = "server"> </asp: TextBox>
<Input type = "button" ID = "Button2" runat = "server" Text = "Button" onserverclick = "Button2_Click"/>

You can also click () without the focus button.
========================================================== ====================================
To hertcloud (· Sun Tzu's Art of War ·)
So how can I press enter to submit an imagebutton?
HTML buttons are difficult to use, and IMG is widely used for websites.
========================================================== ====================================
The same is true for imagebutton.

<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Asp: imagebutton id = "imagebutton1" runat = "server" onclick = "imagebutton1_click"/>
<Asp: textbox id = "textbox2" runat = "server"> </ASP: textbox>
<Asp: ImageButton ID = "ImageButton2" runat = "server" OnClick = "ImageButton2_Click"/>
TextBox1.Attributes. Add ("onkeypress", "if (event. keyCode = 13) {document. getElementById ('imagebutton1'). click (); return false ;}");
TextBox2.Attributes. Add ("onkeypress", "if (event. keyCode = 13) {document. getElementById ('imagebutton2'). click (); return false ;}");
========================================================== ====================================
<Asp: TextBox ID = "TextBox1" runat = "server"> </asp: TextBox>
<Asp: imagebutton id = "imagebutton1" runat = "server" onclick = "imagebutton1_click"/>
<Asp: textbox id = "textbox2" runat = "server"> </ASP: textbox>
<Asp: imagebutton id = "imagebutton2" runat = "server" onclick = "imagebutton2_click"/>
Textbox1.attributes. Add ("onkeypress", "If (event. keycode = 13) {document. getelementbyid ('imagebutton1'). Click (); Return false ;}");
Textbox2.attributes. Add ("onkeypress", "If (event. keycode = 13) {document. getelementbyid ('imagebutton2'). Click (); Return false ;}");
========================================================== ====================================

From: http://topic.csdn.net/u/20070416/14/f5600137-23d5-4aba-aa1e-176b3fea7400.html

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.