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