在ASP.NET网页中,使用服务器button按钮,默认第一个button响应回车键的点击事件。
<wbr></wbr>
比如
<wbr></wbr>
<wbr><body><br><wbr><wbr><wbr> <form id="form1" runat="server"><br><wbr><wbr><wbr> <div><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> 姓名:<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br /><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> 密码:<asp:TextBox ID="txtPwd" runat="server"></asp:TextBox><br /><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><strong><asp:Button ID="btnSubmit" runat="server" Text="登陆"</strong></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr>οnclick="btnSubmit_Click" /></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr> <input id="Reset1" type="reset" value="重置" /><br><wbr><wbr><wbr> </div><br><wbr><wbr><wbr> </form><br> </body></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
这个表单中就按下回车就默认触发了btnSubmit_Click事件。
<wbr></wbr>
很多时候,第一个按钮这不是用户在点击回车后想做事情。一般来说,回车键是完成输入工作后提交数据如、关闭当前弹出窗口、输入关键字后回车进行搜索。
<wbr></wbr>
在ASP.NET网页中设置回车键的解决方法是使用javascript的onkeydown方法捕捉键盘点击事件,使用event.keyCode来获取用户点击的键位。
<wbr></wbr>
<head>
<wbr><wbr><wbr> <title></title><br><wbr><wbr><wbr> <link href="css/admin.css" type="text/css" rel="stylesheet"/><br><wbr><wbr><wbr> <script type="text/javascript"><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> function <strong>KeyDown</strong>() {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> var btnsearch = document.getElementByIdx_x_x('<strong><%=btnSearch.ClientID %></strong>');</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><strong>// 如果按下回车键</strong><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> if (window.event.keyCode == 13) {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> btnsearch.click();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><strong>//取消回车键的默认操作</strong><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> window.event.returnValue = false<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> }<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr> }<br><wbr><wbr><wbr> </script><br> </head><br> <<strong>body οnkeydοwn="KeyDown()"></strong></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>