Perform Button click event when user press Enter key in Textbox

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


&         


        
7条回答
  •  南方客
    南方客 (楼主)
    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:

    
    

    Panel Control

    
        
    
    

    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

提交回复
热议问题