在验证所有FinishButtonClick向导步骤 [英] Validate all wizard steps on FinishButtonClick

查看:408
本文介绍了在验证所有FinishButtonClick向导步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ASP:向导是包含五个WizardSteps控制。所有这些步骤都表单控件,大多数这些控件都有验证。在通过下一个和previous按钮一切向导的用户的步骤是伟大的工作,和验证触发,因为它应该。但是,如果用户选择使用导航在侧边栏的链接向导,他或她可以跳过一些步骤。当最后提交页面(这是一种摘要页)有可能是在无效向导控制。

I have an asp:wizard control that contains five WizardSteps. All of these steps have form controls, and most of these controls have validators. When the user steps through the wizard with the next and previous buttons everything is working great, and validation triggers as it should. However, if the user chooses to navigate the wizard using the links in the SideBar, he or she could skip some of the steps. When the last page is submitted (which is a summary page) there might be controls in the wizard that are invalid.

我想要做的是检查所有控件的状态(或运行所有验证)当用户单击Finish按钮时,或者当用户进入摘要页面。我已经做出了尝试做这个运行在FinishButtonClick事件的所有验证:

What I want to do is to check the state of all controls (or run all validators) when the user clicks the finish button, or when the user enters the summary page. I have made an attempt to run all the validators in the FinishButtonClick event by doing this:

bool validates = true;
foreach (IValidator validator in this.Validators) {
    validator.Validate();
    if (!validator.IsValid) {
        validates = false;
    }
}

e.Cancel = !validates;

但是当我这样做的每个验证声称它们是有效的。我也试过所有控件设置为可见=真实的;在此之前,code座,但没有任何效果。任何想法可能是错误的?或者是这样做,也许一个本地函数来我失踪向导控制的更好的办法?

But when I do this every validator claims that they are valid. I have also tried to set all controls to Visible = true; prior to this code block, but this has no effect. Any idea what could be wrong? Or is it a better way of doing this, maybe a native function to the wizard control that I'm missing?

推荐答案

您不能这样做,因为你正试图验证控件不会在页面上呈现。即验证不存在,那么 Page.Validate() Page.IsValid 因为没有将返回true验证,所以一切都是有效的。是有道理的,我希望?

You can't do this because the controls that you are trying to validate are not rendered on the page. i.e. the validators are not there, so Page.Validate() and Page.IsValid will return true because there are no validators, so everything is valid. Makes sense, I hope?

去查看源代码,你会看到源仅包含标记为向导的当前步骤。因此,对previous页的任何验证都不会呈现,因此不进行检查。

Go to View Source and you will see that the source only contains markup for the current step of the wizard. So any validators on previous pages are not rendered and hence not checked.

我会建议补充工具栏。这样,用户就不能跳过的页面,当他们点击下一步目前的控件将被验证,所以,如果他们已经完成了他们的网页,他们只能继续。

I would suggest hiding the SideBar. That way the user cannot skip pages and when they click 'Next' the current controls will be validated, so they can only continue if they have completed the page that they are on.

P.S。你不通过所有验证需要循环,并检查它们是有效的。只要使用Page.Validate()(您可以在的ValidationGroup甚至传递给此方法),然后检查Page.IsValid布尔。

P.S. You don't need to loop through all validators and check they are valid. Just use Page.Validate() (you can even pass a ValidationGroup to this method) and then check the Page.IsValid boolean.

编辑:
按照下面的评论:

As per comments below:

页面属性:

public bool PageOneValid
{
    get
    {
        if (ViewState["PageOneValid"] == null)
            return false;

        return (bool)ViewState["PageOneValid"];
    }
    set
    {
        ViewState["PageOneValid"] = value;
    }
}

在一个页面点击旁边或补充工具栏点击:

On page one next click or sidebar click:

Page.Validate("PageOne");
PageOneValid = Page.IsValid;

这篇关于在验证所有FinishButtonClick向导步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆