Fire event on enter key press for a textbox

后端 未结 10 1441
[愿得一人]
[愿得一人] 2020-12-01 02:05

I have the following asp.net textbox control.


After the user writes something in this

相关标签:
10条回答
  • 2020-12-01 02:31
    1. Wrap the textbox inside asp:Panel tags

    2. Hide a Button that has a click event that does what you want done and give the <asp:panel> a DefaultButton Attribute with the ID of the Hidden Button.

    <asp:Panel runat="server" DefaultButton="Button1">
       <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>    
       <asp:Button ID="Button1" runat="server" style="display:none" OnClick="Button1_Click" />
    </asp:Panel>
    
    0 讨论(0)
  • 2020-12-01 02:36

    My 2c.. I have used javascript, but found it did things that were not quite expected.

    USE the panel's defaultButton attribute/property as many of the above posts suggest. It is reliable (EASY) and works in all the browsers I have tested it on.

    0 讨论(0)
  • 2020-12-01 02:38

    Try this option.

    update that coding part in Page_Load event before catching IsPostback

     TextBox1.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('ctl00_ContentPlaceHolder1_Button1').click();return false;}} else {return true}; ");
    
    0 讨论(0)
  • 2020-12-01 02:41

    Try follow: Aspx:

    <asp:TextBox ID="TextBox1" clientidmode="Static" runat="server" onkeypress="EnterEvent(event, someMethod)"></asp:TextBox>    
    <asp:Button ID="Button1" onclick="someMethod()" runat="server" Text="Button" />
    

    JS:

    function EnterEvent(e, callback) {
            if (e.keyCode == 13) {
                callback();
            }
        }
    
    0 讨论(0)
  • 2020-12-01 02:42

    ahaliav fox 's answer is correct, however there's a small coding problem.
    Change

    <%=Button1.UniqueId%> 
    

    to

    <%=Button1.UniqueID%> 
    

    it is case sensitive. Control.UniqueID Property

    Error 14 'System.Web.UI.WebControls.Button' does not contain a definition for 'UniqueId' and no extension method 'UniqueId' accepting a first argument of type 'System.Web.UI.WebControls.Button' could be found (are you missing a using directive or an assembly reference?)

    N.b. I tried the TextChanged event myself on AutoPostBack before searching for the answer, and although it is almost right it doesn't give the desired result I wanted nor for the question asked. It fires on losing focus on the Textbox and not when pressing the return key.

    0 讨论(0)
  • 2020-12-01 02:47

    You could wrap the textbox and button in an ASP:Panel, and set the DefaultButton property of the Panel to the Id of your Submit button.

    <asp:Panel ID="Panel1" runat="server" DefaultButton="SubmitButton">
        <asp:TextBox ID="TextBox1" runat="server" />
        <asp:Button ID="SubmitButton" runat="server" Text="Submit" OnClick="SubmitButton_Click" />
    </asp:Panel>
    

    Now anytime the focus is within the Panel, the 'SubmitButton_Click' event will fire when enter is pressed.

    0 讨论(0)
提交回复
热议问题