HaterTot
HaterTot

Reputation: 407

ASP.NET RequiredFieldValidator not letting me navigate away from an asp:Wizard

I wish to allow the user an option to navigate away from a Register page. However the register page is an ASP:Wizard with RequiredFieldValidators.

How can I fix this without removing the RequiredFieldValidators? The "Next" button of the wizard seems to be built-in to the Wizard control and doesn't seem to let me apply a ValidationGroup property, which seems to be the typical way to handle this situation.

Thanks

Upvotes: 0

Views: 4810

Answers (3)

Parker
Parker

Reputation: 11

I was having the same issue. The original need was a way to allow a user to navigate away from an ASP.NET Wizard control page that implemented validation via the framework's ValidationGroup mechanism. In my case, I had a <asp:HyperLink> control in the header of my Site.master master file.

Rather than create a ValidationGroup for the wizard page and associate the wizard buttons with it, I chose to "exclude" my <asp:HyperLink> control by assigning it to a different, unused ValidationGroup.

Upvotes: 1

Greg
Greg

Reputation: 16680

From http://forums.asp.net/p/1022184/1385194.aspx

After searching over and over on how to validate my wizard control steps, i came up with these solutions. Remember that each step should have it's own validation group. For these sample i use "Form" as my validation group.

1) Validation on the next button click. For this one, i simply override the Previous and Next button autogenerated by the Wizard Control by using 2 asp:Button. You then set the cause validation to true and you assign a validation group. Note that the important part is in the CommandName section.

<StepNavigationTemplate >
<asp:Button ID="btnPrevious" runat="server" CssClass="WizardControlButton" Text="Previous" CommandName="MovePrevious"  />
<asp:Button ID="btnNext" runat="server" CssClass="WizardControlButton" CommandName="MoveNext" Text="Next" CausesValidation="true" ValidationGroup="Form" />
</StepNavigationTemplate>

2) Validation on Sidebar click This one is also simple. You add the code in the SideBarButtonClick event of your control and then you check the step id you are currently. After it validates the page with the validation group in parameters. If the page is not valid. It cancel the event.

Protected Sub wizRegistration_SideBarButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles wizRegistration.SideBarButtonClick
    If wizRegistration.ActiveStep.ID = "wizSelectPostalCode" AndAlso e.NextStepIndex > e.CurrentStepIndex Then
        Page.Validate("Form")
        If Not Page.IsValid() Then
            e.Cancel = True   
        End If
    End If
End Sub

Upvotes: 2

Ryan O&#39;Neill
Ryan O&#39;Neill

Reputation: 5697

Could you put another button outside of the wizard labelled 'Cancel' or 'Skip' and set the CausesValidation property to False?

Upvotes: 0

Related Questions