Codeproject有完整的解决方案:
http://www.codeproject.com/Articles/17241/Capturing-the-Enter-key-to-cause-a-button-click
正如文章所说:“决定哪种解决方案最适合您的需求”。
=================== 编辑后的答案 ============================
上面提到的链接介绍了两种捕获“回车键”事件的方法:
Javascript(将onKeyPress事件绑定到对象,并创建一个javascript函数来检查按下了哪个键并执行你的逻辑)
_Page_Load 中的代码:
//Add the javascript so we know where we want the enter key press to go
if (!IsPostBack)
{
txtboxFirstName.Attributes.Add("onKeyPress",
"doClick('" + btnSearch.ClientID + "',event)");
txtboxLastName.Attributes.Add("onKeyPress",
"doClick('" + btnSearch.ClientID + "',event)");
}
Javascript代码:
<SCRIPT type=text/javascript>
function doClick(buttonName,e)
{
//the purpose of this function is to allow the enter key to
//point to the correct button to click.
var key;
if(window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
if (key == 13)
{
//Get the button the user wants to have clicked
var btn = document.getElementById(buttonName);
if (btn != null)
{ //If we find the button click it
btn.click();
event.keyCode = 0
}
}
}
</SCRIPT>
面板控件
<asp:Panel ID="panSearch" runat="server" DefaultButton="btnSearch2" Width="100%" >
<asp:TextBox ID="txtboxFirstName2" runat="server" ></asp:TextBox>
</asp:Panel>
引用:
请注意,Panel标签有一个名为DefaultButton的属性。您可以将此属性设置为要在按Enter键时单击的按钮的ID。因此,Panel内的任何文本框都将将其Enter键按下事件定向到Panel的DefaultButton属性中设置的按钮。