OBJECTIVE:
A Wizard control often needs two TextBoxes on specific steps. In this instance both Step5 and Step6 each require two TextBoxes in each step, that is, each step requires a TextBox to optionally collect an e-mail address and another TextBox to optionally collect a user name. Note however in this instance only one TextBox in each step may be used to submit data. Thus, the current step must remain displayed and an error message must be shown when both TextBoxes in a step contain data.
Step5 and Step6 each contain a label which is used to display default text (-- or --). When an error occurs the default label text is changed to display an error message (blah blah). There is no either-or group control validator so custom validation code must be written [1]. Compiler anomalies required creative use of branching logic statements [2].
1 // By Wizard1 OnActiveStepChanged event handler...
2 switch (Wizard1.ActiveStep.ID)
3 {
4 case "Step0":
5 ValidateSelectedSideBarListItems( );
6 break;
7 ...
8 ...
9 case "Step5":
10 ValidateSelectedSideBarListItems( );
11 break;
12 case "Step6":
13 ValidateSelectedSideBarListItems( );
14 break;
15 ...
16 ...
17 case "Step18":
18 ValidateSelectedSideBarListItems( );
19 break;
20 default:
21 ValidateSelectedSideBarListItems( );
22 break;
23 }//switch (Wizard1.ActiveStep.ID)
1 protected void ValidateSelectedSideBarListItems( )
2 {
3 if (Step5_EmailTextBox.Text.Length > 0 && Step5_NameTextBox.Text.Length > 0)
4 {
5 Step5_ValidationLabel.Text = "error message";
6 Wizard1.ActiveStepIndex = 5;
7 }
8 else if (Step6_EmailTextBox.Text.Length > 0 && Step6_NameTextBox.Text.Length > 0)
9 {
10 Step6_ValidationLabel.Text = "error message";
11 Wizard1.ActiveStepIndex = 6;
12 }
13 else
14 {
15 if (String.IsNullOrEmpty(Step5_EmailTextBox.Text.ToString( ))
16 || String.IsNullOrEmpty(Step5_NameTextBox.Text.ToString( )))
17 {
18 Step5_ValidationLabel.Text = "-- or --";
19 }
20 if (String.IsNullOrEmpty(Step6_EmailTextBox.Text.ToString( ))
21 || String.IsNullOrEmpty(Step6_NameTextBox.Text.ToString( )))
22 {
23 Step6_ValidationLabel.Text = "-- or --";
24 }
25 }
26 } //ValidateSelectedSideBarListItems( )
[1] By default the Wizard will PostBack each time the active step is changed so writing validation code on the server is usually a good place to start. Note to VB developers: I left my VB days behind by choosing to adopt C# which is syntactically exactly the same as JavaScript and it has become much easier and more efficient for me to write or otherwise replicate validation and other code on the client where -- ideally -- this particular type of validation should be done to avoid redundant trips to the server when an error of this type may occur. Starting validation on the server also ensures quality if JavaScript has been disabled in the browser.
[2] In the last "else" block of the ValidateSelectedSideBarListItems( ) method the String.IsNullOrEmpty method is used to avoid logical errors when using || (or) to compare values. Similarly, in the same "else" block the use of two branches of "if" were required. The use of an "else" would not allow the error messages to be reset for both TextBoxes.
<%= clintonG