当用户在文本框中按Enter键时,执行按钮单击事件

jam*_*ams 37 javascript c# asp.net

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" />
</ContentTemplate>
</asp:UpdatePanel>
Run Code Online (Sandbox Code Playgroud)

我必须执行Button1click事件时,用户按Enter keyTextbox1

Har*_*run 84

将表单放在asp.net面板控件中,并使用按钮Id设置其defaultButton属性.请参阅以下代码:

  <asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
         <ContentTemplate>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" />
             </ContentTemplate>
          </asp:UpdatePanel>
    </asp:Panel>
Run Code Online (Sandbox Code Playgroud)

希望对你有帮助...

  • DefaultButton - 优雅的简洁 - 谢谢! (4认同)
  • +1非常简单,非常感谢.:0) (4认同)
  • 面板也很好地转换为div,所以只需用这个替换我的div并放入适当的cssclass.工作就像一个魅力. (2认同)
  • 比其他一些答案更喜欢这个,非常简单. (2认同)

Muh*_*tar 17

在aspx页面加载事件中,添加一个onkeypress框.

this.TextBox1.Attributes.Add(
    "onkeypress", "button_click(this,'" + this.Button1.ClientID + "')");
Run Code Online (Sandbox Code Playgroud)

然后添加此javascript以评估按键,如果是"输入",则单击右键.

<script>
    function button_click(objTextBox,objBtnID)
    {
        if(window.event.keyCode==13)
        {
            document.getElementById(objBtnID).focus();
            document.getElementById(objBtnID).click();
        }
    }
</script>
Run Code Online (Sandbox Code Playgroud)

  • 这是一个丑陋的黑客.正确的答案是更多的赞成. (11认同)
  • 小心,因为这会在Firefox中发出`window.event is undefined`. (2认同)

lie*_*ysd 6

Codeproject对此有一个完整的解决方案:

http://www.codeproject.com/Articles/17241/Capturing-the-Enter-key-to-cause-a-button-click

正如文章所说:“决定哪种解决方案最适合您的需求”

================== 编辑的答案 ============================

The link mentioned above, talks about two ways of capturing the "Enter Key" event:

Javascript (bind the onKeyPress event to the object and create a javascript function to check which key was pressed and do your logic)

_Page_Load in code behind:_

 //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)");
}
Run Code Online (Sandbox Code Playgroud)

Javascript code:

<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>
Run Code Online (Sandbox Code Playgroud)

Panel Control

<asp:Panel ID="panSearch" runat="server" DefaultButton="btnSearch2" Width="100%" >
    <asp:TextBox ID="txtboxFirstName2" runat="server" ></asp:TextBox>
</asp:Panel>
Run Code Online (Sandbox Code Playgroud)

Quoting:

Notice that the Panel tag has a property called DefaultButton. You set this property to the button ID of the button you want to be clicked on an Enter key press event. So any text box inside of the Panel will direct its Enter key press to the button set in the DefaultButton property of the Panel