views:

1857

answers:

5

The expected behaviour on enter in a form seems to be undefined in the HTML 4.01 Specification while the HTML 5 specification says it should be the first submit button in the form.

Internet Explorer (IE) highlights the first button in the form when the form has focus by adding a proprietary border or something. Other browsers do not add such a visual clue.

I'd like to use another button as the default and style this button so all browsers will provide a visual clue that it is the default button. (We're using ASP.NET which uses one form per page, and so it's hard to design the pages so that the default button always comes first.)

While I can easily accomplish this with javascript and css in most browsers, I'm having trouble making IE stop highlighting the first button.

Is there any way to tell IE to NOT highlight the first submit-button or to highlight a different button? Or is there some other solution that I've missed?

A: 

What about defining a particular CSS style (or class) for this button ?

<input type="button" value="basic button" .../>
<input type="button" style="border: solid 2px red;" value="default button" .../>
romaintaz
Yes, but that doesn't answer the question. I've tried to improve the question so it's clearer what I'm looking for.
Alf
A: 

If you are using .Net 2 this may help you

...Use the DefaultFocus property to access the control on the form to display as the control with input focus when the HtmlForm control is loaded. Controls that can be selected are displayed with a visual cue indicating that they have the focus. For example, a TextBox control with focus is displayed with the insertion point positioned inside of it. ...

Peter
Unfortunately, it doesn't help me. Thanks for trying though. :)
Alf
+1  A: 

One way to get around this is to create a dummy button at the top of the form and move it out of sight. Then handle the enter keycode in javascript and style the new default button with css.

Feels dirty though. Any better ideas?

Alf
+1  A: 

ASP.Net 2.0 also introduced the concept of DefaultButton.

This is available on atleast Form and Panel elements:

<form id="form1" runat="server" defaultbutton="Button2">
<div>
    <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:Button id="Button1" runat="server" text="1st Button" onclick="Button1_Click" />
    <br />
    <br />
    <asp:panel id="something" defaultbutton="button3" runat="server">
        <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:Button id="Button2" runat="server" text="2nd Button" onclick="Button2_Click" />
        <br />
        <asp:Button id="Button3" runat="server" text="3rd Button" onclick="Button3_Click" />

    </asp:panel>
    <br />
    <br />

</div>
</form>

So, when you load the page, or are entering text in TextBox1, pressing Enter will submit Button2. When you are entering text in TextBox2 pressing Enter will submit Button3 - as you are inside the Panel.

This is powered by an onkeypress method created by ASP.Net.

When the page loads, IE and Firefox both highlight Button2 (as you want).

If you know which button will be declared as the defaultbutton, you can use what ever CSS you would normally use to style this for all browsers.

Rather annoyingly, when you focus either text box, IE will then (incorrectly) highlight Button1.

This is because IE's default behaviour is overridden by some javascript emitted by ASP.Net:

<script type="text/javascript">
//<![CDATA[
WebForm_AutoFocus('Button2');//]]>
</script>

But, IE will then ignore that once another element has focus, and go back to highlighting the wrong button (which doesn't get used unless the user explicitly clicks it in this example).

Other than replacing them with image buttons, I'm not sure what else I can suggest.

Zhaph - Ben Duguid
A good description of the current state of afairs. Unfortunately the problem remains unsolved, as you yourself point out.
Alf
+2  A: 

on ur asp button control, set useSubmitBehavior="false". That renders the html as a button rather than a submit.

TK
Thank you. That works. :D
Alf