Submit a form by pressing the enter key

  • Thread starter Thread starter Chinnala
  • Start date Start date
C

Chinnala

All,

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.

Thanks,
Chinnala.
 
I think either

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

or

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

with the empty value being the id of your search button.
 
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.
 
Back
Top