Cancel submit on an HTML button


On my ASP.NET search page I have this html button control, which I use for the sole purpose of getting an underlined first letter:

<button id="btnSearch" runat="server" type="submit" onServerClick="btnSearch_ServerClick" AccessKey="S">

Now what this does upon clicking is executing not once but twice - once when the btnSearch_ServerClick fires (this is correct), but then also the submit. I must have the type="submit" there in order for the enter key to activate the button anywhere on the form elements, but I don't want the submit to fire. I've tried stuff like onsubmit="javascript:return false;" but it don't seem to work.

Anyone knows a way to tackle this? I want to mimic the behaviour of a submit button (with Enter key fires event and the button has slightly darkened edges), but I don't want the submit event to actually fire. Oh, and I would very much prefer better solutions than setting an onkeypress if keycode 13 on every text box on the page.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


 type="button"  instead of type="submit"
 return false after the call to your btnSearch_ServerClick function
isidorAuthor Commented:
type="button" will not give me the behaviour of pressing Enter on any form element to fire button, and also not the submit button look (darkened edges).

But that second idea might work; to set return false on the code-behind server event. I'll try it at work tomorrow.

isidorAuthor Commented:
No, the return false idea won't work either. You can't place return false right after the btnSearch_ServerClick call (it is not javascript, but server-side C#). And the ServerClick event is a void, so nothing can be returned from it.

Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Not sure about this, but couldn't you use: onclick="return false;" in addition to the onServerClick?

How about adding onSubmit="return false" in the form tag

<form name="form1" method="post" action="xyz.aspx" onSubmit="return false">

Does this work?


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
isidorAuthor Commented:
iamanindians answer worked fine. Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.