KnowDotNet

Setting the Default Button in an ASP.NET Page with Multiple Submit Buttons

by Les Smith

Suppose I have multiple Submit Buttons on the same page that acts like a wizard.  How do I change the default button as the user moves through the wizard?

Normally, the first button on a page will be the default Submit button.  If you are hiding and showing Panels to create a Wizard effect, and you have multiple buttons that you want to be the default button, you must take special action to achieve this, albeit only one line of code.

In an application that I have just helped to complete, I converted a functionality that had previously taken 20+ ASP (classic) pages to one page that works like a wizard.  Instead of using the Wizard control, I used panels and hide and show them as the user progresses throught the process.  Consequently, I have multiple "Next" buttons, named btnNext1, btnNext2, etc.  As the user moves through the various panels that contain these buttons, I have to change the default Submit button to a new button.

The code for doing this is very simple.  In the page load event, use the following line of code to ensure that the btnNex1 is the default Submit button on the first panel.

  Form.DefaultButton = btnNext1.UniqueID;

In the code behind that processes the functionality of the btnNex1, in order to set up the btnNext2 as the default for the next panel (Step), I use the same type of instruction to change the default to the next "Next" button.

  Form.DefaultButton = btnNext2.UniqueID;

Obviously, this has to be repeated for each panel/Next button on your page.  

This way, on each page, the user can press the Enter button to advance to the next step of the wizard.  Regardless of the panel that is shown, the "Next" button on that panel will now receive the Enter button click event.

Need to automatically organize your code windows?  You'll be amazed how easy it is to keep the code in your code windows organized.  TRY IT FREE FOR 30 DAYS BY CLICKING HERE.



Automatically generate braces in C#! Try CSharpCompleter and stop wasting valuable time needlessly typing hundreds of braces {} daily.  Try CSharpCompleter for 30 DAYS FREE.



Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog.
  

You can also email me directly at les@KnowDotNet.com.