页面首次加载时显示一组验证消息

3
我查看了SO,但没有找到有用的解决方案,它们要么没有回答,像这个孤立的链接,要么与问题无关。现在我在这里提问:
能否在页面首次加载时显示验证器的消息?
我的页面上有几个验证器(必填、自定义、范围……),我已经将它们分组。我希望必填验证组在第一次加载(!page.IsPostBack)时显示其消息,以便我的客户可以一眼看出什么是必需的,什么是不必要的。

谢谢大家,我的问题终于解决了。所有的回答都足够好,值得点赞(+1)。 - Mahdi Tahsildari
3个回答

5
以下代码应该符合您的要求。
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // for all validators to fire, first time
        Page.Validate();
        // Or Only validators under a specific group will fire
        Page.Validate("ValidationGroupName");
    }

}

2

只需在Page_Load中调用验证器的Validate()方法即可。

if(!IsPostBack)
{
    this.UserNameRequired.Validate();
    // and all the other mandatory validators
}

2
当然可以...只需调用表单的submit方法。
这不是你真正需要的吗?提交表单并获取所有消息?
$(function() {

    // this will submit the form, but as you have validation, 
    // it will fire up all validations
    $("form").submit(); 

});

这是一个我有时使用的简单技巧。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接