skip to Main Content

I’ve looked everywhere and read everything and nothing I do seems to work so I’ll ask this question again.

I have an ASP.NET WebForms application that has a page that can collect multiple pieces of data (textboxes and dropdowns) in order to then perform a search. Once collecting all of the data from the user, the user can click the "Search" button to initiate the search. I’d like the user to also be able to simple hit the Enter button and have the search initiate. I cannot seem to do this successfully.

I’ve tried the DefaultButton approach at the Panel level as all of the ASP.NET controls are inside of a panel. No luck. I’ve tried the JavaScript function approach (defaultEnterKey) checking for event.keyCode === 13 and just trying to add an attribute to one of the text boxes (onkeydown) in the code behind just to get it to work. Nope.

I’m obviously missing something.

2

Answers


  1. Well, you could first try at the form level.

    so in the form tag, say try this:

    <form id="form1" runat="server" defaultbutton="Button3">
    

    Give the above a try – even with a update panel, and then a panel inside that update panel, the above should work.

    So try the form level setting – that is the outer most form that wraps everything in that given page.

    Edit: the whopper and Mount everest detail been noted that the page has a master, and thus of couse no form tag exists.

    Thus, I suggest this:

    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    
        <ContentTemplate>
        <asp:Panel ID="Panel1" runat="server" DefaultButton="Button2" >
    
        your markup here 
    
        </asp:Panel>
    
        </ContentTemplate>
    
    </asp:UpdatePanel>
    
    Login or Signup to reply.
  2. You can simply cuse jQuery to bind a function on KeyPress and check if it is the Enter key. If so then do something with it.
    The return false is needed to prevent the form from doing a PostBack.

    <asp:TextBox ID="TextBox1" runat="server" CssClass="CaptureEnter"></asp:TextBox>
    
    <script>
        $('.CaptureEnter').keypress(function (e) {
            if (e.keyCode === 13) {
                alert('Enter was pressed! Start searching for: ' + $(this).val());
                return false;
            }
        });
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search