验证 FinishButtonClick 上的所有向导步骤

发布于 2024-10-01 15:45:51 字数 612 浏览 0 评论 0原文

我有一个 asp:wizard 控件,其中包含五个 WizardSteps。所有这些步骤都有表单控件,并且大多数控件都有验证器。当用户使用下一个和上一个按钮逐步完成向导时,一切都运行良好,并且验证会按预期触发。但是,如果用户选择使用侧栏中的链接来导航向导,则他或她可以跳过某些步骤。提交最后一页(这是摘要页)时,向导中的控件可能无效。

我想做的是当用户单击完成按钮或用户进入摘要页面时检查所有控件的状态(或运行所有验证器)。我尝试通过这样做来运行 FinishButtonClick 事件中的所有验证器:

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

e.Cancel = !validates;

但是当我这样做时,每个验证器都声称它们是有效的。我还尝试将所有控件设置为 Visible = true;在此代码块之前,但这没有效果。知道可能出什么问题吗?或者这是一种更好的方法,也许是我缺少的向导控件的本机功能?

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.

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;

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜光 2024-10-08 15:45:51

您无法执行此操作,因为您尝试验证的控件未在页面上呈现。即验证器不存在,因此 Page.Validate()Page.IsValid 将返回 true,因为没有验证器,所以一切都是有效的。我希望有道理吗?

转到“查看源代码”,您将看到源代码仅包含向导当前步骤的标记。因此,前面页面上的任何验证器都不会呈现,因此不会被检查。

我建议隐藏侧边栏。这样,用户就无法跳过页面,并且当他们单击“下一步”时,将验证当前控件,因此只有在完成了所在页面后才能继续。

PS 您不需要循环遍历所有验证器并检查它们是否有效。只需使用 Page.Validate()(您甚至可以将 ValidationGroup 传递给此方法),然后检查 Page.IsValid 布尔值。

编辑:
根据以下评论:

页面属性:

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

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

在页面上单击下一次或侧边栏单击:

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

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?

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. 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.

EDIT:
As per comments below:

Page Property:

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;
听风吹 2024-10-08 15:45:51

一种选择是验证 SideBarButtonClick 事件,如果验证失败,则将 Cancel 设置为 true。那么您的用户永远不应该访问包含无效数据的摘要页面。

One option would be to validate page state in the SideBarButtonClick event, setting Cancel to true if it fails validation. Then your users should never reach the summary page with invalid data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文