Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enter runs wrong button click event 2

Status
Not open for further replies.

angrymeeow

Programmer
Oct 17, 2002
24
0
0
ZA
I have a page with three normal web forms buttons on it. When I press the enter key on the page the click event of the incorrect button gets run. Is it possible to either change which click event gets run or to have no click event run when enter is pressed?

Any help will be greatly appreciated. :)

Grrrr...meeow [cat2]
 
Hi

When you use an ASP:Button control in your page the ASP.NET renders an <input type=&quot;submit&quot; /> tag to the outputted html. This automatically causes the form to be posted when the enter button is pressed. When you press enter the first submit button in the form will be the one to cause postback. Obviously this can have undesired results when you have several asp:button on the page that start off different processes on the server.

One way around this is to use a HtmlButton control rather than a Button control. You would declare this in your aspx using..

<input type=&quot;button&quot; id=&quot;button1&quot; runat=&quot;server&quot; onserverclick=&quot;myServerClickEventHandler&quot; />

The runat property makes the button available in your code behind where you would declare it as...

protected System.Web.UI.HtmlControls.HtmlButton

If you use VS.NET and like to Drag&Drop this control is available in the HTML tab of your toolbox.

As this renders an input of type=&quot;button&quot; it will not submit the form when the enter button is pressed.

Hope this helps

Rob

Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
Thanks for your reply. Is there a way to have no click event fire when the enter key is pressed?

Grrrr...meeow [cat2]
 
You should be able to use JavaScript or VBScript to capture the pressing of the Enter key, and cancel the event. Not tried this per-se though, usually I use this within TextBoxes to control input, but it seems feasible to me.

Rhys
Buffy: Spike, what are you doing here, five words or less!
Spike: Out for a walk... bitch!
 
I'll tell you why.

I've got a calendar on my page and instead of have to page through years of months to get to a certain date I've added a drop down with all the months of the year and a textbox that the user can type the year into. When the drop down changes the calander is automatically updated. Unfortunately with the textbox the user has to move to another control or press enter to update the calendar. I don't want to submit the request in case the user still has stuff to fill in.

Grrrr...meeow [cat2]
 
there is a cool asp.net control called defaultbuttons

it allows you to assign an input control to a button

so if your focus is on a textbox and you want it to fire button 2 instead of button1(default) you just use this control and when you hit enter button2 is fired instead.

Realy cool control, and best of all its FREE !!! Yes a FREE ASP.NET Control. its very easy to use too. Got mine working in minutes. Here is the website link


IF you are looking for some good .NET tutorial videos follow the resource link in my signature

Hope this helps :)

George Oakes
Goakes@TiresPlus.com = Programmer
George@1-Specialday.com = Mobile DJ
Check out this awsome .Net Resource!
 
[soapbox]
If anyone is interested, I think I know how VS decides which button will be the default submit button.

If you look in the html of a particular form the default will be the first one that appears in the code. I changed which button is the default by moving it's tag to the top and it seems to work.




[red]G[/red][purple]r[/purple][blue]r[/blue][green]r[/green][yellow].[/yellow][white]..[/white][yellow]m[/yellow][green]e[/green][blue]e[/blue][purple]o[/purple][red]w[/red] [cat2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top