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

42 comments:

  1. Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
      or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

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

    ReplyDelete
  3. 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
  4. Good solution, but you cannot use Enter to click a button that has the focus...

    junhyjunhy

    ReplyDelete
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. Local Packers and Movers Chennai list, Cheap Packers Movers Chennai Charges, Affordable, Best Household Shifting Chennai visit here : Packers and Movers Chennai charges

    ReplyDelete
  16. 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
  17. 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
  18. Good post and I like it very much. By the way, anybody try this increase app downloads? I do not how to use.

    ReplyDelete
  19. • 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 updatingAzure Online training Hyderabad

    ReplyDelete
  20. Absolutely nothing is cost-free on this planet, but guarantee that you Buy facebook reviews exclusively from valid sources for good reputation on-line. buy facebook reviews ratings

    ReplyDelete
  21. Packers and Movers Chennai Give Safe and Reliable ***Household Shifting Services in Chennai with Reasonable ###Packers and Movers Price Quotation. We Provide Household Shifting, Office Relocation, ✔✔✔Local and Domestic Transportation Services, Affordable and Reliable Shifting Service Charges @
    Packers And Movers Chennai

    ReplyDelete
  22. Very Nice Article to read. Blog articles helped me alot in many ways to find the good solution. Am very happy to comments here. Thanks for sharing this kind of wonderful article in this blog. Lovely ! Kindly Visit Us @
    CCTV Camera Supplier | MATRIX dealers in Trivandrum

    ReplyDelete
  23. Manish Packers and Movers Pvt Ltd as a Services providing company can make all the difference to your Home Relocation experience. Indore based Company which offers versatile solutions, Right team that easily reduce the stress associated with a Household Shifting, Vehicle Transportation. we help things run smoothly and reduce breakages and offer you seamless, Affordable, Reliable Shifting Services, Compare Shifting Charges, Visit :
    Manish Packers and Movers Ptv Ltd @ https://www.manishpackersmoversindore.in/
    Packers and Movers Indore @ https://www.manishpackersmoversindore.in/packers-and-movers-indore.html
    Manish Packers Indore @ https://www.manishpackersmoversindore.in/manish-packers-and-movers-indore.html
    Packers and Movers Gurgaon @ https://www.manishpackersmoversindore.in/packers-movers-gurgaon.html
    Packers and Movers Kolkata @ https://www.manishpackersmoversindore.in/packers-movers-kolkata.html
    Packers and Movers Mumbai @ https://www.manishpackersmoversindore.in/packers-movers-mumbai.html
    Packers and Movers Bhopal @ https://www.manishpackersmoversindore.in/packers-movers-bhopal.html
    Packers and Movers Nagpur @ https://www.manishpackersmoversindore.in/packers-movers-nagpur.html
    Packers and Movers Raipur @ https://www.manishpackersmoversindore.in/packers-movers-raipur.html
    Packers and Movers Ahmedabad @ https://www.manishpackersmoversindore.in/packers-movers-ahmedabad.html

    ReplyDelete
  24. Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this website. Such a nice blog you are providing ! Kindly Visit Us @ Best Travels in Madurai | Tours and Travels in Madurai | Madurai Travels

    ReplyDelete