Perform Button click event when user press Enter key in Textbox

后端 未结 7 1268
情书的邮戳
情书的邮戳 2020-12-01 07:19


&         


        
相关标签:
7条回答
  • 2020-12-01 07:52

    use Jquery or something here is example

    of it http://riderdesign.com/articles/Check-username-availability-with-JQuery-and-ASP.NET.aspx i hope i will help you more

    0 讨论(0)
  • 2020-12-01 07:56

    Put your form inside an asp.net panel control and set its defaultButton attribute with your button Id. See the code below:

      <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>
    

    Hope this will help you...

    0 讨论(0)
  • 2020-12-01 08:08

    in the html code only, add a panel that contains the page's controls. Inside the panel, add a line DefaultButton = "buttonNameThatClicksAtEnter". See the example below, there should be nothing else required.

    <asp:Panel runat="server" DefaultButton="Button1"> //add this!
      //here goes all the page controls and the trigger button
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" />
    </asp:Panel> //and this too!
    
    0 讨论(0)
  • 2020-12-01 08:09

    Codeproject has a complete solution for this:

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

    and like the article says: "decide which solution best fits your needs"

    =================== EDITED ANSWER ============================

    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)");
    }
    

    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>
    

    Panel Control

    <asp:Panel ID="panSearch" runat="server" DefaultButton="btnSearch2" Width="100%" >
        <asp:TextBox ID="txtboxFirstName2" runat="server" ></asp:TextBox>
    </asp:Panel>
    

    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

    0 讨论(0)
  • 2020-12-01 08:13

    You can try:

    In HTML:

        <asp:TextBox ID="TextBox1" runat="server" onKeyDown="submitButton(event)"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    

    And javascript:

    function submitButton(event) {
            if (event.which == 13) {
                $('#Button1').trigger('click');
            }
        }
    

    Code behind:

    protected void Button1_Click(object sender, EventArgs e)
    {
            //load data and fill to gridview
    } // fixed the function view for users
    

    Hope this help

    0 讨论(0)
  • 2020-12-01 08:14

    You can do it with javascript/jquery:

    <script>
        function runScript(e) {
            if (e.keyCode == 13) {
                $("#myButton").click(); //jquery
                document.getElementById("myButton").click(); //javascript
            }
        }
    </script>
    
    <asp:textbox id="txtUsername" runat="server" onkeypress="return runScript(event)" />
    
    <asp:LinkButton id="myButton" text="Login" runat="server" />
    
    0 讨论(0)
提交回复
热议问题