CodeProject上有這個完整的解決方案:
http://www.codeproject.com/Articles/17241/Capturing-the-Enter-key-to-cause-a-button-click
和喜歡的文章說: 「決定哪種解決方案最適合您的需求」
=========== ========編輯答案============================
上面提到的鏈接,談論了兩個捕獲「Enter鍵」事件的方法:
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>
報價:
注意到面板標籤有一個名爲DefaultButton屬性。您將 此屬性設置爲您想要點擊的按鈕的按鈕ID Enter鍵按下事件。因此面板內部的任何文本框將 指導其輸入按鍵在面板的DefaultButton 屬性設置按鈕
更好的定義,將上調用的函數都點擊按下鍵事件 – Badr 2011-05-10 09:47:44
如果你把它放在發生自動權的形式標記? – Ruben 2011-05-10 09:50:27