views:

195

answers:

2

Every attempt I had at making a button default failed, until someone on here posted some javascript that checks the textbox for the enter key and hits the button.

I tried putting my controls into a form and setting the default button, and some other stuff, but nothign worked except that javascript.

The problem I have now is that I am using an ajax extender that autocompletes the textbox (drops down a list of values as you start typing). People use it by typing in half of something, then hitting the down arrow until the one they want is highlighted, and pressing enter.

The enter press though doesn't select the item because of my javascript to make it press the default button. I am not sure how to get around this.

The annoying thing is that one of my pages, the search button somehow became the default button without me doing anything, so that page works fine. My other page, the button isnt default and I cannot figure out why not!

A: 

Ugh I found out it's because I had autopostback=true so it stopped the button being default.

SLC
A: 

<asp:Button> automatically have property UseSubmitBehaviour = true on them. From my experience (and I'm recalling this from memory), if all buttons on the page have that property set to true, the default button that will have the click event raised when the user presses Enter will be the first <asp:Button> that appears on the page, probably not the desired effect you want (unless you have only one button on the page that is a submit button, like a classical HTML input entry form).

You can set a default button for areas of the page by wrapping the area in a <asp:Panel> control that have a DefaultButton property you can assign the id of the <asp:Button> that you want the click event to be raised on when you press Enter in that part of the page.

You can also set UseSubmitBehaviour = false on all <asp:Button> controls.

Russ Cam