验证 FinishButtonClick 上的所有向导步骤
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法执行此操作,因为您尝试验证的控件未在页面上呈现。即验证器不存在,因此
Page.Validate()
和Page.IsValid
将返回 true,因为没有验证器,所以一切都是有效的。我希望有道理吗?转到“查看源代码”,您将看到源代码仅包含向导当前步骤的标记。因此,前面页面上的任何验证器都不会呈现,因此不会被检查。
我建议隐藏侧边栏。这样,用户就无法跳过页面,并且当他们单击“下一步”时,将验证当前控件,因此只有在完成了所在页面后才能继续。
PS 您不需要循环遍历所有验证器并检查它们是否有效。只需使用 Page.Validate()(您甚至可以将 ValidationGroup 传递给此方法),然后检查 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()
andPage.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:
On page one next click or sidebar click:
一种选择是验证 SideBarButtonClick 事件,如果验证失败,则将
Cancel
设置为true
。那么您的用户永远不应该访问包含无效数据的摘要页面。One option would be to validate page state in the SideBarButtonClick event, setting
Cancel
totrue
if it fails validation. Then your users should never reach the summary page with invalid data.