Wizard Control in ASP.NET - How to set the NextButton Causesvalidation property to false

前端 未结 1 438
野的像风
野的像风 2021-01-07 09:38

I have tried setting it in the code and also in the markup but when the Next Button is clicked, the page is validated, I want to prevnt this from happening and control when

相关标签:
1条回答
  • 2021-01-07 10:32

    The easiest way to do this would be to remove all validator controls from the WizardStep in which validation is to be skipped.

    However, if you need advanced functionality, you will need to set the CausesValidation property of the Next/Previous buttons in your StepNavigationTemplate manually. The ASP.NET Wizard control does not expose properties that let you access the controls in the NavigationTemplates directly, nor does it expose any properties to access the NavigationTemplate. So, we need to rely on the FindControl method to do all the searching.

    A handy piece of information that I found while researching this problem was that at runtime the StepNavigationTemplate is of an internal ASP.NET type called StepNavigationTemplateContainer and has an ID "StepNavigationTemplateContainerID". This enabled me to locate the StepNavigationTemplate and therefore, the Next Button.

    Code follows:


    protected void Wizard1_ActiveStepChanged(object sender, EventArgs e)
    {
      int step = Wizard1.ActiveStepIndex;
    
      // Disable validation for Step 2. (index is zero-based)
      if (step == 1)
      {
        ToggleValidation(false);
      }
      else  // Enable validation for subsequent steps.
      {  
        ToggleValidation(true);
      }
    }
    
    private void ToggleValidation(bool flag)
    {
      WebControl stepNavTemplate = this.Wizard1.FindControl("StepNavigationTemplateContainerID") as WebControl;
      if (stepNavTemplate != null)
      {
        Button b = stepNavTemplate.FindControl("StepNextButton") as Button;
        if (b != null)
        {
          b.CausesValidation = flag;
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题