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_S erverClick " 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:retur n 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
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_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:retur
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
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
But that second idea might work; to set return false on the code-behind server event. I'll try it at work tomorrow.
/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
/isidor
Not sure about this, but couldn't you use: onclick="return false;" in addition to the onServerClick?
-r-
-r-
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
iamanindians answer worked fine. Thanks!
try
type="button" instead of type="submit"
or
return false after the call to your btnSearch_ServerClick function