Thursday, August 25, 2011

ASP.NET: Disable Submit Form on Enter Key

By default, ASP.NET submits a form when the enter key is pressed.  When more than one is present, the first submit button in the page is 'clicked'.  You can change the default button which is pressed when the user presses the Enter key by using the following attribute in the form

<form id="form1" runat="server" defaultbutton="btnDefaultButton" >

I used this technique in order to disable this behaviour by using a dummy button at the end of the screen and pointing the defaultbutton to this button.  The dummy button must be invisible and do nothing, so here's the code

At the top:
<form id="form1" runat="server" defaultbutton="btnDisableEnter" >

At the bottom:
<asp:Button ID="btnDisableEnter" runat="server" Text="" OnClientClick="return false;" style="display:none;"/>

It seems to do the job quite well till now.  Let me know if you have a better solution or if you found it useful

25 comments:

  1. Thank you, that´s just what i´ve been looking for....

    ReplyDelete
  2. Thank you, it works but I have a question. I'm using a master page and I need to capture the "Enter" event. How can I capture that event in any page that use the master page?

    ReplyDelete
    Replies
    1. you will need to use javascript onkeydown or keypress event. Watch out for browser incompatibilities!

      Delete
    2. When using a master page, the DefaultButton property can be set on a Panel instead.

      Delete
  3. Good solution, but you cannot use Enter to click a button that has the focus...

    junhyjunhy

    ReplyDelete
  4. This Goos Solution !!!!
    But I have found something very good with JS
    http://www.itorian.com/2012/07/stop-from-submission-posting-on-enter.html
    Try This. Working fine !!!

    ReplyDelete
  5. Packers and Movers in Delhi @
    https://www.egpackers.in/packers-and-movers-delhi.php
    Packers and Movers in Gurgaon @
    https://www.egpackers.in/packers-and-movers-gurgaon.php
    Packers and Movers in Noida @
    https://www.egpackers.in/packers-and-movers-noida.php
    Packers and Movers in Faridabad @
    https://www.egpackers.in/packers-and-movers-faridabad.php
    Packers and Movers in Dwarka @
    https://www.egpackers.in/packers-and-movers-dwarka-delhi.php
    Packers and Movers in Ghaziabad @
    https://www.egpackers.in/packers-and-movers-ghaziabad.php
    Packers and Movers in Marathahalli @
    https://www.egpackers.in/packers-and-movers-marathahalli-bangalore.php
    Packers and Movers in Hyderabad @
    https://www.egpackers.in/packers-and-movers-hyderabad.php

    ReplyDelete
  6. I must say you had done a tremendous job,I appreciate all your efforts.Thanks alot for your writings......Waiting for a new 1.. We Provide Best Packers And Movers Bangalore List for Get Free Best Quotes, Compare Charges, Save Money And Time,
    Household Shifting Services. @ Packers And Movers in Bangalore

    ReplyDelete
  7. There's a lot of interesting info here! I'm very grateful for the post! movers and packers hyderabad , is here to make your life simpler.
    Packers And Movers Hyderabad

    Packers And Movers begumpet-hyderabad
    P ackers And Movers banjara-hills

    ReplyDelete
  8. Excellent post! I must thank you for this informative read. I hope you will post again soon. Warehousing of merchandise .
    While general look the things you ought to utilize little stockpiling zone space stockpiling range compartments to program the aides. Books get to be heavier when pressed 7 or more aides in a crate.
    Packers And Movers Chennai

    ReplyDelete
  9. Really impressive post. I read it whole and going to share it with my social circules. I enjoyed your article and planning to rewrite it on my own blog.
    please visit here
    Packers And Movers Bangalore

    ReplyDelete
  10. Lovely Website, Maintain the fantastic work. Thank you so much!
    Local Packers and Movers delhi list, Cheap Packers Movers delhi Charges, Affordable, Best Household Shifting delhi @
    http://packersmovers.top/delhi/

    ReplyDelete
  11. I must say you had done a tremendous job,I appreciate all your efforts.Thanks alot for your writings......Waiting for a new 1...Please visit our wonderful and valuable website-
    Packers and Movers Mumbai

    ReplyDelete
  12. Lovely Website, Maintain the fantastic work. Thank you so much!
    We Provide Best Packers And Movers ahmedabad List for Get Free Best Quotes, Compare Charges,
    Save Money And Time, Household Shifting Services @
    Packers and Movers Ahmedabad

    ReplyDelete
  13. must say you had done a tremendous job,I appreciate all your efforts.Thanks alot for your writings......Waiting for a new 1..
    Packers And Movers Bangalore to hyderabad

    ReplyDelete
  14. Local Packers and Movers Chennai list, Cheap Packers Movers Chennai Charges, Affordable, Best Household Shifting Chennai visit here : Packers and Movers Chennai charges

    ReplyDelete
  15. very good post which I really enjoy reading. It is not every day that I have the possibility to see something like this.
    Packers And Movers Pune

    ReplyDelete
  16. Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.more php jobs in hyderabad.

    ReplyDelete