Jan
5
2009

Default wizard button in ASP.NET



Comments available as RSS 2.0

When you press enter/return on a wizard control in ASP.NET, the default action is to just submit the form, disregarding any buttons which were pressed. This seems pretty counter-intuitive to me – if I press enter on a form, it should be obvious I want to go ‘Next’. After two hours trying to debug an installer wondering why it seemed to work sometimes and not others I eventually tracked it down to the default button. I’m using a master page, but the principle is the same if you’re not.

The method I used uses the Page_Load event to set the default button for the form.

if(!Page.IsPostBack) { // First page, nothing clicked
    Master.Page.Form.DefaultButton = wzdInstall.FindControl("StartNavigationTemplateContainerID$StartNextButton").UniqueID;
} else {
    switch(wzdInstall.ActiveStepIndex) { // set the default button to next
        case 0: //page has posted back but is still on page one, about to load page 2
        default:
            Master.Page.Form.DefaultButton = wzdInstall.FindControl("StepNavigationTemplateContainerID$StepNextButton").UniqueID;
            break;
        case 1: // my wizard only had three steps, but this should be the number of steps-1
            Master.Page.Form.DefaultButton = wzdInstall.FindControl("FinishNavigationTemplateContainerID$FinishButton").UniqueID;
            break;
    }
}
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

Comments

  1. DRWebMonkey says:

    I used the following, inspired by your code from inside the ActiveStepChanged step

            If CType(sender, Wizard).FindControl("StepNavigationTemplateContainerID$StartNextButton") IsNot Nothing Then
                Page.Form.DefaultButton = CType(sender, Wizard).FindControl("StepNavigationTemplateContainerID$StartNextButton").UniqueID
            End If
            If CType(sender, Wizard).FindControl("FinishNavigationTemplateContainerID$FinishButton") IsNot Nothing Then
                Page.Form.DefaultButton = CType(sender, Wizard).FindControl("FinishNavigationTemplateContainerID$FinishButton").UniqueID
            End If
  2. bahar says:

    hi,

    I get exception error when using this code in page load function of asp.net C#.
    plz let me know what should I do?
    my code is exactly:

    *if (!Page.IsPostBack)
            { // First page, nothing clicked
                Master.Page.Form.DefaultButton = Wizard1.FindControl("StartNavigationTemplateContainerID$StartNextButton").UniqueID;
            }
            else
            {
                //Wizard1.ActiveStepIndex = 0;
                switch (Wizard1.ActiveStepIndex)
                { // set the default button to next
                    case 0: //page has posted back but is still on page one, about to load page 2
                        break;
     
                    case 2: // my wizard only had three steps, but this should be the number of steps-1
                        Master.Page.Form.DefaultButton = Wizard1.FindControl("FinishNavigationTemplateContainerID$FinishButton").UniqueID;
                        break;
     
                    default:
                        Master.Page.Form.DefaultButton = Wizard1.FindControl("StepNavigationTemplateContainerID$StepNextButton").UniqueID;
                        break;
     
                }
            }
  3. Echilon says:

    And the exception is..?

Leave a Comment

Login using OpenID or enter your details below to leave a comment.

OpenID
Anonymous


Comment

Powered by WP Hashcash