updated on 1/9/2008
有时候我们希望在TextBox输入完之后,直接敲击Enter键要求页面提交,在IE里有时候可以成功但有时候确出乎意料的未提交。
那么IE里敲击Enter键有没有什么规律呢。经查找资料发现还是有的:
1. When the Enter key is pressed within an ASP.NET Web Form that has multiple single-line textboxes, the form will submit and the Button Web control that appears first in the HTML page's markup will have its Click event fired.
2. If you have a Web Form with just one single-line text input (even if there are additional input fields, like CheckBoxLists, RadioButtonLists, DropDownLists, and so on), hitting enter in the text input will not cause the Button Web control's Click event to fire when using Internet Explorer.
因此正好在我看到这个资料的第二天, 有一个项目客户回馈:
In SubContList, the ENTER button should be the same as clicking Find.
在这个页面里,只有一个TextBox,一个查找按钮,正好符合上述的第二条规律,如果在TextBox里输入查询条件,直接敲击Enter键是不行的。因此只好再添加另外一个TextBox,但是要让其隐藏:
<asp:TextBox runat="server" style="DISPLAY:none;VISIBILITY:hidden" ID="Textbox1" Text="In order to accept enter event"/>
这样就符合第一条规则了,可以正常提交页面了。
我的项目是asp.net1.1的,IE6,不知道在asp.net2.0里可以如何处理。
另外大家可以在这里看到相关资料
Enter and the Button Click Event
Using JavaScript to prevent or trigger form submission when ENTER is hit
Two Common Pitfalls When Submitting a Web Form Using the Enter Key
在asp.net2.0中,加入了DefaultButton和DefaultFocus特性,极大的方便了我们的开发,推荐以下blog:
http://weblogs.asp.net/scottgu/archive/2005/08/04/421647.aspx
本篇blog涵盖的有:
Default Buttons in ASP.NET 2.0
Default Focus in ASP.NET 2.0
Focus on Errors
Putting it all together
![](/images/cnblogs_com/happyhzs2000/qsygsl.jpg)