I am working on an application that has a search page with two buttons: one to reset the form and the other one to submit the form. The reset button also clears information kept in session. By default, the Search button has the focus on that page, but has soon as we would click on a textbox, the focus would switch to the Reset button in IE, preventing people from being able to use the Enter key to submit the form. Normally, the solution to this problem would be to define the default button in the form tag as follows:

However, the form tag is in the Master Page in our case. I found a good post about this on another blog with a solution to the problem:

http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=374

Using that solution, I now have an OnPreRender event attached to the Search page Submit button. Here is the code-behind:

protected void btnSearch_PreRender(object sender, EventArgs e)
{
    Page.Form.DefaultButton = ((Button)sender).UniqueID;
    Page.Form.DefaultFocus = ((Button)sender).UniqueID;
}

After I added this code, it was possible to use the Enter key to submit the form. However, a problem  remained. Clicking on a textbox on the Search page still gave the focus to the Reset button. So, although the Enter key did sumbit the form, the highlighted button was still the Reset button. I then realized that by default, an asp:Button is rendered as a submit button. I changed that by adding the UseSubmitBehavior=”false” property to the button:

<asp:Button ID="btnReset" runat="server"
    UseSubmitBehavior="false"
    Text="Reset"
    OnClick="btnReset_Click"/>

And this solved the problem.

Leave a Reply

Your email address will not be published.