Link to home
Start Free TrialLog in
Avatar of isidor
isidor

asked on

Cancel submit on an HTML button

Hello,

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">
   <u>S</u>earch
</button>

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.

/isidor
Avatar of Niversoft
Niversoft
Flag of Canada image


try
 type="button"  instead of type="submit"
or
 return false after the call to your btnSearch_ServerClick function
Avatar of isidor
isidor

ASKER

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.

/isidor
Avatar of isidor

ASKER

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.

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

-r-
ASKER CERTIFIED SOLUTION
Avatar of Arijit S
Arijit S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of isidor

ASKER

iamanindians answer worked fine. Thanks!