Submit a form by pressing the enter key

Discussion in 'ASP .Net' started by Chinnala, Jan 9, 2007.

  1. Chinnala

    Chinnala Guest


    I have a "Search" button and "Cancel" button on a web page. When I hit
    enter, the "Cancel" button receives the focus and btnCancel event is
    fired. But I want the search button event to be fired when I click
    enter ? How can I implement this? Appreciate your replies.

    Chinnala, Jan 9, 2007
    1. Advertisements

  2. Chinnala

    mank1327 Guest

    I think either

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


    <form id="form1" runat="server" defaultfocus="">

    with the empty value being the id of your search button.
    mank1327, Jan 10, 2007
    1. Advertisements

  3. Likely You Cancel button is located before the Search one. Pressing the
    Enter is equal to clicking the first button on the form. The simplest
    solution would be to swap the buttons around.

    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    Eliyahu Goldin, Jan 10, 2007
  4. It's defaultButton='idOfTheButton'

    I would suggest using the following code in Page_Load or whereever your
    logic is appropriate:

    base.Page.Form.DefaultButton = buttonID.UniqueID;

    rather than directly using

    <form defaultbutton='buttonID' ..>

    unless your button is directly within the form.

    If your button is inside a UserControl or a datagrid / gridview etc, use
    UniqueID (value of ID attribute on the client side) to get the correct id
    which may or may not be same as the value of the property ID.
    Gaurav Vaish \(\), Jan 10, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.