困饶很久的一个问题,web页,按回车获得焦点,触发web控件事件..如何?
一个web页
<form id= "form1 " runat= "server ">
<asp:TextBox ID= "TextBox1 " runat= "server "> </asp:TextBox>
<asp:Button ID= "Button1 " runat= "server " Text= "Button " OnClick= "Button1_Click " />
<asp:TextBox ID= "TextBox2 " runat= "server "> </asp:TextBox>
<asp:Button ID= "Button2 " runat= "server " Text= "Button " OnClick= "Button2_Click " />
</form>
当在textBox1输入东西,按回车时,触发Button1_Click服务器事件
当在textBox2输入东西,按回车时,触发Button2_Click服务器事件
请问,如何实现?困饶很长时间了
======================================================================
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();} ");
上面的稍微有点不对
======================================================================
focus()
======================================================================
直接click就可,不用focus
======================================================================
onkeypress= "if (event.keyCode == 13) btntasklt.click();
======================================================================
不能使用 服务器按钮 或 <input type= "submit "
只能是 html按钮
<asp:TextBox ID= "TextBox1 " runat= "server "> </asp:TextBox>
<input ID= "Button1 " runat= "server " type= "button " Text= "Button " onserverclick= "Button1_Click " />
<asp:TextBox ID= "TextBox2 " runat= "server "> </asp:TextBox>
<input type= "button " ID= "Button2 " runat= "server " Text= "Button " onserverclick= "Button2_Click " />
而且可以不用focus 直接 按钮.click()即可
======================================================================
to hertcloud(·£孙子兵法£·)
那如果 我的是个imageButton 改怎么回车提交呢?
用html Button 比较难看 ,网站大多用的IMG
======================================================================
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;} ");
======================================================================
转自:
http://topic.csdn.net/u/20070416/14/f5600137-23d5-4aba-aa1e-176b3fea7400.html