21

I have an ASP.NET application where there's a few ASP.NET buttons and several plain HTML buttons. Anytime there's a textbox where a user hits enter, the ASP.NET button tries to submit the form.

I know I can change the defaultButton, but I don't want there to be any default button. I just want it so when the user presses enter it doesn't do anything.

I've tried setting defaultButton to blank, but that doesn't seem to work. How do I prevent the form from being submitted by the ASP.NET button when enter is pressed?

davidism
  • 121,510
  • 29
  • 395
  • 339
Ryan Smith
  • 8,344
  • 22
  • 76
  • 103

4 Answers4

36

You can set the button's UseSubmitBehavior = false

btnCategory.UseSubmitBehavior = false;
Evan Parsons
  • 1,139
  • 20
  • 31
David
  • 72,686
  • 18
  • 132
  • 173
  • That does indeed produce the behaviour needed: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior.aspx – Zhaph - Ben Duguid Sep 24 '09 at 18:58
  • @RyanSmith sometimes it's difficult to search for negatives, like "don't have a submit button" because of the number of ways the negative can be articulated. – Bob Kaufman Nov 08 '12 at 22:06
  • 2
    An additional note: ASP.Net's ImageButton doesn't have a UseSubmitBehavior attribute. If you're using one of those, see the "hidden dummy button" solution below. also see: [This question](http://stackoverflow.com/questions/1561400/how-to-disable-submit-behaviour-of-aspimagebutton) – CodePartizan May 14 '14 at 18:47
26

Here is what I used to fix this problem.

<form runat="server" defaultbutton="DoNothing">
        <asp:Button ID="DoNothing" runat="server" Enabled="false" style="display: none;" />
Bryan Legend
  • 6,790
  • 1
  • 59
  • 60
  • 1
    Nice hack! I had an asp.net imagebutton in my masterpage that kept submitting whenever the enter button was clicked on any page. However, this little trick fixed the problem. – Jagd Feb 02 '11 at 19:48
  • I tried literally everything but for whatever reason (bug) or something, ImageButtons continued to be fired off as DefaultButtons even when you don't set them as such. I tried all above attempts, this is the only thing that worked. As much as I hate use a dummy asp:Button it worked perfectly... – emalamisura Sep 13 '12 at 19:17
  • Kinda hate to use hacks like that, but this is the quickest solution when you have imageButtons. – Gabriel GM Sep 22 '14 at 16:09
1

I also had this problem with an ImageButton on my MasterPage (the Logout button) being set as the default button (so whenever someone pressed Enter, it would log them out). I solved it by using the following line in the Page_Load event on every child page, which is a bit of a work-around, but it works:

Form.DefaultButton = cmdSubmit.UniqueID;

Hope this helps someone else.

Thys
  • 149
  • 1
  • 5
0
<asp:TextBox ID="TXT_Quality" runat="server" Width="257px" 
onkeypress="return key_Pressed(event, this);">
</asp:TextBox>   

function key_Pressed(e, textarea)
 {
    var code = (e.keyCode ? e.keyCode : e.which);
    if(code == 13) 
    { 
       return false;
    }
    return true;
 }
Biju NB
  • 1
  • 1