我已尝试在代码中设置它,也在标记中设置它,但是当单击“下一步”按钮时,页面已经过验证,我希望在验证发生时以及何时不发生时将其发生并进行控制。任何建议或代码样本将不胜感激

有帮助吗?

解决方案

最简单的方法是从要跳过验证的 WizardStep 中删除所有验证器控件。

但是,如果您需要高级功能,则需要手动设置 StepNavigationTemplate 中的Next / Previous按钮的 CausesValidation 属性。 ASP.NET向导控件不公开允许您直接访问NavigationTemplates中的控件的属性,也不公开任何属性来访问NavigationTemplate。因此,我们需要依赖 FindControl 方法来进行所有搜索。

我在研究这个问题时发现的一条便利信息是,在运行时, StepNavigationTemplate 是一个名为 StepNavigationTemplateContainer 的内部ASP.NET类型,并且有一个ID“ ; StepNavigationTemplateContainerID&QUOT ;.这使我能够找到 StepNavigationTemplate ,因此找到了Next Button。

代码如下:


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;
    }
  }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top